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

Static Configuration for Switfmailer using yii2 won't send mails

使用下面的静态配置会导致这样的异常。

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 发送邮件了。我能做什么?

我认为 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.