标签:
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();
就可以发出邮件了.
标签:
原文地址:http://www.cnblogs.com/tudou1223/p/4478805.html