Yii2配置swiftmailer使用gmail smtp发送电子邮件

时间:2018-01-25 06:20:51

标签: php email yii2 xampp gmail

我正在研究yii2。在localhost上使用我的XAMP我正在尝试发送电子邮件。在发送电子邮件时,我收到POSTMAN中的轰鸣声错误。

  

“name”:“例外”,       “message”:“无法与主机smtp.gmail.com [#0]建立连接”,       “代码”:0,       “type”:“Swift_TransportException”,

贝娄是我的common/config/main-local

   'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'transport'=>[
            'class'=>'Swift_SmtpTransport',
            'host'=>'smtp.gmail.com',
            'username'=>'MY GMAIL ID',
            'password'=>'Password',
            'port'=>'465', //587 also tried this with ssl and tls
            'encryption'=>'ssl' //tls also tried with port 465,26 and 587
        ],
        '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,
    ],

以下是我的发送请求

Yii::$app->mailer->compose()
        ->setFrom('MY ID')
        ->setTo('SEND ID')
        ->setSubject('New Message')
        ->setTextBody('HI')
        ->setHtmlBody('<b>HTML content</b>')
        ->send();

我搜索了每个问题,但无法解决问题。我现在该怎么办?

任何帮助都将受到高度赞赏。

3 个答案:

答案 0 :(得分:2)

确保您先为自己的帐户启用了less secure apps

然后你应该使用port:587encryption:tls的以下设置,它会起作用,我在家里的本地计算机上使用这些设置从localhost发送电子邮件。

'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'transport'=>[
            'class'=>'Swift_SmtpTransport',
            'host'=>'smtp.gmail.com',
            'username'=>'MY GMAIL ID',
            'password'=>'Password',
            'port' => '587' ,
            'encryption' => 'tls' ,
        ],
        '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,
    ],

答案 1 :(得分:0)

穆罕默德的评论很有帮助。我的信誉不足,无法将此评论添加到他的帖子中。

启用对gmail上安全性较低的应用的访问后,请等待一段时间以使更改生效,否则,发生在我身上的“ TLS不可用”错误将继续出现。

要把所有东西放在一起,

 'mailer' => [
     'class' => 'yii\swiftmailer\Mailer',
     'transport' => [
         'class' => 'Swift_SmtpTransport',
         'host' => 'smtp.gmail.com',  
         'username' => 'xxxx@gmail.com',
         'password' => 'your password',
         'port' => '587',
        'encryption' => 'tls', 
        'streamOptions' => [ 'ssl' => [ 'allow_self_signed' => true, 'verify_peer' => false, 'verify_peer_name' => false, ], ]

     ],
    'viewPath' => '@common/mail',
 ],

答案 2 :(得分:0)

*重要* 禁用SSL验证会带来安全隐患。如果没有验证SSL / HTTPS连接的真实性,恶意攻击者可能会冒充受信任的端点(例如GitHub或其他远程Git主机),并且您将容易受到中间人攻击。使用此解决方案之前,请确保您完全了解安全问题。

  1. 确保您首先为帐户启用了less secure apps

  2. 通过other email platforms

  3. 检查Gmail
  4. 编辑\ common \ config \ main-local.php

    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        'useFileTransport' => false,
        'transport' => [
            'class'      => 'Swift_SmtpTransport',
            'host'       => 'smtp.gmail.com',
            'username'   => 'yourname@gmail.com',
            'password'   => 'yourpass',
            'port'       => '587',
            'encryption' => 'tls',
            'streamOptions' => [
                'ssl' => [
                'allow_self_signed' => true,
                'verify_peer' => false,
                'verify_peer_name' => false,
                ],
            ],
        ],
    ],