美文网首页
Yii2.0 发送邮件教程

Yii2.0 发送邮件教程

作者: 云过飘雨 | 来源:发表于2017-06-22 14:12 被阅读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';
           }
    

    三、如果邮件发送不成功,注意开启邮箱的SMTP服务

    四、如果邮件还是发送不成功,注意查看你的邮件主题是否设为了test,这种情况会被163邮箱屏蔽,发送不出去。这个奇怪的坑我是踩过了。

    相关文章

      网友评论

          本文标题:Yii2.0 发送邮件教程

          本文链接:https://www.haomeiwen.com/subject/txlfcxtx.html