PHP使用rabbitmq发邮件简单使用

将发邮件的mailer.php封装

<?php
use PHPMailer\PHPMailer\PHPMailer;
 
include_once "phpMailer/PHPMailer.php";
include_once "phpMailer/Exception.php";
include_once "phpMailer/SMTP.php";
class Mailer{
    public $username="123456789@qq.com";//发送的邮箱
    public $password="*************";//qq邮箱授权码
    public function sendMail($title,$content,$address)
    {
        $mail = new PHPMailer();
        $mail->SMTPDebug = 1;
        $mail->isSMTP();
        $mail->SMTPAuth=true;
        $mail->Host = ‘smtp.qq.com‘;
        $mail->SMTPSecure = ‘ssl‘;
        $mail->Port = 465;
 
        $mail->CharSet = ‘UTF-8‘;
        $mail->FromName = ‘啦啦啦啦一朵花‘;
        $mail->Username =$this->username;
        $mail->Password =$this->password;
        $mail->From=$this->username;
        $mail->isHTML(true);
        
        $mail->addAddress($address,"aaa");
        $mail->Subject = $title;
        $mail->Body = $content;
        $status = $mail->send();
        if($status) {
                return 1;
            }else{
                return 0;
            }
    }
}

在RabbitMQ的send.php写:

<?php
      
     $exchangeName = ‘demo‘;
     $queueName = ‘hello‘;
     $routeKey = ‘hello‘;
     $message = ‘Hello World!‘;
     $connection = new AMQPConnection(array(‘host‘ => ‘127.0.0.1‘, ‘port‘ => ‘5672‘, ‘vhost‘ => ‘/‘, ‘login‘ => ‘guest‘, ‘password‘ => ‘guest‘));
     $connection->connect() or die("Cannot connect to the broker!\n");
      
     try {
             $channel = new AMQPChannel($connection);
             $exchange = new AMQPExchange($channel);
             $exchange->setName($exchangeName);
             $queue = new AMQPQueue($channel);
             $queue->setName($queueName);
 
             $arr=[
                [
                 "title"=>"I miss you really",
                 "content"=>"红红火火恍恍惚惚",
                 "address"=>"234567891@qq.com" 
                ],
                [
                  "title"=>"I miss you really",
                 "content"=>"红红火火恍恍惚惚",
                 "address"=>"23344556677@qq.com"
                ]
             ];
             foreach ($arr as $v){
                 $res=$exchange->publish(json_encode($v), $routeKey);
                 var_dump($res);
             }    
             
     } catch (AMQPConnectionException $e) {
             var_dump($e);
             exit();
     }

另一个是RabbitMQ下的receive.php

<?php 
 
    $exchangeName = ‘demo‘;
    $queueName = ‘hello‘;
    $routeKey = ‘hello‘;
     
    $connection = new AMQPConnection(array(‘host‘ => ‘127.0.0.1‘, ‘port‘ => ‘5672‘, ‘vhost‘ => ‘/‘, ‘login‘ => ‘guest‘, ‘password‘ => ‘guest‘));
    $connection->connect() or die("Cannot connect to the broker!\n");
    $channel = new AMQPChannel($connection);
    $exchange = new AMQPExchange($channel);
    $exchange->setName($exchangeName);
    $exchange->setType(AMQP_EX_TYPE_DIRECT);
    $exchange->declareExchange();
    $queue = new AMQPQueue($channel);
    $queue->setName($queueName);
    $queue->declareQueue();
    $queue->bind($exchangeName, $routeKey);
     
    var_dump(‘[*] Waiting for messages. To exit press CTRL+C‘);
    while (TRUE) {
            $queue->consume(‘callback‘);
    }
    $connection->disconnect();
     
    function callback($envelope, $queue) {
            $msg = $envelope->getBody();
            $msg = json_decode($msg,true);
            include_once ‘mailer.php‘;
 
            $mail = new Mailer();
            $res=$mail->sendMail($msg["title"],$msg["content"],$msg["address"]);
            var_dump($res);
    }

 

PHP使用rabbitmq发邮件简单使用

上一篇:Cookie replay attacks in ASP.NET when using forms authentication


下一篇:使用cmd命令行(.NET Core CLI)来启动ASP.NET Core 应用程序的多个实例