yii发送邮件使用PHPMailer类详解 [ 1.1 版本 ]
php mailer下载地址:http://www.shouce.ren/post/d/id/1172
下载后把mailer解到extensions文件夹下
下面是完整示例代码:
配置文件里/protected/config/params.php设置如下
'emailUserName'=>'11111111@qq.com',
'emailPassword'=>'11111111',
'emailHost'=>'smtp.qq.com',
'emailPort'=>'25',
'emailTimeout'=>'20',
方法中
public static function sendMail($toemail = '', $subject = '', $message = '') {
$mailer = Yii::createComponent ( 'application.extensions.mailer.EMailer' );
//邮件配置
$mailer->SetLanguage('zh_cn');
$mailer->Host = Yii::app()->params['emailHost']; //发送邮件服务器
$mailer->Port = Yii::app()->params['emailPort']; //邮件端口
$mailer->Timeout = Yii::app()->params['emailTimeout'];//邮件发送超时时间
$mailer->ContentType = 'text/html';//设置html格式
$mailer->SMTPAuth = true;
$mailer->Username = Yii::app()->params['emailUserName'];
$mailer->Password = Yii::app()->params['emailPassword'];
$mailer->IsSMTP ();
$mailer->From = $mailer->Username; // 发件人邮箱
$mailer->FromName = Yii::app()->params['emailFormName']; // 发件人姓名
$mailer->AddReplyTo ( $mailer->Username );
$mailer->CharSet = 'UTF-8';
// 添加邮件日志
$modelMail = new MailLog ();
$modelMail->accept = $toemail;
$modelMail->subject = $subject;
$modelMail->message = $message;
$modelMail->send_status = 'waiting';
$modelMail->save ();
// 发送邮件
$mailer->AddAddress ( $toemail );
$mailer->Subject = $subject;
$mailer->Body = $message;
if ($mailer->Send () === true) {
$modelMail->times = $modelMail->times + 1;
$modelMail->send_status = 'success';
$modelMail->save ();
return true;
} else {
$error = $mailer->ErrorInfo;
$modelMail->times = $modelMail->times + 1;
$modelMail->send_status = 'failed';
$modelMail->error = $error;
$modelMail->save ();
return false;
}
}
手册网_shouce点ren
注册时间:2015-11-14
最后登录:2016-04-22
在线时长:3小时30分
最后登录:2016-04-22
在线时长:3小时30分
- 粉丝5
- 金钱85
- 威望20
- 积分315
共 1 条评论
没用 先收藏一下