yiimailer

Send HTML emails with layouts
37 followers

Yii extension for sending HTML emails with layouts using PHPMailer

Features

  • 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

Installation

  1. Copy YiiMailer folder to protected/extensions
  2. Add 'ext.YiiMailer.YiiMailer' line to your imports in main and/or console yii config
  3. Copy mail.php config file to protected/config
  4. Create email layout file mail.php in protected/views/layouts/ (default path, can be changed in config)
  5. Create all the views you want to use in protected/views/mail/ (default path, can be changed in config)
  6. 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 = 'from@example.com';
$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);
}

Examples

Two examples included: one for standard contact form in yii web app and the other one for yii console app.

Github page

https://github.com/vernes/YiiMailer

Total 7 comments

#12597 report it
Philipp Stracker at 2013/03/31 09:35am
Difference to Yii Mail extension

I do not know the exact difference to the Yii Mail extension. But I love this extension! I use it, because I was able to understand and use it within 5 Minutes, while I am still not 100% clear about how to use Yii Mail (and I do not care either)

When I was looking for a mailer-solution this one was the easiest to understand. And it also works great.

Good job vernes!

#12570 report it
krasavcheg.ua at 2013/03/29 04:38am
It's AMAZING

Great ext!

Well done Vernes

#11799 report it
Vernes at 2013/02/04 03:46pm
@fleuryc

I created this extension a while ago for use in my personal projects and recently i decided to share it, if someone finds it useful that would be great.

I am an old PHPMailer user and really haven't use Yii-Mail or SwiftMailer (both are great btw).

#11789 report it
fleuryc at 2013/02/04 05:49am
Compared to Yii-mail (Swift Mailer)

Hi!

Just a question @Vernes : why did you create this ext, since the Yii Mail extension does already exist and is very popular (even if a bit old)?

What does your ext (PHP Mailer) do that Yii-Mail (Swift Mailer) doesn't (and vice-versa)?

I'm just being curious, your ext seems great (even if the configuration could be handled in a more standard way (as an application component)).

Cheers!

#11770 report it
Vernes at 2013/02/02 01:03pm
New version 1.1

Added support for console applications

#11717 report it
mshakeel at 2013/01/29 07:28am
YiiMailer using CronJob (Console Application)

Can we use this wonderful extension in our console application?

Since it renders the views and layout which can be a problem in cronjob (as discussed here Forum link)

#11277 report it
Nathan at 2013/01/03 03:34am
AWS

I may start using such an extension if it also supported AWS SES.

Leave a comment

Please to leave your comment.

Create extension