EMailer is a simple wrapper for the PHPMailer library, which is bundled. You should keep an eye on this library, since it could be updated and the API could get broken.
Please read the source code comments for licensing information. Please read the phpmailer/LICENCE file for the PHPMailer license.
Answer: this is encapsulated as a Yii extension, and you can modify the methods or extend the functionality as you'd like, instead of modifying the original source code :)
Answer: my mistake. The license for this extension is and has always been LGPL.
protected/extensionsUsage example, using SMTP as the sending method, creating a component inside a controller:
$message = 'Hello World!'; $mailer = Yii::createComponent('application.extensions.mailer.EMailer'); $mailer->Host = <your smtp host>; $mailer->IsSMTP(); $mailer->From = 'wei@example.com'; $mailer->AddReplyTo('wei@example.com'); $mailer->AddAddress('qiang@example.com'); $mailer->FromName = 'Wei Yard'; $mailer->CharSet = 'UTF-8'; $mailer->Subject = Yii::t('demo', 'Yii rulez!'); $mailer->Body = $message; $mailer->Send();
In the configuration (notice the pathViews and pathLayouts parameters):
'components'=>array( 'mailer' => array( 'class' => 'application.extensions.mailer.EMailer', 'pathViews' => 'application.views.email', 'pathLayouts' => 'application.views.email.layouts' ), // ... }
In the controller:
$message = 'Hello World!'; Yii::app()->mailer->Host = 'smtp.yiiframework.com'; Yii::app()->mailer->IsSMTP(); Yii::app()->mailer->From = 'wei@pradosoft.com'; Yii::app()->mailer->FromName = 'Wei'; Yii::app()->mailer->AddReplyTo('wei@pradosoft.com'); Yii::app()->mailer->AddAddress('qian@yiiframework.com'); Yii::app()->mailer->Subject = 'Yii rulez!'; Yii::app()->mailer->Body = $message; Yii::app()->mailer->Send();
Since this is a wrapper, please see:
Total 20 comments
I had the same problem. In order to put images in your email (not as an attachment) you have to provide an absolute and public url to the image. Can you post your view code?
Hi. I have tried your extension: It's very very nice!
I have this problem: I follow your sample:
my view include some picture, but the email don't show those images.
I see into html: http://prova/demo/images/splash.jpg but the exact path should be: http://www.mysite.com/prova/demo/images/splash.jpg
Can you help me? Thank you
5934 Check http://phpmailer.worxware.com/index.php?pg=properties $mailer->Username and $mailer->Password should work fine, they work for me.
I have some question, about how to define user and password if my host use those.
I agree with others, it was very easy to get this plugin working.
Thanks for this extension, PHPMailer is my favorite php mail library.
@prchakal
Hi,
Can you post a sample getting HTML from a VIEW?
Hello, can you add exceptions in __construct() method? Something like this:
why should i use this extension and not just phpmailer by itself in the components folder?
Thank you very much for your efforts. Very easy to use and implement.
It was quick and easy to get running.
Nice work. I'm not too fond of phpmailer in general because of how it's using both variables and methods to set its state, but it's light weight so I'll get over it.
Most of my emails are html formatted so I added an html argument to the method signature. Let me know if this could be done in a simpler way:
public function getView($view, $vars = array(), $layout = null, $html=true) { $body = Yii::app()->controller->renderPartial($this->pathViews.'.'.$view, array_merge($vars, array('content'=>$this->_myMailer)), true); if ($layout === null) { if($html){ $this->_myMailer->MsgHtml($body); }else{ $this->_myMailer->Body = $body; } } else { if($html){ $this->_myMailer->MsgHtml(Yii::app()->controller->renderPartial($this->pathLayouts.'.'.$layout, array('content'=>$body), true)); }else{ $this->_myMailer->Body = Yii::app()->controller->renderPartial($this->pathLayouts.'.'.$layout, array('content'=>$body), true); } } }
maybe, wo must modify some code to hiden some exception don't expect; in class.phpmailer.php file about 522st line, i change to: if($this->SMTPDebug) { echo $e->getMessage()."\n"; } so , we can decide the exception based on property SMTPDebug;
thank for share!
Expected to use this extension with attachment email
...but I have 2 suggestions:
1.
I'd prefer to you this extension like this:
to do this, I've changed the __construct() method to init(), and the added it under components in the "protected/config/main.php" like this:
'components' => array( 'mailer' => array( 'class' => 'application.extensions.mailer.EMailer' ) )2.
It would be great, if it's possible to configure the path of the view and layout files, instead of setting this hardcoded in the getView() method. so the whole thing looks like this:
'components' => array( 'mailer' => array( 'class' => 'application.extensions.mailer.EMailer', 'pathViews' => 'application.views.emails', 'pathLayouts' => 'applications.views.emails.layouts' ) )I've already made these changes, so if anybody is interested let me know, and I'll send you my version of EMailer.php.
Anyway, good job!
Very easy to use, just follow the documentation and the examples included in the PHPMailer package (http://sourceforge.net/project/showfiles.php?group_id=26031&package_id=252700)
I was about to write a Phpmailer wrapper and found someone already built it up. Just tested it with the latest PHPMailer V5.0, works well( both mail and smtp ), the GetView() addition function by kvl is a must have as well, they work like a charm. Recommend to everyone who need an feature rich email function in his/her app.
Very nice and very useful! In order to get a good view usability, I've added the method GetView() to the Emailer class (borrowed from "emailer" extension - see details here) - and all have become just perfect!
public function GetView($view, $vars = array(), $layout = null) { $body = Yii::app()->controller->renderPartial('application.views.email.'.$view, array_merge($vars, array('email'=>$this->myMailer)), true); if ($layout === null) { $this->myMailer->Body = $body; } else { $this->myMailer->Body = Yii::app()->controller->renderPartial('application.views.email.layouts.'.$layout, array('content'=>$body), true); } }Leave a comment
Please login to leave your comment.