CakePHP SMTP Integration Guide

Guide to integrate Mailazy with your CakePHP application


You need to complete these given prerequisites, you can skip the step if you have already completed.

  1. Sign up for a Mailazy account.
  2. Complete Domain Authentication.
  3. Generate the Mailazy Access Key

Integrate Mailazy with CakePHP

CakePHP comes with an email library that supports SMTP. You can check CakePHP documentation page for more details. We have developed an example that 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:

email/ html/ default.ctp text/ default.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 Login email with the following structure:

app/ views/ elements/ email/ text/ registration.ctp html/ registration.ctp

In app/views/elements/email/text/registration.ctp add:

Dear <!--?php echo $name ?-->,

Thank you for Login. Please go here to finish your registration.

and in app/views/layouts/email/html/default.ctp add:

Dear <!--?php echo $name ?-->,

Thank you for registering. Please go to here 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 accesskey / accesssecret details, better to make them constant)

$this->Email->smtpOptions  =  array(
  'host' => '',
  'client' => ''

$this->Email->delivery  = 'smtp';
$this->Email->from  = 'Your Name ';
$this->Email->to  = 'Recipient Name ';
$this->set('name', 'Recipient Name');
$this->Email->subject  = 'This is a subject';
$this->Email->template  = 'Login';
$this->Email->sendAs  = 'both';