使用swiimailer和yii2时遇到问题。我在dev机器上编写代码,它在windows中运行,运行顺畅。但是,当我将相同的代码移动到生产站点时,swiftmailer不会发送电子邮件,但会将电子邮件保存到文件中。我从控制台发送邮件
composer.json
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": "dev-master",
"yiisoft/yii2-bootstrap": "*",
"yiisoft/yii2-swiftmailer": "~2.0.0",
main.php
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'xxx',
'password' => 'xxx',
'port' => '587',
'encryption' => 'tls',
'streamOptions' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
]
],
'useFileTransport' => false,
],
sendEmail.php
$res = \Yii::$app->mailer->compose()
->setFrom("stat@test.net")
->setTo(self::getRecipients())
->setHtmlBody($data)->setSubject("Stat Report ".date("d.m.Y H:i"))
->send();
应该出错。任何日志中都没有写入错误。
答案 0 :(得分:0)
try-catch
确认是否没有错误:
try {
$res = \Yii::$app->mailer->compose()
->setFrom("stat@test.net")
->setTo(self::getRecipients())
->setHtmlBody($data)->setSubject("Stat Report ".date("d.m.Y H:i"))
->send()
}
catch (\Swift_TransportException $e) {
var_dump($e->getMessage());
}
或者尝试Yii-swiftmalier的记录器:
[
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'enableSwiftMailerLogging' => true,
],
],
// ...
],
答案 1 :(得分:0)
尝试不使用streamOptions
,例如:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'xxx',
'password' => 'xxx',
'port' => '587',
'encryption' => 'tls',
],
],