Last week I publised a short tutorial about sending email using CakeEmail.

In this post, I'm starting from the previous tutorial and then I'm going to show how to send emails in html format.

Let's start!

 

 

cakephp

First we are going to add a new method to our controller, a good name for it will be send_html_email. In the method we have to specify the html format and the template we want to use.

 <?php 

public function send_html_email($dest=null, $aMsg=null)
{
        $Email = new CakeEmail('gmail');
        $Email->to($dest);
        $Email->emailFormat('html');
        $Email->template('mytemplate')->viewVars( array('aMsg' => $aMsg));
        $Email->subject('Automagically generated email');
        $Email->replyTo('the_mail_you_want_to_receive_replies@yourdomain.com');
        $Email->from ('your_user@gmail.com');
        $Email->send();
       return ;
}

In the declaration of this method I added a new parameter, a message to pass to the template. In lines 3 and 4 I specified the format for the email and the template to use (and a var to pass to it). so your method expects to find a template (named mytemplate.ctp) in app/View/Emails/html/. The following few lines of code sow a simple example for mytempate.ctp:

<html>

<h1>Good Morning Sir/Madam<br></h1>



<p><?php echo $aMsg;?></p>

</html>

 

The var $aMsg is passed from the controller to the template through the viewVars() in line 4.

Since all configurations have been done in the previous tutorial, now we have to do nothing more.

 

Gg1