Send e-mail from YII2 using PHP mail

To enable sending mail from your YII2 application using PHP mail function, make the following adjustment in the YII2 config file.

Find the following:

  1. 'mailer' => [
  2.             'class' => 'yii\swiftmailer\Mailer',
  3.             'viewPath' => '@common/mail',
  4.             // send all mails to a file by default. You have to set
  5.             // 'useFileTransport' to false and configure a transport
  6.             // for the mailer to send real emails.
  7.             'useFileTransport' => true,
  8.         ],

This will be under the components parameters in the config. If using advanced YII2 instance, the file will be in common/main-local.php by default.

Change the code to this:

  1. return [
  2.     'components' => [
  3.         ...
  4.         'mailer' => [
  5.             'class' => 'yii\swiftmailer\Mailer',
  6.             'viewPath' => '@common/mail',
  7.             'transport' => [
  8.                 'class' => 'Swift_MailTransport',
  9.             ],
  10.             'useFileTransport' => false,
  11.         ],
  12.  
  13.     ],
  14. ];

useFileTransport must be changed to false and transport is set to use Swift_MailTransport.

See the following site for info on swiftmailer transport types.

http://swiftmailer.org/docs/sending.html#using-the-mail-transport

Published by

Joel Bowers

Web developer since 1999. PHP, YII2, Laravel, Javascript, HTML, CSS, jQuery, Perl, Wordpress, MySQL.

Leave a Reply

Your email address will not be published. Required fields are marked *