[EXTENSION] Mailer using PHPMailer


(Metayii Framework) #1

Feel free to download. Comment on this thread.

http://www.yiiframew…ailer/#download


(Konstantin Mirin) #2

I'm using this code:

$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/activate.html?id=10&activationKey=210021087516536241





-- 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?


(Metayii Framework) #3

New release: 2.0 using PHPMailer 5.0 and including the getView method.


(Metayii Framework) #4

Konstantin> What you're seeing is caused by the UTF-8 encoding.

bitmatix> Implemented your suggestion on pathViews and pathLayouts.


(Luke Pullin) #5

Could you please post a working example of how to get the body (message) of email to contain that of a view.

lets say i have a views/email/verifyEmail.php

thanks


(Luke Pullin) #6

BUMP!!!

Anyone know what actions tells mailer to render view????


(Luke Pullin) #7

This is what I have so far inside my controller;

$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

(system) #8

Have you tried:

$email->MsgHTML($this->render('email/verify', array…


(Luke Pullin) #9

yes tekki…

same error


(Phpwindcn) #10

you should try sth like:

$mailer->GetView('verify', array(


                                    'account'=> $account,


                                    'contact_info'=> $contact_info,)


                                , 'main'


                              			);

(Luke Pullin) #11

tried that too will;

$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)

(Phpwindcn) #12

there was a typo in my last post( I have corrected that).

The last param of GetView() method should be the layout you want to use, should be a string, e.g. 'main'


(Luke Pullin) #13

Thanks so much will.

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.


(Luke Pullin) #14

Bump

Anyone know how to avoid rendering to layout and only a view?


(Phpwindcn) #15

Quote

Bump

Anyone know how to avoid rendering to layout and only a view?

In this case, you set the 3rd params to null, like


(Luke Pullin) #16

Thanks will, such an idiot, was trying to set it to false instead of obvious, NULL!!!


(Luke Pullin) #17

One last issue I am having trouble trying to steam out is that for the following code;

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.


(Luke Pullin) #18

Found where the conflict was occurring in EMailer.php, changed code from this;

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);

      }

  }


(Phpwindcn) #19

good catch! I hope the maintainer can release bug fix release.


(Metayii Framework) #20

Thanks, fixed in 2.2

Sorry if it takes me a bit to answer or follow the posts (being too busy here)