2016-04-24 180 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),并找出为什么它不能从控制台正确发送,但是来自我的应用的其他区域。