Rate this page:

CakePHP

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:

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 registration 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 registering. Please go to http://domain.com 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 <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)

<?php
$this->Email->smtpOptions = array(
  'port'=>'587',
  'timeout'=>'30',
  'host' => 'smtp.sendgrid.net',
  'username'=>'sendgrid_username',
  'api_key'=>'sendgrid_api_key',
  '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();
?>
Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the SendGrid tag on Stack Overflow.

        
        
        

        Thank you for your feedback!

        We are always striving to improve our documentation quality, and your feedback is valuable to us. Please select the reason(s) for your feedback or provide additional information about how we can improve:

        Sending your feedback...
        🎉 Thank you for your feedback!
        Something went wrong. Please try again.

        Thanks for your feedback!

        Refer us and get $10 in 3 simple steps!

        Step 1

        Get link

        Get a free personal referral link here

        Step 2

        Give $10

        Your user signs up and upgrade using link

        Step 3

        Get $10

        1,250 free SMSes
        OR 1,000 free voice mins
        OR 12,000 chats
        OR more