CakePHP: How to have Consistent SMTP Connection

CakePHP: How to have Consistent SMTP Connection

CakePHP SMTP

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!

 

Tags:
, ,
Jay
Jayadev Das
jayadev.das@andolasoft.com

Do what you do best in – that’s what I’ve always believed in and that’s what I preach. Over the past 25+ years (yup that’s my expertise ‘n’ experience in the Information Technology domain), I’ve been consulting to small, medium and large companies ‘bout Web Technologies, Mobile Future as well as on the good-and-bad of tech. Blogger, International Business Advisor, Web Technology Expert, Sales Guru, Startup Mentor, Insurance Sales Portal Expert & a Tennis Player. And top of all – a complete family man!