手册网_shouce点ren 2015-11-20 20:14:51 6779次浏览 1条评论 5 1 0

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;
    } 
}
觉得很赞
您需要登录后才可以评论。登录 | 立即注册