yii2.0 发送邮件笔记

Yii2.0集成SwiftMailer教程
本文介绍如何在Yii2.0框架中使用内置的SwiftMailer组件来发送邮件,包括配置SMTP服务器、设置邮件编码及发送者信息,并提供了一个简单的发送示例。

  Yii2.0内部已经集成了swiftmailer发送邮件类,无需再用phpmailer之类的类了,听说swiftmailer这个也很强大.既然yii2.0内部已经有了swiftmailer扩展,则就用它内部的方法实现就好了.

 在web.php里配置邮件的相关参数:

  

'mailer' => [  
           'class' => 'yii\swiftmailer\Mailer', //引入swiftmailer扩展
           'transport' => [  
               'class' => 'Swift_SmtpTransport',  
               'host' => 'smtp.163.com',//smtp服务器host
               'username' => '*****', //登录163的账号不要@以后的内容
               'password' => '*****',  //登录密码
               'port' => '25',  //端口
               'encryption' => 'tls',  //貌似是加密方式,没查到
                                   
                           ],   
           'messageConfig'=>[  
               'charset'=>'UTF-8', //消息编码为utf-8
               'from'=>['*****'=>'admin']//发送人邮箱  
               ],  
        ],

 

然后在方法里写下:

$mail= Yii::$app->mailer->compose();   //定义一个发送对象
        $mail->setTo('******'); //接收人邮箱 
        $mail->setSubject("邮件测试"); //标题
        $mail->setTextBody('zheshisha '); //内容
        $html = '';
        $html.= '<table border="1">';
        $html.= '<tr><th>1</th><th>2</th><th>3</th><th>4</th></tr>';
        $html.= '<tr><td>asd</td><td>dssad</td><td>asddsa</td><td>asdasda</td></tr>';
        $html.='</table>';
        $mail->setHtmlBody($html);  //发送的html内容
        //根据返回值判断  
        if($mail->send())  
            echo "发送成功";  
        else  
            echo "发送失败";   
        die();

就可以发出邮件了.

 

转载于:https://www.cnblogs.com/tudou1223/p/4478805.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值