Yii2.0 发送邮件

来源:互联网 发布:mysql全套视频百度云 编辑:程序博客网 时间:2024/06/05 07:52

很多系统处理忘记密码问题时会采用发送邮件的方式,这篇文章是我在开发过程中总结的方法,还碰到了让人哭笑不得的错误,当时也是花了好久时间才调试好,分享给大家。
我开发用的是Yii2.0的高级模板。

一、配置文件 common/config/main-local.php

'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' => 'yourAuthenticationCode',//此处非邮箱密码,而是开启smtp服务之后的授权码           'port' => '25',           'encryption' => 'tls',                       ],       'messageConfig'=>[           'charset'=>'UTF-8',           'from'=>['youremaill@163.com'=>'admin']           ],            ],

二、使用方法

$mail = Yii::$app->mailer->compose();       $mail->setTo('***email***@163.com');       $mail->setSubject('Your subject');//主题中不要写test       $mail->setHtmlBody('Hello , Welcome !');

三、开启邮箱的SMTP服务

确保你用于发送邮件的邮箱开启了SMTP服务,我使用163邮箱,在设置->POP3/SMTP/IMAP可以开启SMTP服务。
这里写图片描述

四、注意发送邮件的主题不能为test,否则发送不出去

当你做好前三步之后,你还是不能成功发送邮件的话,看一下你在发送邮件时是否将主题设置成了test,没错!这样会发不出去的(′⌒`) 我在测试的时候一直用test做主题,一下午都没成功,摔桌! 改成别的主题一下子就成功了。
谢谢你长的这么好看,还这么有耐心的看完了(*  ̄3)(ε ̄ *)

原创粉丝点击