Yii extension for sending HTML emails with layouts using PHPMailer
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); }
Two examples included: one for standard contact form in yii web app and the other one for yii console app.
Total 8 comments
I can't reproduce your problem, are you sure your paths are ok? Did you get some error?
Did you try putting "header.png" image somewhere else just to see if that would work?
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!
Great ext!
Well done Vernes
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).
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!
Added support for console applications
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)
I may start using such an extension if it also supported AWS SES.
Leave a comment
Please login to leave your comment.