Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

93 lines (73 sloc) 2.512 kb
layout weight title seo navigation
page
0
CakePHP
title description
Send Email with CakePHP & SendGrid
View instructions on how to easily send email with CakePHP using SendGrid, by setting up your views and controller.
show
true

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:

{% codeblock %} email/ html/ default.ctp text/ default.ctp {% endcodeblock %}

In app/views/layouts/email/text/default.ctp add: {% codeblock lang:php %}

{% endcodeblock %}

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

{% codeblock lang:php %}

{% endcodeblock %}

Then create the template for your emails. In this example we created templates for a registration email with the following structure:

{% codeblock %} app/ views/ elements/ email/ text/ registration.ctp html/ registration.ctp {% endcodeblock %}

In app/views/elements/email/text/registration.ctp add: {% codeblock lang:php %} Dear <!--?php echo $name ?-->, Thank you for registering. Please go to http://domain.com to finish your registration. {% endcodeblock %}

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

{% codeblock lang:php %} Dear <!--?php echo $name ?-->, Thank you for registering. Please go to here to finish your registration. {% endcodeblock %}

In your controller enable the email component:

{% codeblock lang:php %}

{% endcodeblock %}

Then anywhere in your controller you can do something like the following to send an email:

{% codeblock lang:php %} <?php $this->Email->smtpOptions = array( 'port'=>'587', 'timeout'=>'30', 'host' => 'smtp.sendgrid.net', 'username'=>'sendgrid_username', 'password'=>'sendgrid_password', 'client' => 'yourdomain.com' );

$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 = 'registration'; $this->Email->sendAs = 'both'; $this->Email->send(); ?> {% endcodeblock %}

Jump to Line
Something went wrong with that request. Please try again.