40

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' => 'yii\swiftmailer\Mailer',
      '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.

frops
  • 2,196
  • 4
  • 29
  • 42

6 Answers6

50

Make sure you have initialised your application in production environment to send emails from your application,else it will be written in to the mailoutput folder.Or manually edit the config file like follows.

In the components's section of your common/main-local.php

'mail' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@backend/mail',
        'useFileTransport' => false,//set this property to false to send mails to real email addresses
        //comment the following array to send mail using php's mail function
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => 'username@gmail.com',
            'password' => 'password',
            'port' => '587',
            'encryption' => 'tls',
            ],
    ],

In your Controller

    \Yii::$app->mail->compose('your_view', ['params' => $params])
    ->setFrom([\Yii::$app->params['supportEmail'] => 'Test Mail'])
    ->setTo('to_email@xx.com')
    ->setSubject('This is a test mail ' )
    ->send();

This should work! Hope this will help you!

Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
Dency G B
  • 8,096
  • 9
  • 47
  • 78
  • Thank you! I already found the answer. It appears, some providers receive only existing email in the field of "return path" – frops Jul 30 '14 at 07:36
  • Looking at the example for password reset, should it be `mailer`, not `mail` yourApp->frontend->models->PasswordResetRequestForm.php – johnsnails Nov 29 '14 at 22:42
  • Hi Dency G B, I am new to yii2 and framework. Can you kindly explain where shoudl I enter the code suggested for controller. should I place it under a new controller/action and how to use that or place it under create or update. Thanks. – Joshi Jan 09 '15 at 21:20
9

You need not using SMTP transport with swiftmailer, only remove 'useFileTransport' => true in the config file (app/config/web.php in basic template) and the mails will flow.

Take a look in the docs:

http://www.yiiframework.com/doc-2.0/ext-swiftmailer-index.html

Sohel Ahmed Mesaniya
  • 3,344
  • 1
  • 23
  • 29
uselma
  • 91
  • 1
  • 2
5

Warning: This option no longer available, as Mandrill was bought by Mailchimp

Sometimes could be issues with using SwiftMailer not dependent from you. Like when I used mail.ru e-mail server. I found solution in laravel community and implemend in Yii2.

You can use alternative service like https://mandrillapp.com/ (12k email per month, 250 within hour is free) and setting up like below:

laravel community / setup mail with mandrill

'host' => 'smtp.mandrillapp.com',
'username' => 'user@domain.name',
'password' => 'oDLKswXZIkry8634f1jCDg', // new generated API key by mandrill
'port' => '587',
'encryption' => 'tls',

If you are using gmail email you can also can face with security issue. You can swith off security by allowing application use your gmail account.

If you signed in with google use links below:

https://www.google.com/settings/security/lesssecureapps

Hope it will help somebody

sambua
  • 2,274
  • 3
  • 22
  • 20
4

If you're using the basic template, then you would need to add

'viewPath' => '@app/mail',

to the config

Martin
  • 949
  • 1
  • 10
  • 15
1

Actually, you have to use config key mailer instead of mail.

'components' => [
...
    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'localhost',
            'username' => 'username',
            'password' => 'password',
            'port' => '587',
            'encryption' => 'tls',
        ],
    ],
...
],
Naeem Ali
  • 322
  • 1
  • 4
  • 22
MichalB
  • 11
  • 1
1

Google gmail security option

https://myaccount.google.com/lesssecureapps

Project file path

config\web.php
'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    'useFileTransport' => false,
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.gmail.com',  
        'username' => 'email_address@gmail.com',
        'password' => 'email_password',
        'port' => '465',
        'encryption' => 'ssl',
        'streamOptions' => [ 
            'ssl' => [ 
            'allow_self_signed' => true,
            'verify_peer' => false,
            'verify_peer_name' => false,
            ],
        ]
    ]
],

Add function inside your controller

public function actionSend() {
    $send = Yii::$app->mailer->compose()
    ->setFrom('from_mail@gmail.com')
    ->setTo('to_mail@gmail.com')
    ->setSubject('Test Message')
    ->setTextBody('Plain text content. YII2 Application')
    ->setHtmlBody('<b>HTML content <i>Ram Pukar</i></b>')
    ->send();
    if($send){
        echo "Send";
    }
}
Ram Pukar
  • 1,583
  • 15
  • 17