Yii2 SwiftMailer不通过默认mail()函数发送电子邮件

时间:2016-03-23 03:03:23

标签: php yii2 swiftmailer yii2-advanced-app

以下是我的配置:

'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            'transport' => [
                'class' => 'Swift_MailTransport',
            ],
            'useFileTransport' => False,
        ],

我甚至试过以下事项:

'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            'useFileTransport' => False,
        ],

我使用以下代码发送电子邮件:

var_dump(\Yii::$app->mailer->compose()
                    ->setTo($email)
                    ->setFrom(\Yii::$app->params['supportEmail'])
                    ->setSubject('Message subject')
                    ->setTextBody('Plain text content')
                    ->setHtmlBody('<b>HTML content</b>')
                    ->send());

但是每次抛出都会返回false。

我使用mail()函数发送相同内容的电子邮件,其工作正常,电子邮件是即时的。但问题只出在SwiftMailer上。

我无法找到调试它的方法。我启用了Yii2Debugger,但我也无法解决任何信息。

修改

我尝试使用Gmail SMTP,我正在

Connection could not be established with host smtp.gmail.com [Connection timed out #110]

我尝试了他们的默认SMTP,我没有收到任何错误,但仍然是假的。

  

我们正在分享托管环境。我需要检查任何PHP设置。

1 个答案:

答案 0 :(得分:-1)

尝试此配置

'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'useFileTransport'=>false,
            // 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.

            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.gmail.com',
                'username' => 'yourmail@gmail.com',
                'password' => 'yourpassword',
                'port' => '465',
                'encryption' => 'ssl',
            ],
        ],

希望它有所帮助:)