我想通过控制台使用Swift_SmtpTransport发送电子邮件。yii2 - 通过控制台使用传输发送电子邮件
相同的传输设置在common/config/main-local.php中工作,在console/config/main-local.php中不起作用。
在配置/主local.php控制台我:
<?php
return [
'components' => [
'mail' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
'htmlLayout' => '@common/mail/layouts/html',
'textLayout' => '@common/mail/layouts/text', // custome layout
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'gator.hostgator.com',
'username' => '[email protected]',
'password' => '*******',
'port' => '465',
'encryption' => 'ssl',
],
],
],
];
利用这种配置(和普通的设置是相同的工作)我用命令加载脚本并没有电子邮件发送和没有错误。
有了这个(我删除传输设置),我用命令运行相同的脚本和邮件发送OK:
<?php
return [
'components' => [
'mail' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
'htmlLayout' => '@common/mail/layouts/html',
'textLayout' => '@common/mail/layouts/text', // custome layout
],
],
];
在控制台/控制器/ CronController我有这样的:
<?php
namespace console\controllers;
use yii\console\Controller;
use backend\models\Definicoes;
use common\models\Acordo;
use Yii;
/**
* Cron controller
*/
class CronController extends Controller {
public function actionIndex() {
$data_hoje = date('Y-m-d');
$model_definicoes = Definicoes::find()->one();
Yii::$app->mail->compose('@common/mail/cron_acordo', ['model_definicoes' => $model_definicoes])
->setFrom([Yii::$app->params['adminEmail'] => Yii::$app->params['nome']])
->setSubject('Alert')
->setTo('[email protected]')
->send();
}
}
为什么会发生?我不能在控制台中使用传输?
谢谢!
任何人都有这个问题? – user724065
听起来很奇怪,对我来说从控制台发送邮件工作得很好。如果Yii不报告错误,也许Swift-mailer会这样做。打开Swift-mailer日志并检查。在'class'=>'yii \ swiftmailer \ Mailer''下面添加这个''enableSwiftMailerLogging'=> true'来发送Swift-mailer错误消息到Yii日志。 – karpy47
我在Cpanel中验证了主机,用户和通行证,并且没问题。这种设置在我发送电子邮件没有问题的情况下工作得很好。 在日志中我没有任何错误。 如何验证问题在哪里? – user724065