[EXTENSION] Mailer using PHPMailer
#2
Posted 06 February 2009 - 01:49 PM
$email = Yii::createComponent('application.extensions.mailer.EMailer');
$email->From = Yii::app()->params['register']['fromEmail'];
$email->FromName = Yii::app()->params['register']['fromName'];
$email->AddReplyTo(Yii::app()->params['register']['fromEmail']);
$email->AddAddress($user->email);
$email->CharSet = 'UTF-8';
$email->Subject = Yii::app()->params['register']['subject'];
$email->MsgHTML($this->renderPartial('email/register', array('user' => $user), true));
$email->Send();
Here is a view I use:
Hi, <?php echo $user->fullName;?><br />
<br />
You were successfully registered on the EveryStyle.co.uk with:<br />
Username <?php echo $user->username;?><br />
Password <?php echo $user->password_repeat;?><br />
<br />
Please follow the link below to confirm you email:<br />
<a href="http://site.com/activate.html?id=<?php echo $user->userID;?>&activationKey=<?php echo $user->activationKey; ?>">http://site.com/activate.html?id=<?php echo $user->userID;?>&activationKey=<?php echo $user->activationKey; ?></a><br />
Message is OK in Microsoft Outlook or Google mail, but my client is using OSx and there is a mailer (called "mail" or something like this), that shows the following:
--b1_4a941e1cb54df417c1a9bb7af6fe27d6
Content-Type: text/plain; charset = "UTF-8"
Content-Transfer-Encoding: 8bit
Hi, bk
You were successfully registered on the EveryStyle.co.uk with:
Username bk
Password bk
Please follow the link below to confirm you email:
http://site.com/acti...021087516536241
-- EveryStyle team
--b1_4a941e1cb54df417c1a9bb7af6fe27d6
Content-Type: text/html; charset = "UTF-8"
Content-Transfer-Encoding: 8bit
Hi, bk<br />
<br />
You were successfully registered on the EveryStyle.co.uk with:<br />
Username bk<br />
Password bk<br />
<br />
Please follow the link below to confirm you email:<br />
<a href="http://site.com/activate.html?id=10&activationKey=210021087516536241">http://site.com/activate.html?id=10&activationKey=210021087516536241</a><br />
As I can see, PHPMailer simply added text version also and this may be causing a problem. However, when I send him messages using either Google or Microsoft Outlook everything is OK, he receives attachments without any problems etc.
Any ideas why is this happening?
#3
Posted 11 April 2009 - 11:50 AM
#4
Posted 16 May 2009 - 10:51 AM
bitmatix> Implemented your suggestion on pathViews and pathLayouts.
#5
Posted 21 May 2009 - 03:58 PM
lets say i have a views/email/verifyEmail.php
thanks
#7
Posted 22 May 2009 - 10:54 AM
$email = Yii::createComponent('application.extensions.mailer.EMailer');
$email->IsSMTP();
$email->IsHTML();
//$email->SMTPDebug = true;
$email->Host = 'smtp.myhost.com';
$email->From = 'test@myhost.com';
$email->AddReplyTo('do-not-reply@myhost.com');
$email->AddAddress('user@myhost.com');
$email->CharSet = 'UTF-8';
$email->Subject = 'Test';
// body of email to contain protected/views/email/verify
$email->MsgHTML($this->render('verify', array(
'account'=> $account,
'contact_info'=> $contact_info,)
, true
));
$email->Send();
I have a file called verify located at
protected/views/email/verify.php
However I keep getting error;
CException
Description
AccountController cannot find the requested view "verify".
Source File
C:yiiframeworkwebCController.php(683)
00671: {
00672: if(($viewFile=$this->getViewFile($view))!==false)
00673: {
00674: $output=$this->renderFile($viewFile,$data,true);
00675: if($processOutput)
00676: $output=$this->processOutput($output);
00677: if($return)
00678: return $output;
00679: else
00680: echo $output;
00681: }
00682: else
00683: throw new CException(Yii::t('yii','{controller} cannot find the requested view "{view}".',
00684: array('{controller}'=>get_class($this), '{view}'=>$view)));
00685: }
00686:
00687: /**
00688: * Renders dynamic content returned by the specified callback.
00689: * This method is used together with {@link COutputCache}. Dynamic contents
00690: * will always show as their latest state even if the content surrounding them is being cached.
00691: * This is especially useful when caching pages that are mostly static but contain some small
00692: * dynamic regions, such as username or current time.
00693: * We can use this method to render these dynamic regions to ensure they are always up-to-date.
00694: *
00695: * The first parameter to this method should be a valid PHP callback, while the rest parameters
#8
Posted 22 May 2009 - 03:26 PM
$email->MsgHTML($this->render('email/verify', array...
#10
Posted 22 May 2009 - 03:50 PM
$mailer->GetView('verify', array(
'account'=> $account,
'contact_info'=> $contact_info,)
, 'main'
);
#11
Posted 22 May 2009 - 04:00 PM
$email->GetView('verify', array(
'account'=> $account,
'contact_info'=> $contact_info,)
, true
);
ended up with this error;
CException
Description
AccountController cannot find the requested view "application.views.email.layouts.1".
Source File
C:yiiframeworkwebCController.php(683)
00671: {
00672: if(($viewFile=$this->getViewFile($view))!==false)
00673: {
00674: $output=$this->renderFile($viewFile,$data,true);
00675: if($processOutput)
00676: $output=$this->processOutput($output);
00677: if($return)
00678: return $output;
00679: else
00680: echo $output;
00681: }
00682: else
00683: throw new CException(Yii::t('yii','{controller} cannot find the requested view "{view}".',
00684: array('{controller}'=>get_class($this), '{view}'=>$view)));
00685: }
00686:
00687: /**
00688: * Renders dynamic content returned by the specified callback.
00689: * This method is used together with {@link COutputCache}. Dynamic contents
00690: * will always show as their latest state even if the content surrounding them is being cached.
00691: * This is especially useful when caching pages that are mostly static but contain some small
00692: * dynamic regions, such as username or current time.
00693: * We can use this method to render these dynamic regions to ensure they are always up-to-date.
00694: *
00695: * The first parameter to this method should be a valid PHP callback, while the rest parameters
Stack Trace
#0 protectedextensionsmailerEMailer.php(216): CController->renderPartial('application.vie...', Array, true)
#1 protectedcontrollersAccountController.php(144): EMailer->getView('verify', Array, true)
#12
Posted 22 May 2009 - 04:11 PM
The last param of GetView() method should be the layout you want to use, should be a string, e.g. 'main'
#13
Posted 22 May 2009 - 04:16 PM
I finally got it too work by;
$email->GetView('verify', array(
'account'=> $account,
'contact_info'=> $contact_info,)
, 'verify'
);
I had to copy the view folder into the layout folder for it to work.
I don't need to use a layout, how do I avoid it from doing this? I just what it to render the view.
This is why in my original code above I had false, thinking it would stop the code from running this part.
#14
Posted 27 May 2009 - 02:55 PM
Anyone know how to avoid rendering to layout and only a view?
#15
Posted 27 May 2009 - 03:00 PM
Quote
Anyone know how to avoid rendering to layout and only a view?
In this case, you set the 3rd params to null, like
#16
Posted 27 May 2009 - 03:10 PM
#17
Posted 28 May 2009 - 11:39 AM
This is successfully sending an email from view 'verify' as long as I don't request values from $email. Both $account and $contact_info work fine. This is the error
If I change the getView() in EMailer.php from renderPartial to render, all of $email values are accessible. However, that then renders with a layout which i don't want. I suspect some sort of conflict with the name email might be the reason but it would be great if someone could explain this to me.
#18
Posted 28 May 2009 - 12:05 PM
public function getView($view, $vars = array(), $layout = null)
{
$body = Yii::app()->controller->renderPartial($this->pathViews.'.'.$view, array_merge($vars, array('email'=>$this->_myMailer)), true);
if ($layout === null) {
$this->_myMailer->Body = $body;
}
else {
$this->_myMailer->Body = Yii::app()->controller->renderPartial($this->pathLayouts.'.'.$layout, array('content'=>$body), true);
}
}
to this;
public function getView($view, $vars = array(), $layout = null)
{
$body = Yii::app()->controller->renderPartial($this->pathViews.'.'.$view, array_merge($vars, array('content'=>$this->_myMailer)), true);
if ($layout === null) {
$this->_myMailer->Body = $body;
}
else {
$this->_myMailer->Body = Yii::app()->controller->renderPartial($this->pathLayouts.'.'.$layout, array('content'=>$body), true);
}
}
#20
Posted 29 May 2009 - 07:14 PM
Sorry if it takes me a bit to answer or follow the posts (being too busy here)