Sending email using CakePHP 3

Spread the love

CakePHP 3 send email functionality is having certain steps to follow, for proper setup and working. I found sending email in CakePHP 3 somewhat tricky.

So I am always following certain steps in sequence to setup CakePHP 3 send email functionality correctly.

NOTE: In order to send emails from CakePHP 3 using your server, you have to do email server settings in your CakePHP 3 application.

In this example, I am using “CakePHP 3 Mailer” feature.

Steps for setup of CakePHP 3 send email functionality with Mailer are as below:

  1. Bake your new instance of the class Mailer with any name as per your requirement. I am using a name “SendEmail”. Bake this new Mailer instance using Composer in your project directory by using command –
    “<path to your project directory>bincake bake mailer SendEmail”.– This will create required layout files in the directory – LayoutEmailhtml with name send_email.ctp and same file in directory LayoutEmailtext.
    – Creates new directory “Mailer” with a class file, named “.
  2. Now add public method named “sendEmail” in “SendEmailMailer” class and add code as per below in method
    public function sendEmail(array $data)
    {
         $this->from('test@gmail.com','Users')
                    ->to('touser@gmail.com')
                    ->subject(sprintf('Welcome %s', $data->name))
                    ->template('default','default')
                    ->set(['data'=>$data->first_name]);
    }

    Updates in your controller:

  3. Now go to your controller. In this example, I am using “UsersController” and add “use CakeMailerMailerAwareTrait;” at the top of “UsersController” class. And add use MailerAwareTrait; inside “UsersController” above all methods. Like below:
    namespace AppController;
    
    use AppControllerAppController;
    use CakeEventEvent;
    use CakeI18nDate;
    use CakeUtilityText;
    use CakeUtilitySecurity;
    use CakeRoutingRouter;
    use CakeMailerMailerAwareTrait;
    
    /**
     * Users Controller
     *
     * @property AppModelTableUsersTable $Users
     */
    class UsersController extends AppController {
        
        use MailerAwareTrait;
    
        public function initialize() {
            parent::initialize();
    
            $this->loadComponent('Images');
            //$this->loadComponent('Csrf');
            $this->loadComponent('Date');
        }
    .............
  4. Now add “$this->getMailer(‘SendEmail’)->send(‘sendEmail’, [$user]);” in a method where you want to call an email functionality for sending email.
    In “$this->getMailer(‘SendEmail’)->send(‘sendEmail’, [$user]);” – ‘SendEmail‘ is a Mailer class name which we had created using bake. and ‘sendEmail‘ is a method name present in ‘SendEmail’ class and “[$user]” would be any data in the form of “ARRAY“. Finally, this will call ‘sendEmail’ method and if all things are proper, an email will get send.Furthermore, I used above call in my “view” method of UsersController as shown below:
    public function view() {
            $id = $this->Auth->user('id');
            $user = $this->Users->get($id, [
                'contain' => ['Groups']
            ]);
            $this->getMailer('SendEmail')->send('sendEmail', [$user]);
            $this->set('user', $user);
            $this->set('_serialize', ['user']);
    }

    So, as per above example, we are able to send emails in CakePHP 3 with ease. In conclusion hope, this example will help you in using CakePHP 3 send email functionality.


Spread the love

2 thoughts on “Sending email using CakePHP 3”

Leave a Comment