zoukankan      html  css  js  c++  java
  • PHP使用rabbitmq发邮件简单使用

    将发邮件的mailer.php封装

    <?php
    use PHPMailerPHPMailerPHPMailer;
     
    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!
    ");
          
         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!
    ");
        $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);
        }
  • 相关阅读:
    终于,我还是对自己的博客下手了
    对字典进行排序
    小米官网的css3导航菜单
    背景色渐变
    处理手机上点击链接出现的蓝色边框
    如何修改HTML5 input placeholder 颜色
    自定义浏览器滚动条样式
    两行文字,固定宽高,超出部分以三点隐藏
    css3控制div上下跳动-效果图
    css3控制div上下跳动
  • 原文地址:https://www.cnblogs.com/dawuge/p/13164332.html
Copyright © 2011-2022 走看看