YII中用phpmailer发送邮件 [ 新手入门 ]
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
共 2 条回复
- 
 dusong0717 回复于 2013-05-04 14:14 举报 dusong0717 回复于 2013-05-04 14:14 举报定,支持 
- 
 dashixiong 回复于 2017-09-02 15:57 举报 dashixiong 回复于 2017-09-02 15:57 举报人类确实无法阻止你发这封邮件了,连我都无法阻止你。 
xmihu945
            注册时间:2011-10-28
最后登录:1970-01-01
在线时长:0小时0分
    最后登录:1970-01-01
在线时长:0小时0分
- 粉丝0
- 金钱75
- 威望0
- 积分75
