CakePHP: How To Have Consistent SMTP Connection

It’s necessary to configure an SMTP server that will take care of your email deliveries while setting up a mail client. The process is easy – just add the right SMTP parameters to the settings of your email software & you’re good to go.

SMTP server has a limit for number of requests in a time interval & a max limit for sending emails per connection, which varies as per different service providers.

  • While creating the object of Cake Email utility, we have to pass “persistent = true”.

[code language=”html”]
$Email = new CakeEmail();
$Email->config(array(‘persistent’ => true));
[/code]

  • Change the below file for persistent connection changes.

[code language=”html”]
lib\Cake\Network\Email\SmtpTransport.php
public function send(CakeEmail $email) {
$this->_cakeEmail = $email;

if(!$this->_config[‘persistent’] || !$this->_socket){
$this->_connect();
$this->_auth();
}
$this->_sendRcpt();
$this->_sendData();
if(!$this->_config[‘persistent’]){
$this->_disconnect();
}
return $this->_content;
}
[/code]

  • Add this function in the CakeEmail file.

[code language=”html”]
lib\Cake\Network\Email\CakeEmail.php
public function disconnect() {
$this->transportClass()->disconnect();
}

lib\Cake\Network\Email\SmtpTransport.php
public function disconnect(){
$this->_disconnect();
}
[/code]

  • Disconnect the connection, after emails are sent.

[code language=”html”]
$Email->disconnect();
[/code]

Remember that using a standard SMTP (the one associated to free email providers like Yahoo, Hotmail or Gmail) doesn’t ensure delivery of all your messages, especially if you have a long list of recipients. 

Hope  this article helped configuring an SMTP connection.

Andolasoft has the experience in CakePHP Framework & application development. Planning something on CakePHP? Convert your idea into app. Talk to us Today!