How to use the swiftMailer in Yii2
Asked 07 September, 2021
Viewed 1.9K times
  • 51
Votes

I can't finally understand how to use the swiftMailer extension in Yii2. Judging by that on this subject I didn't find questions, the task is trivial, but up to the end I couldn't understand.

There are examples which don't describe in more detail all cycle of sending the letter or I don't understand something :(

Setup

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

Send

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

I want will receive a concrete working example. Thank you.

P.S. I adjusted domain records MX, DKIM, SPF added.

UPD (some answer):


  

E-mail which is passed in "From" field, it is put down by default in the field of "Return-path", has to be the existing address. Some providers don't allow sending mail from nonexistent email addresses.

6 Answer