使用yii2的Switfmailer的静态配置不会发送邮件

时间:2018-11-17 15:36:59

标签: configuration yii2-advanced-app swiftmailer

使用以下静态配置可能会导致此类异常。

Swift_TransportException: Failed to authenticate on SMTP server with username "thomas.kipp@eisvogel-online-software.de" using 3 possible authenticators in

使用邮件客户端可以毫无问题地发送邮件,因此以下参数基本上是正确的

'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            'useFileTransport' => false, //set this property to true to send mails to mail emulator. Set this property to false to send real mails.              
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.strato.de',
                'username' => 'thomas.kipp@eisvogel-online-software.de',
                'password' => 'top secret',
                'port' => '587',
                'encryption' => 'tls',
            ],
        ],

此配置有什么问题?配置如下:

     'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            'useFileTransport' => false, //set this property to true to send mails to mail emulator. Set this property to false to send real mails.              
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.gmail.com',
                'username' => 'tklustig.thomas@gmail.com',
                'password' => 'top secret',
                'port' => '587',
                'encryption' => 'tls',
            ],
        ],

会抛出这样的错误:

Swift_TransportException: Connection could not be established with host smtp.gmail.com [Connection timed out #145] in

与gmx相同。我再也无法使用Swiftmailer发送邮件了。我该怎么办?

1 个答案:

答案 0 :(得分:0)

我认为Google的smtp在不同的端口和加密类型上工作。

'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        'useFileTransport' => false,             
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => 'username',
            'password' => 'password',
            'port' => '465',
            'encryption' => 'ssl',
        ],
    ],

我认为这应该可以解决您的问题。

顺便说一句。我无法弄清楚为什么您的tls连接不起作用。尝试使用ssl。