CakePHP: Send email using CakeEmail

Sending an email is a very common feature of any application. We generally send newsletters, notifications, invoices etc. through emails. Email can be in simple text format or in beautiful HTML format.

In this tutorial, we will learn how to send emails in our CakePHP application using CakeEmail class.

Configurations

Very first thing of setting up the email functionality is, setting the configurations like from address, name, etc. There are 2 ways to do this configuration, either creating email.php in app/Config or setting them at run time while sending email.

Using configuration file

To make central configuration, create a email.php in app/Config/ directory. CakePHP provides a default email configuration file email.php.default, just rename it to email.php or copy the content from default file and create new email.php.

In that configuration file, there are three default options: default, smtp and fast. We can define the different config options here. For example,

When we send an email thorough CakeEmail, the above configuration array will set john@example.com as from email address and John Doe as from name.

Runtime options

We can set different options using the CakeEmail object. There are different methods available to set different config options. For example,

The above method will set the from option: from name and from email. See here for all available methods.

Basic use

To use CakeEmail in our application, we first need to load the class using App::uses() in our controller or model or where we want to use the class.

Now, we need to create an object of CakeEmail. Here, either we can load the configuration options from the class file (app/Config/email.php) or we can set them as runtime using different methods.

For example, to load config from file,

Or using our second option, runtime options

To set subject and email body,

The send() method accepts message body text as an argument and sets it as email body text.

Email Format

In above example, we saw a simple text email. To send a formatted HTML email, we can use templates in our email. CakeEmail allows to set the email format and use our view file and layout file for html formatting.

To set email format as html,

Now, let’s create a layout and view file. Create app/View/Layouts/Emails/html/mytemplate.ctp as our layout file. The content should look like,

And create app/View/Emails/html/myview.ctp as our view file. The content can be anything which contains HTML code, images and css, etc. Following is sample content,

After this, to set view and layout,

If you don’t specify template name, it will use the default template based on email format, app/View/Layouts/Emails/text/default.ctp for text and app/View/Layouts/Emails/html/default.ctp for html format.

The complete code will look like,

That’s it. Simple!

Passing data in template file

There are cases where we want to create dynamic content of our email. For example, to send a welcome email to a new user, we want to use user’s name, email address in message details. We can do this by passing user data to our template and view file.

There is a viewVars() method to handle this. We can pass our data in array format and that will be get available in our view file. Let’s consider the user welcome email. Create a new view file in app/View/Emails/html/welcome.ctp and the code should look like,

Now let’s pass our user data to this view using viewVars() method.

You can prepare $userDataArr by making database query or from anything that you want. Just pass data through viewVars() and then you can use them in your view file and layout file as well.

Adding attachments

To add attachment to email, you can use attachments() method of CakeEmail. Attachment can be any text file, image or zip file. Just pass the attachment data in array format, for example, to attach a text file,

You can also attach multiple files by creating multi array like,

That is it. This is the basic email sending functionality in CakePHP using CakeEmail.

You may also like...