现在的位置: 首页 > 编程开发 > Php > 编程开发 > 正文

如何在Yii中使用Phpmailer发送邮件

2015年10月16日 Php, 编程开发 ⁄ 共 774字 ⁄ 字号 暂无评论

1. 首先到phpmailer 在github托管的开源仓库下载最新的phpmailer 地址 https://github.com/PHPMailer/PHPMailer
2. 然后解压到yii项目目录下的 /protected/extensions/phpmailer 目录 将phpmailer 里面的 class.stmp.php 复制到 /protected/extensions/ 下改名为STMP.php 注意 下载下来的文件夹是大写的,在Linux和yii中是区分大小写的,改成小写的即可
3. 再新建文件 /protected/extensions/CPhpMailer.php,内容如下:

_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;
}
}?>

4. 配置文件/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',
),

5. 然后在控制器里面的代码是


$mailer = Yii::app()->phpMailer->_mailer;$mailer->Subject = '清松博客测试邮件';
$mailer->Body = 'Hello!';
$mailer->AddAddress('xxx@hotmail.com');
$mailer->AddAddress('xxx@gmail.com');
$mailer->send();

6. 由于以上代码格式可能被压缩,复制到编辑器后格式化一下即可。

7. 一切看起来都是这么的美好!

给我留言

您必须 [ 登录 ] 才能发表留言!

×