CakePhp 3.x | Send Email using SMTP

Author - Parminder Kaur
1 Comment

To send an email in cakephp use Email class of Cakephp 3. In app.php under config folder, add a new entry in the table EmailTransport.

For example here ‘mail’. Specify host, port, username and password in it:

'EmailTransport' => [
        'default' => [
            'className' => 'Mail',
            // The following keys are used in SMTP transports
            'host' => 'localhost',
            'port' => 25,
            'timeout' => 30,
            'username' => 'user',
            'password' => 'secret',
            'client' => null,
            'tls' => null,
            'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
            ‘mail’=> [
                    'host' => '',
                    'port' => 587,
                    'username' =>xxxxx', //gmail id
                    'password' =>xxxxx, //gmail password
                    'tls' => true,
                    'className' => 'Smtp'

Now in Controller, the function to send email uses above-written entry in transport() function as below.

Add path in controller- use Cake\Mailer\Email:

function sendEmail()
           $message = "Hello User";            
            $email = new Email();
            $email->from(['Sender_Email_id' => 'Sender Name'])
            ->subject(‘Test Subject’)
            ->attachments($path) //Path of attachment file

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at for your Insurance, eLearning and Salesforce applications.

One response on “CakePhp 3.x | Send Email using SMTP

  1. Cicely says:

    Thanks for the info!

Leave a Reply

Your email address will not be published.