Send Email using smtp in cakePHP


E-mail component is available in cakePHP . It is very easy to send email from that.

First we should include Email component in our controller.

(i.e) app/controller/example_controller.php


var $components = array('Email');

Then include below in your code


function email(){
$this->Email->to = 'recipent@gmail.com';
$this->Email->subject = 'Include your subject';
$this->Email->from = 'youremail@gmail.com';
$this->Email->template = 'template';  // file name template.ctp will be included in /views/elements/email/text/template.ctp
$this->Email->smtpOptions = array(
'port'=>'465',
'timeout'=>'30',
'host' => 'ssl://smtp.gmail.com',
'username'=>'youremail@gmail.com',
'password'=>'yourpassword',
);
$this->Email->delivery = 'smtp';
if ($this->Email->send()) {
return true;
} else {
echo $this->Email->smtpError;
}

}

Dont forget to include template.ctp in /views/elements/email/text/template.ctp

. It is very easy to send e-mail :)from your cakePHP application

enzoy :)


About these ads

About sathia

Web developer at cloudmint
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

17 Responses to Send Email using smtp in cakePHP

  1. V.Ramachandran says:

    Great man.:-)

  2. farisi says:

    Thanks,
    very helpful.

  3. Pranjal Bikash Das says:

    i am getting an error saying “Unable to find the socket transport "ssl" – did you forget to enable it when you configured PHP? “

  4. Rizwan says:

    templet.ctp..why include this?

    • sathia says:

      Hi Rizwan,
      That is file where we place content of email we going to send.
      that file can be in any name. it can be both in html format or text format.
      I think this may help you

  5. Ravindra says:

    It is great way to send mail using smtp .But i found a query in that ,There is no option to send body part excluding template.

    • sathia says:

      Hi,

      I think you are asking like this. You can send email without template. See options below.

      $this->Email->delivery = ‘smtp';
      $this->Email->from = $this->data["Contact"]["from"];
      $this->Email->to = $this->data["Contact"]["to"];
      $this->Email->subject = $this->data["Contact"]["subject"];
      //$this->Email->template = “default”;
      $this->Email->send($this->data["Contact"]["body"]);

      I have uncommented template options and passed body text to send() as parameter.

  6. http://bestnaturalhealthsupplements.com says:

    I would like to thank you for the efforts you’ve put in writing this blog. I’m hoping the same high-grade site post from you in the upcoming as well. Actually your creative writing skills has inspired me to get my own site now. Really the blogging is spreading its wings rapidly. Your write up is a good example of it.

  7. nikchandel says:

    my template.ctp isn’t rendering html, also how will I pass values in template

  8. vdipak says:

    Hey hi can any one tell me that where we have to call this Email funciton?

  9. Victor says:

    Nice work. Quick question, how do I pass form input into template.ctp

  10. Dorothea says:

    “Send Email using smtp in cakePHP | Sathia’s Blog” was indeed a pretty wonderful post, . Keep publishing and I’ll
    try to continue to keep reading! Thanks for the post -Autumn

  11. aman says:

    Nice tutorial

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s