Yii Framework Forum: [EXTENSION] Mailer using PHPMailer - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

[EXTENSION] Mailer using PHPMailer Rate Topic: -----

#1 User is offline   MetaYii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 07-October 08
  • Location:The Matrix

Posted 13 November 2008 - 06:27 PM

Feel free to download. Comment on this thread.

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

#2 User is offline   KJedi 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 381
  • Joined: 19-October 08
  • Location:Nikolaev, Ukraine (Europe)

Posted 06 February 2009 - 01:49 PM

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

#3 User is offline   MetaYii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 07-October 08
  • Location:The Matrix

Posted 11 April 2009 - 11:50 AM

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

#4 User is offline   MetaYii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 07-October 08
  • Location:The Matrix

Posted 16 May 2009 - 10:51 AM

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

bitmatix> Implemented your suggestion on pathViews and pathLayouts.
0

#5 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 21 May 2009 - 03:58 PM

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
0

#6 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 22 May 2009 - 10:41 AM

BUMP!!!

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

#7 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 22 May 2009 - 10:54 AM

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


0

#8 User is offline   tekki 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 05-May 09

Posted 22 May 2009 - 03:26 PM

Have you tried:

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

#9 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 22 May 2009 - 03:46 PM

yes tekki...

same error
0

#10 User is offline   will 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 179
  • Joined: 21-March 09

Posted 22 May 2009 - 03:50 PM

you should try sth like:
$mailer->GetView('verify', array(
                                    'account'=> $account,
                                    'contact_info'=> $contact_info,)
                                , 'main'
                              );

0

#11 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 22 May 2009 - 04:00 PM

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)


0

#12 User is offline   will 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 179
  • Joined: 21-March 09

Posted 22 May 2009 - 04:11 PM

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'
0

#13 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 22 May 2009 - 04:16 PM

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.
0

#14 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 27 May 2009 - 02:55 PM

Bump

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

#15 User is offline   will 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 179
  • Joined: 21-March 09

Posted 27 May 2009 - 03:00 PM

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

0

#16 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 27 May 2009 - 03:10 PM

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

#17 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 28 May 2009 - 11:39 AM

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.
0

#18 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 28 May 2009 - 12:05 PM

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

0

#19 User is offline   will 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 179
  • Joined: 21-March 09

Posted 28 May 2009 - 12:18 PM

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

#20 User is offline   MetaYii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 07-October 08
  • Location:The Matrix

Posted 29 May 2009 - 07:14 PM

Thanks, fixed in 2.2

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

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users