最近做一个项目需要添加发邮件的功能,一次我收集了一些邮件配置的功能。并且实现将Yii移植到BAE上时实现同样的功能,向某个邮箱发送一份BAE的消息邮件,分享给大家,希望能给大家带来帮组。
本地Yii实现发邮件实例:
步骤一:
下载Yii的mailer扩展:
官方扩展链接:http://www.yiiframework.com/extension/mailer/
下载后解压放置在protected/extensions/下
步骤二:
申请一个邮箱作为发件箱,以下我们以163邮箱为例
步骤三:
public function actionMail(){ $message = 'mail success!!!'; $mailer = Yii::createComponent('application.extensions.mailer.EMailer'); $mailer->Host = 'smtp.163.com'; $mailer->IsSMTP(); $mailer->SMTPAuth = true; $mailer->From = 'XXXXXX@163.com'; //设置发件地址 $mailer->AddReplyTo('XXXXX@163.com'); $mailer->AddAddress('XXXXX@qq.com'); //设置收件件地址 $mailer->FromName = 'XXXXXX'; //这里设置发件人姓名 $mailer->Username = 'XXXXXXXX@163.com'; //这里输入发件地址的用户名 $mailer->Password = 'XXXX'; //这里输入发件地址的密码 $mailer->SMTPDebug = true; //设置SMTPDebug为true,就可以打开Debug功能,根据提示去修改配置 $mailer->CharSet = 'UTF-8'; $mailer->Subject = Yii::t('demo', 'Yii rulez!'); //设置邮件的主题 $mailer->Body = $message; $mailer->Send(); }
步骤四:
在浏览器中输入http://hostname/index.php?r=site/mail, 查看收件箱看是否收到邮件。如果设置正确一般不会出什么问题。
由于我们需要把应用部署到云端,而云端禁用mail函数,这是我们需要使用BAE给我提供的API来实现邮件服务。我们如果了解百度云定制的Yii框架,我们可以使用email邮箱来接受Yii运行的一些调试信息。这各主要是通过,CEmailLog类来实现这一功能,而百度云的定制框架上也实现了这个类。但需要更改一些东西。
更改/framework/baeconfig目录下的baeconfig.php文件,添加消息队列和收件箱。
'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( array( 'class'=>'CBaeLogRoute', 'levels'=>'error, warning,trace', ), array( 'class'=>'CEmailLogRoute', 'levels'=>'error, warning', 'queueName'=>'XXXXXXXX', //这里是消息队列的名称 'emails'=>'XXXXXX@qq.com',//这里是收件箱的名称 ), ), ),
下面我贴出BAE的CEmailLog类的实现代码:
<?php /** * CEmailLogRoute class file. * * @author Qiang Xue <qiang.xue@gmail.com> * @link http://www.yiiframework.com/ * @copyright Copyright © 2008-2011 Yii Software LLC * @license http://www.yiiframework.com/license/ */ /** * CEmailLogRoute sends selected log messages to email addresses. * * The target email addresses may be specified via {@link setEmails emails} property. * Optionally, you may set the email {@link setSubject subject}, the * {@link setSentFrom sentFrom} address and any additional {@link setHeaders headers}. * * @property array $emails List of destination email addresses. * @property string $subject Email subject. Defaults to CEmailLogRoute::DEFAULT_SUBJECT. * @property string $sentFrom Send from address of the email. * @property array $headers Additional headers to use when sending an email. * * @author Qiang Xue <qiang.xue@gmail.com> * @version $Id: CEmailLogRoute.php 3426 2011-10-25 00:01:09Z alexander.makarow $ * @package system.logging * @since 1.0 */ class CEmailLogRoute extends CLogRoute { /** * @var array list of destination email addresses. */ private $_email=array(); /** * @var string email subject */ private $_subject; /** * @var string email sent from address */ private $_from; /** * @var array list of additional headers to use when sending an email. */ private $_headers=array(); /** *the queueName of BCMS */ public $queueName=''; /** *initilize the queueName */ public function __construct($queueName='') { $this->queueName=$queueName; } /** * Sends log messages to specified email addresses. * @param array $logs list of log messages */ protected function processLogs($logs) { $message=''; foreach($logs as $log) $message.=$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]); $message=wordwrap($message,70); $subject=$this->getSubject(); if($subject===null) $subject=Yii::t('yii','Application Log'); if(IS_BAE) { $this->sendEmail($this->getEmails(),$subject,$message); } else { foreach($this->getEmails() as $email) $this->sendEmail($email,$subject,$message); } } /** * Sends an email. * @param string $email single email address * @param string $subject email subject * @param string $message email content */ protected function sendEmail($email,$subject,$message) { if(IS_BAE) { require_once ( "Bcms.class.php" ) ; $bcms = new Bcms(); $ret = $bcms->mail ( $this->queueName, $message,json_encode($email)) ; if ( false === $ret ) { Yii::trace("send log to email is failed"); } else { Yii::trace("send log to email success"); } } else { $headers=$this->getHeaders(); if(($from=$this->getSentFrom())!==null) $headers[]="From: {$from}"; mail($email,$subject,$message,implode("\r\n",$headers)); } } /** * @return array list of destination email addresses */ public function getEmails() { return $this->_email; } /** * @param mixed $value list of destination email addresses. If the value is * a string, it is assumed to be comma-separated email addresses. */ public function setEmails($value) { if(IS_BAE) { if(is_array($value)) $this->_email=$value; else $this->_email=preg_split('/[\s,]+/',$value,-1,PREG_SPLIT_NO_EMPTY); } else { if(is_array($value)) $this->_email=$value; else $this->_email=preg_split('/[\s,]+/',$value,-1,PREG_SPLIT_NO_EMPTY); } } /** * @return string email subject. Defaults to CEmailLogRoute::DEFAULT_SUBJECT */ public function getSubject() { return $this->_subject; } /** * @param string $value email subject. */ public function setSubject($value) { $this->_subject=$value; } /** * @return string send from address of the email */ public function getSentFrom() { return $this->_from; } /** * @param string $value send from address of the email */ public function setSentFrom($value) { $this->_from=$value; } /** * @return array additional headers to use when sending an email. * @since 1.1.4 */ public function getHeaders() { return $this->_headers; } /** * @param mixed $value list of additional headers to use when sending an email. * If the value is a string, it is assumed to be line break separated headers. * @since 1.1.4 */ public function setHeaders($value) { if (is_array($value)) $this->_headers=$value; else $this->_headers=preg_split('/\r\n|\n/',$value,-1,PREG_SPLIT_NO_EMPTY); } }
BAE上发送邮件通过bae的云消息API的Mail函数实现的。
public function mail($queueName,$message,$address,$optional=array())
以下是我修改后的Mailer类(仿CEmailLog)
<?php class Mailer{ /** * @var array list of destination email addresses. */ private $_email=array(); /** * @var string email subject */ private $_subject; /** * @var string email sent from address */ private $_from; /** * @var array list of additional headers to use when sending an email. */ private $_headers=array(); /** *the queueName of BCMS */ public $queueName='XXXXXXXXXXXXXXXXXXXXXXXXXXX'; //这里填你的消息队列的名称 /** *initilize the queueName */ public function __construct() { include_once ( "Bcms.class.php" ) ; } /** * Sends an email. * @param string $email single email address * @param string $subject email subject * @param string $message email content */ public function sendEmail($email,$subject,$message) { if(IS_BAE) { $log = new Log; $bcms = new Bcms(); $ret = $bcms->mail ( $this->queueName, $message,array($email)) ; if ( false === $ret ) { $log->fb("send log to email is failed"); Yii::trace("send log to email is failed"); } else { $log->fb("send log to email success"); Yii::trace("send log to email success"); } } else { $headers=$this->getHeaders(); if(($from=$this->getSentFrom())!==null) $headers[]="From: {$from}"; mail($email,$subject,$message,implode("\r\n",$headers)); } } /** * @return array list of destination email addresses */ public function getEmails() { return $this->_email; } /** * @param mixed $value list of destination email addresses. If the value is * a string, it is assumed to be comma-separated email addresses. */ public function setEmails($value) { if(IS_BAE) { if(is_array($value)) $this->_email=$value; else $this->_email=preg_split('/[\s,]+/',$value,-1,PREG_SPLIT_NO_EMPTY); } else { if(is_array($value)) $this->_email=$value; else $this->_email=preg_split('/[\s,]+/',$value,-1,PREG_SPLIT_NO_EMPTY); } } /** * @return string email subject. Defaults to CEmailLogRoute::DEFAULT_SUBJECT */ public function getSubject() { return $this->_subject; } /** * @param string $value email subject. */ public function setSubject($value) { $this->_subject=$value; } /** * @return string send from address of the email */ public function getSentFrom() { return $this->_from; } /** * @param string $value send from address of the email */ public function setSentFrom($value) { $this->_from=$value; } /** * @return array additional headers to use when sending an email. * @since 1.1.4 */ public function getHeaders() { return $this->_headers; } /** * @param mixed $value list of additional headers to use when sending an email. * If the value is a string, it is assumed to be line break separated headers. * @since 1.1.4 */ public function setHeaders($value) { if (is_array($value)) $this->_headers=$value; else $this->_headers=preg_split('/\r\n|\n/',$value,-1,PREG_SPLIT_NO_EMPTY); } }
步骤二:
实现Mailaction:
public function actionMail(){ $message = 'Hello World!'; $mailer = new Mailer; $log->fb($mailer); $mailer->sendEmail('XXXXXXXXX@qq.com','Yii rulez!',$message); }
步骤三:
在浏览器中输入http://hostname/index.php?r=site/mail, 查看收件箱是否收到BAE的邮件。