Yii2.0 发送邮件教程
2017-06-22 本文已影响0人
云过飘雨
一、配置文件
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => false,// false发送到真实的邮箱之中,true会将邮件缓存到文件中
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.163.com', //每种邮箱的host配置不一样
'username' => 'youremail@163.com',
'password' => 'yourauthcode',//此处非邮箱密码,而是开启smtp服务之后的授权码
'port' => '25',
'encryption' => 'tls',
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['youremail@163.com'=>'admin']
],
],
二、使用方法
$mail = Yii::$app->mailer->compose();
$mail->setTo('email@163.com');
$mail->setSubject('I am new');//主题中不要写test
$mail->setHtmlBody('Hello ! Welcome !');
if($mail->send())
{
echo 'success';
}
else {
echo 'false';
}