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.
FAQ ¶
- Why should I use this extension instead of using the phpmailer directly?
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 :)
- Why the license was GPL on the download page when the extension is released under LGPL?
Answer: my mistake. The license for this extension is and has always been LGPL.
- My view has images on it, but when it is send as e-mail, no images are shown. Why?
You must place the images in a public HTTP server, since you're using the tag in your view.
File checksums ¶
- 847183726a2e59d8aa5968e2fa6f7597 mailer-2.2.tar.bz2
- bef73d62003ab84399706c578294cccb mailer-2.2.zip
Resources ¶
Documentation ¶
Requirements ¶
- Yii 1.0 or above
- PHPMailer (version 5.0 bundled)
Installation ¶
- Extract the release file under
protected/extensions
Usage ¶
As a normal component: ¶
Usage example, using SMTP as the sending method, creating a component inside a controller:
<?php
$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();
As an application component: ¶
In the configuration (notice the pathViews and pathLayouts parameters):
<?php
'components'=>array(
'mailer' => array(
'class' => 'application.extensions.mailer.EMailer',
'pathViews' => 'application.views.email',
'pathLayouts' => 'application.views.email.layouts'
),
// ...
}
In the controller:
<?php
$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:
Change Log ¶
20090529 2.2 ¶
- Fixed bug (reported by ooaat)
20090516 2.1 ¶
- Added pathViews and pathLayouts configuration parameters, to indicate a path aliases where the view and layout for getView are.
- Added an empty init() method to make it work as an application component.
20090411 2.0 ¶
- Updated PHPMailer to version 5.0
- Included getView method.
Very easy to use
It was quick and easy to get running.
HTML parameter
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); } }
}
debug model!
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;
so nice~
thank for share!
Attachment
Expected to use this extension with attachment email
Nearly perfect...
...but I have 2 suggestions:
1.
I'd prefer to you this extension like this:
Yii::app()->mailer->Subject = 'Test'; Yii::app()->mailer->Body = 'Hello world!';
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!
Greate Extension
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)
Works fine with Phpmailer v5.0
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.
some addition
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); } }
easy and efficient extension
It's very easy to use this extension.
thanks
Thank you very much for your efforts. Very easy to use and implement.
exceptions
Hello, can you add exceptions in __construct() method? Something like this:
public function __construct($exceptions = false) { $this->_myMailer = new PHPMailer($exceptions); }
Views
Hi,
Can you post a sample getting HTML from a VIEW?
@prchakal
@prchakal
$html = $this->renderPartial('myview',array('data'=>$data),true); //OR get view from other controller $html = $this->renderPartial('/mycontroller/myview',array('data'=>$data),true); ... Yii::app()->mailer->Body = $html; ...
Usefull extension
Thanks for this extension, PHPMailer is my favorite php mail library.
thank you
I agree with others, it was very easy to get this plugin working.
don't show images into view
Hi.
I have tried your extension: It's very very nice!
I have this problem:
I follow your sample:
$html = $this->renderPartial('/mycontroller/myview',array('data'=>$data),true); ... Yii::app()->mailer->Body = $html;
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
Re:don't show images into view
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?
In the controller
Where should I put the code "in the controller"? Thank you. :)
RE:In the controller
It depends on what you are trying to achieve. For example, if you want to send an email when a client creates an order in your store you'd have to put your code in the actionCreate() of the OrderController class.
HTML Email
Just a note for anyone trying to send an html email, I had to call this method to get html to render correctly:
Yii::app()->mailer->IsHTML(true);
EMailer->getView() stopped working
After updating from PHP 5.2 to 5.3, getView() in the EMailer-Class stopped working. I got an error message in
return call_user_func_array(array($this->_myMailer, $method), $params);
telling me, that parameter 1 needs to be a valid callback and there is no function "renderPartial" in EMailer class.
After further investigation, I found that the error comes from invoking $mailer->getView(), the else-part which will be applied when the class is called without a Yii CController (e.g. from a cron job or console application). If any of you need it, here is the else part, where I only changed the $body- and the second return-statement.
else { $body = CController::renderInternal(Yii::getPathOfAlias($this->pathViews . '.' . $view) . '.php', array_merge($vars, array('content' => $this->_myMailer)), true); if ($layout === null) { return $body; } else { return CController::renderInternal(Yii::getPathOfAlias($this->pathLayouts . '.' . $layout) . '.php', array('content' => $body), true); } }
Smtp login password
Hello, is it possible to put smtp server and credentials in config/main.php ?
Set a view
@prchakal, @briiC.lv and others somebody!
You can just use this for set view:
$mailer->getView('myview');
And if have a model use this:
$mailer->getView('myview', array('model'=>$model));
Small fix
I think it would be more useful if:
Author change
protected $pathViews = 'application.views.email'; protected $pathLayouts = 'application.views.email.layouts';
to
public $pathViews = null; public $pathLayouts = null;
and fix the method "getView" as follows
public function getView($view, $vars=array(), $layout=null) { $viewFile=$view; if (!is_null($this->pathViews)) { $viewFile=$this->pathViews.'.'.$view; } $body=Yii::app()->controller->renderPartial($viewFile, array_merge($vars, array('content'=>$this->_myMailer)), true); /* the rest of the program code */ }
That fix allow you to set $pathViews and $pathLayouts attributes from the component config
'components'=>array( 'mailer' => array( 'class'=>'application.extensions.mailer.EMailer', 'pathLayouts'=>'application.views.mail.layouts', 'pathViews'=>'application.views.mail.view', ), ....
and pass the arbitrary view name to the method "getView"
Yii::app()->mailer->getView('application.modules.feedback.views.mail.admin_email_notification')
Callbacks
PHPMailer 5.2.2 has new feature called "Callbacks".
The documentation says:
NEW CALLBACK FUNCTION:
We have had requests for a method to process the results of sending emails
through PHPMailer. In this new release, we have implemented a callback
function that passes the results of each email sent (to, cc, and/or bcc).
We have provided an example that echos the results back to the screen. The
callback function can be used for any purpose. With minor modifications, the
callback function can be used to create CSV logs, post results to databases, etc.
Well, it is easy in PHPMailer but I have no idea how to use it within Yii framework with MetaYii extension wrapper. Anybody can advise, please?
UPDATE:
The solution is to place the callback function in an application helper component and then pass the result to controller as application parameter.
I hope this might be helpful.
Works
Awesome used the wrapper very nice, for those who want to test smtp on Windows use this awesome dummy smtp application SMTP Tool
Hide Header
Hi,
is there any way to hide the X-Originating-IP in header for security reason??
Be carefull
I have had a really serious privacy issue (it is my fault) but it would be fine if you add to this brief some reminders.
If you use Emailer extension as a component and you use it in that way:
Yii::app()->mailer->AddAdress('someaddress@somedomain.com'); Yii::app()->mailer->Send();
REMEMBER! it is not a fresh object, so if you loop to send the same mail to several addresses you will (maybe) need to:
Otherwise you will generate emails with "visible" addresses in the "To" field. I didn't realize about that because I was in a hurry and it was problematic.
Thanks, the extension is really useful.
i need know how i get error and mail server
I have two Questions :
thanks
Reliance on controller
Right now component relies on current controller. Yii::app()->controller.
This is not very good, because in console commands there is no controller and I would still like to send emails.
Please, fix this.
@undsoft
You can fix this yourself by checking if Yii::app()->controller is set, and if not, you just make a fake Controller:
$controller = new CController('ThisDoesNotMatter');
You can then use $controller->renderInternal instead of $controller->renderPartial.
I keep getting the error that SMTP is already defined
I get the error
Cannot redeclare class SMTP in <b>/Developer/php/Mariana/protected/extensions/mailer/phpmailer/class.smtp.php</b> on line <b>49</b>
The code that i'm using is
Yii::app()->mailer->Host = $smtp->host; Yii::app()->mailer->IsSMTP(); Yii::app()->mailer->SMTPAuth=true; Yii::app()->mailer->From = $_POST["email"]; Yii::app()->mailer->Port = $smtp->port; Yii::app()->mailer->Username = $smtp->email; Yii::app()->mailer->Password = $smtp->password; Yii::app()->mailer->FromName = $_POST["name"]; Yii::app()->mailer->AddReplyTo($_POST["email"]); Yii::app()->mailer->AddAddress($smtp->email); Yii::app()->mailer->Subject = utf8_decode($_POST["subject"]); Yii::app()->mailer->Body = utf8_decode($_POST["text"]); echo Yii::app()->mailer->Send();
Config
Nice and clean extension. And I like PHPmailer anyway. Only wish I could store some defaults in the config. My From, Fromname, isSMTP, Host, AddReplyTo etc. is always the same and I have to repeat this over and over again. Any thoughts on this?
Default Config
All you have to do is give the EMailer class a "public $defaultConfig = array()" or so. Instead of $this->_myMailer = new PHPMailer() in the constructor, just let Yii do the work:
$config = $this->defaultConfig; $config['class'] = 'PHPMailer'; // You might want to add the aliased path if needed $this->_myMailer = Yii::createComponent($config);
And you have your default configuration.
I actually have the default-config array as the "mailer" in my main configuration and every time I send out a mail I just do $mail = Yii::app()->mailer, set body and subject and send. Then a simple Yii::app()->setComponent('mailer', NULL); is enough to reset it for the next run. I don't need any previews so I just work with PHPMailer directly. No need for the extra layer.
Cannot override default property values
If you are having trouble overriding the default property values by setting values in your main config, it's because this extension is using a constructor instead of using the init() method.
If you're having trouble, simply move everything within __construct() into init(), and delete the constructor. You'll also need to make $pathViews and $pathLayouts public.
I also modified my copy by adding a $libPath property so that I can specify my own path to PHPMailer. In my case, I downloaded the latest copy from Github and placed in the vendors directory. I then added the following to init() just before instantiating a new PHPMailer object:
Yii::import("{$this->libPath}.*"); require_once('class.phpmailer.php');
I hope this helps others who run into similar issues.
protected must change to public
About these:
protected $pathViews = 'application.views.email'; protected $pathLayouts = 'application.views.email.layouts';
I thinks must be "public" var for set custom values.
plz support from view in theme folder
this extension don't support from view in theme folder by default.
How to add CC
I tried without CC its works fine for me:
$adminMail = Yii::app()->mailer;
$adminMail->ClearAllRecipients();
$adminMail->ClearAttachments();
$adminMail->ClearCustomHeaders();
$adminMail->From = "usename@mail.com";
$adminMail->FromName = UserName;
$adminMail->AddAddress("mail@mail.com");
$adminMail->Subject = 'Request for call back from Site';
$adminMail->IsHTML(true);
$adminMail->Body = $html;
$adminMail->Send();
If i add this $adminMail->AddCC("somemail@mail.com");
the mail is not working.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.