CakePhp 3.x | Send Email using SMTP

Author - Parminder Kaur
28.02.2017
|
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' => 'smtp.gmail.com',
                    '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->transport('mail');
            $email->from(['Sender_Email_id' => 'Sender Name'])
            ->to('Receiver_Email_id')
            ->subject(‘Test Subject’)
            ->attachments($path) //Path of attachment file
            ->send($message);

}
Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com 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. Required fields are marked *

Send your message