Anthony Wanjohi
Anthony Wanjohi

Reputation: 25

Configure CakePhp to send mail with SMTP

My web servers have disabled mail for security purposes I now need to reconfigure my cakephp code to send the emails via SMTP as recommended by the host.

My code runs fine on localhost with php mail enabled

use Cake\Mailer\Email;

class LoansController extends AppController

public function sendtestemail(){
$email = new Email();
$email->setViewVars(['name' => 'test test', 'subject'=>'subject test', 
'message'=>'testit']);
$email
->template('bulkemail')
->emailFormat('html')
->to('[email protected]')
->from('[email protected]')
->subject($subject)
->send();
}

error: Could not send email: mail() has been disabled for security reasons Cake\Network\Exception\SocketException

Upvotes: 2

Views: 6222

Answers (2)

Stephen Ngethe
Stephen Ngethe

Reputation: 1044

What worked for me is from https://book.cakephp.org/2/en/core-utility-libraries/email.html

Edit /app/Config/email.php

public $smtp = array(
       'transport' => 'Smtp',
        'from' => array('[email protected]' => 'Betting site'),
        'host'      => 'ssl://smtp.gmail.com',
        'port'      => 465,
        'username'  => '[email protected]',
        'password'  => 'xxxxxxxxxxpass',
        'client'    => null,
        'log'       => true
        );

Upvotes: 0

Lucas Meine
Lucas Meine

Reputation: 1619

My code runs fine on localhost with php mail enabled

It works fine in localhost but not in your remote hosting because your hosting company disabled it and you probably do not have much control over it.

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.

In your case ‘Smtp’. Specify host, port, username and password in it:

'EmailTransport' => [
        'default' => [
            'className' => 'Smtp',
            // 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);

}

Also have in mind that many hosting companies also block default smtp ports. ( I'm aware that digital ocean does it, for example ). So, you might have to change that port or contact them to get it opened for you ( usually after some sort of verification ).

Some reference about what I just answered: https://www.digitalocean.com/community/questions/digital-ocean-firewall-blocking-sending-email

Upvotes: 2

Related Questions