F
F
Fyodor Dostoyevsky2014-07-28 15:39:28
Yii
Fyodor Dostoyevsky, 2014-07-28 15:39:28

How to send emails via swiftMailer in Yii2?

Hello. I can't finally figure out how to use the swiftMailer extension in Yii2 after all . Judging by the fact that I did not find any questions on this topic, the task is trivial, but I could not figure it out to the end.
There are examples that do not describe in more detail the entire cycle of sending a letter, or I don’t understand something
:(

return [
    //....
   'components' => [
    ......
    'mail' => [
      'class' => 'yii\swiftmailer\Mailer',
      'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'localhost',
        'username' => 'username',
        'password' => 'password',
        'port' => '587',
        'encryption' => 'tls',
      ],
    ],
  ]
];

Sending
Yii::$app->mail->compose()
    ->setTo($toEmail)
    ->setFrom([$this->email => $this->name])
    ->setSubject($this->subject)
    ->setTextBody($this->body)
    ->send();

From all this it is not clear how, for example, I can connect to the mail on biz.mail.ru. There is no mail server on hosting.
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'port' => '587', here host : smtp.mail.ru,
username : [email protected] .ru, password : password of [email protected] Tell me what I'm doing wrong. PS Domain records MX set up, DKIM, SPF added. UPD1: I can't send a letter in the specified way. The page takes a long time to load, and then falls off due to a timeout: screenshot.ru/upload/images/2014/07/28/mama_bus_de...


Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
matperez, 2015-03-05
@matperez

The answer is of course outdated, but many may be useful. This is how you can get a log of the entire process of sending a message, and there it becomes clear what the problem is.

$mailer = Yii::$app->get('mailer');
$message = $mailer->compose() ... ;
$logger = new Swift_Plugins_Loggers_ArrayLogger();
$mailer->getSwiftMailer()->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));
if (!$message->send()) {
       echo $logger->dump();
}

D
Dmitry Entelis, 2014-07-28
@DmitriyEntelis

'host' => 'smtp.mail.ru',
'username' => '[email protected]',
'password' => 'password of [email protected]',
'port' => '587',
obviously
www.yiiframework.com/wiki/656/how-to-send-emails-u...

A
Alexander, 2015-08-07
@p0vidl0

Regarding smtp.mail.ru. Port 465 - SSL, 587 - TLS.
Plus the address ->setFrom([ $this->email => $this->name]) must match 'username' => ' [email protected] '.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question