CakePHP comes with an email library that already supports SMTP. For more information check out the CakePHP documentation page. This example shows how to send an email with both HTML and text bodies.
In app/views/layouts/ you need to define the layout of your text and HTML emails:
1email/2html/3default.ctp4text/5default.ctp
In app/views/layouts/email/text/default.ctp add:
<!--?php echo $content_for_layout; ?-->
and in app/views/layouts/email/html/default.ctp add:
<!--?php echo $content_for_layout; ?-->
Then create the template for your emails. In this example we created templates for a registration email with the following structure:
1app/2views/3elements/4email/5text/6registration.ctp7html/8registration.ctp
In app/views/elements/email/text/registration.ctp add:
1Dear <!--?php echo $name ?-->,2Thank you for registering. Please go to http://domain.com to finish your registration.
and in app/views/layouts/email/html/default.ctp add:
1Dear <!--?php echo $name ?-->,2Thank you for registering. Please go to <a href="http://domain.com">here</a> to finish your registration.
In your controller enable the email component:
<!--?php var $components = array('Email'); ?-->
Then anywhere in your controller you can do something like the following to send an email: (make sure to replace your own sendgrid_username / sendgrid_api_key details, better to make them constant)
1<?php2$this->Email->smtpOptions = array(3'port'=>'587',4'timeout'=>'30',5'host' => 'smtp.sendgrid.net',6'username'=>'sendgrid_username',7'api_key'=>'sendgrid_api_key',8'client' => 'yourdomain.com'9);1011$this->Email->delivery = 'smtp';12$this->Email->from = 'Your Name ';13$this->Email->to = 'Recipient Name ';14$this->set('name', 'Recipient Name');15$this->Email->subject = 'This is a subject';16$this->Email->template = 'registration';17$this->Email->sendAs = 'both';18$this->Email->send();19?>