V
V
Vasily2017-11-01 10:21:38
Yii
Vasily, 2017-11-01 10:21:38

How to make swiftmailer work from console controller in Yii2?

Applications are created on the portal. When an application is created, it-administrators are sent emails. At first there was an option when the mailing function was performed right in the action when creating an application:

Yii::$app->mailer->compose('newtaskcreated', [   //шаблон письма
            'model' => $this
        ])  ->setTo($mass_email)                // массив адресов
            ->setSubject("Портал Yii2 - Новая заявка, №".$this->id)
            ->send();

In this version, the function works for a long time, for each new email address, approximately 1 second of the mailing is added. For 3 addresses - 3 seconds delay for creating a new order. There was a need to remove the mailing of letters in the background. I wanted to call the AJAX distribution function, but this option did not fit, because there are requests that are created by the server.
I did not understand the command bus, and it was decided to make the distribution cron. When an application is created, it is added to the mailing queue, cron scans the queue every 10 minutes and makes a mailing. So, in the console controller, the same mailing function from the web application does not work! Gives an error message:
Exception 'Swift_TransportException' with message 'Expected response code 220 but got code "", with message ""'

in /var/www/it2/basic_yii/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php:419

That is, it works for a web application, but not in the console. Where to dig?
Settings in the web file (basic application):
'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'useFileTransport' => false,
            'viewPath' => '@app/mail',
            'htmlLayout' => 'layouts/html',
            'messageConfig' => [
                'charset' => 'UTF-8',
                'from' => ['[email protected]' => 'XXXXXX'],
            ],
            // данные для YANDEX sftp
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.yandex.ru',
                'username' => '[email protected]',
                'password' => 'XXXXXX',
                'port' => '465',
                'encryption' => 'ssl',
            ],
        ],

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-11-01
@parazitl2

most likely in your console application configuration (in the config/console.php file) the mailer component is not defined or not configured correctly

A
alex stephen, 2017-11-01
@berezuev

If the server is running on nginx, then you can quickly send from the browser

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question