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);
        }
  • 相关阅读:
    关于ArcMap中的地图文档单位
    洛谷—— P2983 [USACO10FEB]购买巧克力Chocolate Buying
    COGS——T 826. [Tyvj Feb11] GF打dota
    洛谷—— P1855 榨取kkksc03
    洛谷—— P2663 越越的组队
    COGS——T 1578. 次小生成树初级练习题
    Django中间件
    March 7 2017 Week 10 Tuesday
    March 6 2017 Week 10 Monday
    March 5 2017 Week 10 Sunday
  • 原文地址:https://www.cnblogs.com/dawuge/p/13164332.html
Copyright © 2011-2022 走看看