xmihu945 2011-10-31 16:03:24 5830次浏览 2条回复 0 1 0

1、从 http://sourceforge.net/projects/phpmailer/ 下载phpmailer,解压在 /protected/extensions/phpmailer 目录 2、新建文件 /protected/extensions/CPhpMailer.php,内容如下:

<?php
require_once(Yii::getPathOfAlias('application.extensions.phpmailer').DIRECTORY_SEPARATOR.'class.phpmailer.php');

class CPhpMailer
{
    //其他属性参考phpmailer的配置
    function __construct()
    {
        $this->_mailer = new PHPMailer();
        $this->_mailer->CharSet = "utf-8";
        $this->_mailer->IsSMTP();
        $this->_mailer->SMTPAuth = true;
        $this->_mailer->AltBody = "text/html";
        $this->_mailer->IsHTML(true);
    }
    function init()
    {
        $this->_mailer->Host = $this->host;
        $this->_mailer->Port = $this->port;
        $this->_mailer->Username = $this->user;
        $this->_mailer->Password = $this->pass;
        $this->_mailer->From = $this->from;
        $this->_mailer->FromName = $this->fromName;
    }
}
?>

3、配置文件/protected/config/main.php中载入组件

'components'=>array(
    'phpMailer'=>array(
        'class'=>'application.extensions.CPhpMailer',
        'host' => 'mail.myhost.com',
        'port' => 25,
        'from' => 'myname@myhost.com',
        'fromName' => 'myname',
        'user' => 'username',
        'pass' => 'password',
),

4、控制器中写发送逻辑

$mailer = Yii::app()->phpMailer->_mailer;
$mailer->Subject = '人类已经阻止不了我发送邮件了';
$mailer->Body = '<font color="red">hello, 我是葫芦娃</font>';
$mailer->AddAddress('xxx@hotmail.com');
$mailer->AddAddress('xxx@gmail.com');
$mailer->send();

5、over

觉得很赞
您需要登录后才可以回复。登录 | 立即注册