php用redis做消息队列发送邮件

@admin  December 15, 2016

<?php

/**
 * Created by PhpStorm.
 * User: lyang
 * Date: 2016/10/20
 * Time: 10:32
 */
namespace UT\Common\Lib\Email;

use UT\Common\Lib\Crypt;

class Email
{
    const AltBody             = "为了查看该邮件,请切换到支持 HTML 的邮件客户端"; 
    public $EMAILER;
    /**
     * @param \PHPMailer $mail
     * @param            $to
     * @param            $subject
     * @param string     $body
     *
     * @return bool|int
     */
    public function postmail(\PHPMailer $mail, $to, $subject, $body = '')
    {
        $mail->Subject = $subject;
        $mail->AltBody = self::AltBody;
        $mail->msgHTML($body);
        $mail->addAddress($to,$to);
        if (!$mail->send()) { 
            return false;
        } else { 
            return true;
        }
    } 
    /**
     * @return \PHPMailer
     */
    public static function getMailer()
    {
        if(self::$EMAILER instanceof \PHPMailer){
            return self::$EMAILER;
        }
        $mail = new \PHPMailer();
        $mail->isSMTP();
        $mail->SMTPDebug   = self::EMAIL_DEBUG;
        $mail->Debugoutput = 'html';
        $mail->Host        = "smtp.163.com";
        $mail->Port        = 25;
        $mail->SMTPAuth    = true;
        $mail->CharSet     = "utf-8";
        $mail->Username    = "yangakw@163.com";
        $mail->Password    = "*******";
        $mail->setFrom('yangakw@163.com', 'yangakw@163.com'); 
        return self::$EMAILER = $mail;
    }

    /**
     * @return \Redis
     */
    public static function getRedis()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        return $redis;
    }
    /**
     * @param $email
     * @param $subject
     * @param $body
     *
     * @return int
     */
    public static function pushEmailQueue($email, $subject, $body)
    {
        $redis           = self::getRedis();
        $data["email"]   = $email;
        $data["subject"] = $subject;
        $data["body"]    = $body;
        return $redis->lpush("EMAIL", json_encode($data));
    }  
}

添加新评论