2016-04-24 179 views
1

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

爲什麼會發生?我不能在控制檯中使用傳輸?

謝謝!

+0

任何人都有這個問題? – user724065

+0

聽起來很奇怪,對我來說從控制檯發送郵件工作得很好。如果Yii不報告錯誤,也許Swift-mailer會這樣做。打開Swift-mailer日誌並檢查。在'class'=>'yii \ swiftmailer \ Mailer''下面添加這個''enableSwiftMailerLogging'=> true'來發送Swift-mailer錯誤消息到Yii日誌。 – karpy47

+0

我在Cpanel中驗證了主機,用戶和通行證,並且沒問題。這種設置在我發送電子郵件沒有問題的情況下工作得很好。 在日誌中我沒有任何錯誤。 如何驗證問題在哪裏? – user724065

回答

1

另一種方式來檢查,如果您的傳輸設置是正確的 你可以在CronController.php

  \Yii::$app->mail->setTransport([ 
      'class' => 'Swift_SmtpTransport', 
      'host' => 'gator.hostgator.com', 
      'username' => '[email protected]', 
      'password' => '*******', 
      'port' => '465', 
      'encryption' => 'ssl', 
      ]); 

此行之前直接設置交通

的Yii :: $ APP-> MAIL->撰寫( '@公共/郵件/ cron_acordo',[ 'model_definicoes'=> $ model_definicoes])

0

這是因爲在YII common/main.php與你在console/main.php合併配置。 重置你的配置直接發送:

'mailer' => [ 
     'useFileTransport' => false, 
     //other configs 
    ] 

見文件:yii\mail\BaseMailer.php,已經在行77-78此評論:

/** 
* @var boolean whether to save email messages as files under [[fileTransportPath]] instead of sending them 
* to the actual recipients. This is usually used during development for debugging purpose. 
* @see fileTransportPath 
*/ 
public $useFileTransport = false; 
0

你應該在console.php確保您的配置權,確保關鍵郵件已配置。 祝你好運!

0

所以,當我在尋找我遇到的問題的答案時,我遇到了這個問題。通過啓用SwiftMailer的日誌記錄,我終於找到了「發件人地址被拒絕:用戶在中繼收件人表中未知」,從而找到了我的問題的答案。實際上,我在「發件人」地址中使用的郵件在控制檯部分中與在前端部分中使用的不同,這是問題所在,它甚至不需要配置。

我只能通過查看日誌來解決這個問題,所以爲了啓用日誌記錄,在'郵件'組件下的Yii配置中(或者你調用它的任何東西)添加key =>值對' enableSwiftMailerLogging'=> true(在這個問題中的示例:Config mailer parameters from model - Yii2)。然後,在你的日誌組件的配置,根據需要添加

[ 
    'class' => 'yii\log\FileTarget', 
    'categories' => ['yii\swiftmailer\Logger::add'], 
] 

的「目標」它稍微記錄在這裏:http://www.yiiframework.com/doc-2.0/yii-swiftmailer-logger.html

通過這樣做,我可以尋找到日誌(控制檯/運行/ app.log),並找出爲什麼它不能從控制檯正確發送,但是來自我的應用的其他區域。