CakePHP email timeout & PHP maximum execution time -


my application uses exchange smtp server sending emails. @ present don't have message queuing in our architecture , emails sent part of http request/response cycle.

occasionally exchange server has issues , times out while email sending, , result email doesn't send. sometimes, cake recognizes time out , throws exception. application code can catch exception , report user went wrong.

however, on other occasions php hits maximum execution time before cake can throw exception , user gets error 500 no useful information happened.

in effort combat this, overwrote cakeemail::send() in custom class customemail (extending cakeemail) follows:

public function send($content = null) {     //get php , email timeout values     $phptimeout = ini_get("max_execution_time");      //if $this->_transportclass debug, invoke parent::send() , return     if (!$this->_transportclass instanceof smtptransport) {         return parent::send($content);     }      $cfg = $this->_transportclass->config();     $emailtimeout = isset($cfg["timeout"]) && $cfg["timeout"] ? $cfg["timeout"] : 30;      //if php max execution time set (and isn't 0), set email timeout plus 1 second; should mean smtp server should time out before php     if ($phptimeout) {         set_time_limit($emailtimeout + 1);     }      //send email     $send = parent::send($content);      //reset php timeout previous value     set_time_limit($phptimeout);      return $send; } 

however, isn't alwayus successful , have had few instances of this:

fatal error: maximum execution time of 31 seconds exceeded in [c:\path\app\vendor\pear-pear.cakephp.org\cakephp\cake\network\cakesocket.php, line 303]

cakesocket.php line 303 $buffer = fread()... line cakesocket::read():

public function read($length = 1024) {     if (!$this->connected) {         if (!$this->connect()) {             return false;         }     }      if (!feof($this->connection)) {         $buffer = fread($this->connection, $length);         $info = stream_get_meta_data($this->connection);         if ($info['timed_out']) {             $this->setlasterror(e_warning, __d('cake_dev', 'connection timed out'));             return false;         }         return $buffer;     }     return false; } 

any ideas?

the problem lies here:

//if php max execution time set (and isn't 0), set email timeout plus 1 second; should mean smtp server should time out before php if ($phptimeout) {     set_time_limit($emailtimeout + 1); } 

in places in code increasing max time out more 30 seconds, email timeout still 30. code reverted php timeout 31 seconds, , i'm guessing other stuff happening before email started send caused issues.

fixed code:

//if php max execution time set (and isn't 0), set email timeout plus 1 second; should mean smtp server should time out before php if ($phptimeout && $phptimeout <= $emailtimeout) {     set_time_limit($emailtimeout + 1); } 

Comments

Post a Comment

Popular posts from this blog

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -