我想通過控制檯使用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