user724065
user724065

Reputation: 67

yii2 - Send email by console use transport

I want send email by console with Swift_SmtpTransport.

The same transport settings work in common/config/main-local.php and don't work in console/config/main-local.php.

In console in config/main-local.php I have:

<?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',
            ],
        ],
    ],    
];

With this configuration (and in common the settings are the same and work) I load the script by command and no email send and no error.

With this (I delete the transport settings) I run the same script by command and the email send ok:

<?php
return [
    'components' => [        
        'mail' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            'htmlLayout' => '@common/mail/layouts/html',
            'textLayout' => '@common/mail/layouts/text',  // custome layout        
        ],
    ],    
];

In console/controllers/CronController I have this:

<?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();        
    }
}

Why this happen? I can´t use transport in console?

Thanks!

Upvotes: 1

Views: 4409

Answers (4)

BVBAccelerate
BVBAccelerate

Reputation: 182

So, I came across this when I was looking for an answer to a problem I had. I was able to find the answer to my problem by enabling the logging for the SwiftMailer which ended up finding "Sender address rejected: User unknown in relay recipient table." Essentially, the mail that I was using in the "from" address was different in the console portion than it was from the frontend portion, and that was the issue, it didn't even have to do with the config.

I was only able to figure this out by looking at the logging, so to enable logging, in your Yii configuration under the 'mail' component (or whatever you're calling it) add the key=>value pair 'enableSwiftMailerLogging' => true (example of where is in this question: Config mailer parameters from model - Yii2). Then, in your log component config, under the 'targets' you need to add

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

It's slightly documented here: http://www.yiiframework.com/doc-2.0/yii-swiftmailer-logger.html

By doing this I was able to look into the logs (console/runtime/app.log for me) and find out why it wasn't sending out correctly from the console but was from the other areas of my app.

Upvotes: 0

chenqionghe
chenqionghe

Reputation: 1

You should make sure your configurations in console.php are right, make sure the key mailer is configured. Good luck !

Upvotes: 0

ThangTD
ThangTD

Reputation: 1684

This is because yii merge config in common/main.php with yours at console/main.php. Reset your config to send directly:

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

See file: yii\mail\BaseMailer.php, have this comment at line 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;

Upvotes: 0

vasillis
vasillis

Reputation: 342

another way to check if your transport settings are correct you could set the transport directly in CronController.php

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

before this line

Yii::$app->mail->compose('@common/mail/cron_acordo', ['model_definicoes' => $model_definicoes])

Upvotes: 4

Related Questions