Yii extension for sending emails with layouts using PHPMailer
- Based on latest PHPMailer (version 5.2.2 bundled)
- Supports Yii layouts and translation
- Supports web and console applications
- Send full HTML emails with embeded images
- Work with views like you usually do in Yii
- Copy YiiMailer folder to protected/extensions
- Add 'ext.YiiMailer.YiiMailer' line to your imports in main and/or console yii config
- Copy mail.php config file to protected/config
- Create email layout file mail.php in protected/views/layouts/ (default path, can be changed in config)
- Create all the views you want to use in protected/views/mail/ (default path, can be changed in config)
- Put all images you want to embed in emails in images/mail/ (default path, can be changed in config)
Instantiate YiiMailer in your controller or console command and pass view and data array:
$mail = new YiiMailer('contact', array('message' => 'Message to send', 'name' => 'John Doe', 'description' => 'Contact form'));
or
$mail = new YiiMailer(); $mail->setView('contact'); $mail->setData(array('message' => 'Message to send', 'name' => 'John Doe', 'description' => 'Contact form'));
Layout is automatically set from config but you may override it with $mail->setLayout('layoutName')
Render HTML mail and set properties:
$mail->render(); $mail->From = '[email protected]'; $mail->FromName = 'John Doe'; $mail->Subject = 'Mail subject'; $mail->AddAddress(Yii::app()->params['adminEmail']);
You may use all PHPMailer properties you would usually use.
And finally send email(s):
if ($mail->Send()) { $mail->ClearAddresses(); Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.'); } else { Yii::app()->user->setFlash('error','Error while sending email: '.$mail->ErrorInfo); }
Two examples included: one for standard contact form in yii web app and the other one for yii console app.