用RabbitMQ发邮件(结合PHPMailer)

将发邮件的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;
            }
    }


}

// $mail->ErrorInfo();
?>
在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);
	}


打开cmd.exe(最好用管理员身份运行)先运行php send.php,然后运行php receive.php,如果receive.php不报错,则发送邮件成功

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值