From cf22557383fea89906633b8034ff9f08eb498621 Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Mon, 18 Feb 2013 01:08:04 +0530 Subject: Added keep-alive connection to SMTP. Fixed socket read/write timeouts. Added PHP useragent --- system/libraries/Email.php | 524 +++++++++++++++++++++++++-------------------- 1 file changed, 290 insertions(+), 234 deletions(-) (limited to 'system/libraries/Email.php') diff --git a/system/libraries/Email.php b/system/libraries/Email.php index 997757b0a..36cebfab5 100644 --- a/system/libraries/Email.php +++ b/system/libraries/Email.php @@ -94,6 +94,13 @@ class CI_Email { * @var int */ public $smtp_timeout = 5; + + /** + * STMP Persistent connection + * + * @var bool + */ + public $smtp_keepalive = FALSE; /** * SMTP Encryption @@ -399,6 +406,19 @@ class CI_Email { log_message('debug', 'Email Class Initialized'); } + + // -------------------------------------------------------------------- + + /** + * Destructor - Releases Resources + * + * @return void + */ + function __destruct() + { + if(is_resource($this->_smtp_connect)) + $this->_send_command('quit'); + } // -------------------------------------------------------------------- @@ -770,6 +790,23 @@ class CI_Email { // -------------------------------------------------------------------- + /** + * Set Useragent + * + * @param string + * @return void + */ + public function set_useragent($type = '') + { + if( ! $type) + $this->useragent = isset($_SERVER['HTTP_USER_AGENT'])? $_SERVER['HTTP_USER_AGENT'] : 'PHP/'.phpversion(); + else + $this->useragent = $type; + return $this; + } + + // -------------------------------------------------------------------- + /** * Set Wordwrap * @@ -1766,241 +1803,260 @@ class CI_Email { * @return bool */ protected function _send_with_smtp() - { - if ($this->smtp_host === '') - { - $this->_set_error_message('lang:email_no_hostname'); - return FALSE; - } - - if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate()) - { - return FALSE; - } - - $this->_send_command('from', $this->clean_email($this->_headers['From'])); - - foreach ($this->_recipients as $val) - { - $this->_send_command('to', $val); - } - - if (count($this->_cc_array) > 0) - { - foreach ($this->_cc_array as $val) - { - if ($val !== '') - { - $this->_send_command('to', $val); - } - } - } - - if (count($this->_bcc_array) > 0) - { - foreach ($this->_bcc_array as $val) - { - if ($val !== '') - { - $this->_send_command('to', $val); - } - } - } - - $this->_send_command('data'); - - // perform dot transformation on any lines that begin with a dot - $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody)); - - $this->_send_data('.'); - - $reply = $this->_get_smtp_data(); - - $this->_set_error_message($reply); - - if (strpos($reply, '250') !== 0) - { - $this->_set_error_message('lang:email_smtp_error', $reply); - return FALSE; - } - - $this->_send_command('quit'); - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * SMTP Connect - * - * @return string - */ - protected function _smtp_connect() - { - $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL; - - $this->_smtp_connect = fsockopen($ssl.$this->smtp_host, - $this->smtp_port, - $errno, - $errstr, - $this->smtp_timeout); - - if ( ! is_resource($this->_smtp_connect)) - { - $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr); - return FALSE; - } - - $this->_set_error_message($this->_get_smtp_data()); - - if ($this->smtp_crypto === 'tls') - { - $this->_send_command('hello'); - $this->_send_command('starttls'); - - $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT); - - if ($crypto !== TRUE) - { - $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data()); - return FALSE; - } - } - - return $this->_send_command('hello'); - } - - // -------------------------------------------------------------------- + { + if ($this->smtp_host === '') + { + $this->_set_error_message('lang:email_no_hostname'); + return FALSE; + } + + if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate()) + { + return FALSE; + } + + $this->_send_command('from', $this->clean_email($this->_headers['From'])); + + foreach ($this->_recipients as $val) + { + $this->_send_command('to', $val); + } + + if (count($this->_cc_array) > 0) + { + foreach ($this->_cc_array as $val) + { + if ($val !== '') + { + $this->_send_command('to', $val); + } + } + } + + if (count($this->_bcc_array) > 0) + { + foreach ($this->_bcc_array as $val) + { + if ($val !== '') + { + $this->_send_command('to', $val); + } + } + } + + $this->_send_command('data'); + + // perform dot transformation on any lines that begin with a dot + $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody)); + + $this->_send_data('.'); + + $reply = $this->_get_smtp_data(); + + $this->_set_error_message($reply); + + if (strpos($reply, '250') !== 0) + { + $this->_set_error_message('lang:email_smtp_error', $reply); + return FALSE; + } + + if($this->smtp_keepalive) + $this->_send_command('reset'); + else + $this->_send_command('quit'); + + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * SMTP Connect + * + * @param bool + * @return string + */ + protected function _smtp_connect($force=FALSE) + { + if( ! is_resource($this->_smtp_connect) || $force) + { + $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL; + + $this->_smtp_connect = fsockopen($ssl.$this->smtp_host, + $this->smtp_port, + $errno, + $errstr, + $this->smtp_timeout); + + if ( ! is_resource($this->_smtp_connect)) + { + $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr); + return FALSE; + } + + stream_set_timeout($this->_smtp_connect, $this->smtp_timeout); + $this->_set_error_message($this->_get_smtp_data()); + + if ($this->smtp_crypto === 'tls') + { + $this->_send_command('hello'); + $this->_send_command('starttls'); + + $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT); + + if ($crypto !== TRUE) + { + $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data()); + return FALSE; + } + } + + return $this->_send_command('hello'); + } + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Send SMTP command + * + * @param string + * @param string + * @return string + */ + protected function _send_command($cmd, $data = '') + { + switch ($cmd) + { + case 'hello' : + + if ($this->_smtp_auth OR $this->_get_encoding() === '8bit') + { + $this->_send_data('EHLO '.$this->_get_hostname()); + } + else + { + $this->_send_data('HELO '.$this->_get_hostname()); + } + + $resp = 250; + break; + case 'starttls' : + + $this->_send_data('STARTTLS'); + $resp = 220; + break; + case 'from' : + + $this->_send_data('MAIL FROM:<'.$data.'>'); + $resp = 250; + break; + case 'to' : + + if ($this->dsn) + { + $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data); + } + else + { + $this->_send_data('RCPT TO:<'.$data.'>'); + } + + $resp = 250; + break; + case 'data' : + + $this->_send_data('DATA'); + $resp = 354; + break; + case 'reset': + + $this->_send_data('RSET'); + + $resp = 250; + break; + case 'quit' : + + $this->_send_data('QUIT'); + $resp = 221; + break; + } + + $reply = $this->_get_smtp_data(); + + $this->_debug_msg[] = '
'.$cmd.': '.$reply.'
'; + + if ((int) substr($reply, 0, 3) !== $resp) + { + $this->_set_error_message('lang:email_smtp_error', $reply); + return FALSE; + } + + if ($cmd === 'quit') + { + fclose($this->_smtp_connect); + } + + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * SMTP Authenticate + * + * @return bool + */ + protected function _smtp_authenticate() + { + if ( ! $this->_smtp_auth) + { + return TRUE; + } + + if ($this->smtp_user === '' && $this->smtp_pass === '') + { + $this->_set_error_message('lang:email_no_smtp_unpw'); + return FALSE; + } + + $this->_send_data('AUTH LOGIN'); + + $reply = $this->_get_smtp_data(); + + if (strpos($reply, '503') === 0) // Already authenticated + return TRUE; + + if (strpos($reply, '334') !== 0) + { + $this->_set_error_message('lang:email_failed_smtp_login', $reply); + return FALSE; + } + + $this->_send_data(base64_encode($this->smtp_user)); + + $reply = $this->_get_smtp_data(); + + if (strpos($reply, '334') !== 0) + { + $this->_set_error_message('lang:email_smtp_auth_un', $reply); + return FALSE; + } + + $this->_send_data(base64_encode($this->smtp_pass)); + + $reply = $this->_get_smtp_data(); + + if (strpos($reply, '235') !== 0) + { + $this->_set_error_message('lang:email_smtp_auth_pw', $reply); + return FALSE; + } - /** - * Send SMTP command - * - * @param string - * @param string - * @return string - */ - protected function _send_command($cmd, $data = '') - { - switch ($cmd) - { - case 'hello' : - - if ($this->_smtp_auth OR $this->_get_encoding() === '8bit') - { - $this->_send_data('EHLO '.$this->_get_hostname()); - } - else - { - $this->_send_data('HELO '.$this->_get_hostname()); - } - - $resp = 250; - break; - case 'starttls' : - - $this->_send_data('STARTTLS'); - $resp = 220; - break; - case 'from' : - - $this->_send_data('MAIL FROM:<'.$data.'>'); - $resp = 250; - break; - case 'to' : - - if ($this->dsn) - { - $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data); - } - else - { - $this->_send_data('RCPT TO:<'.$data.'>'); - } - - $resp = 250; - break; - case 'data' : - - $this->_send_data('DATA'); - $resp = 354; - break; - case 'quit' : - - $this->_send_data('QUIT'); - $resp = 221; - break; - } - - $reply = $this->_get_smtp_data(); - - $this->_debug_msg[] = '
'.$cmd.': '.$reply.'
'; - - if ((int) substr($reply, 0, 3) !== $resp) - { - $this->_set_error_message('lang:email_smtp_error', $reply); - return FALSE; - } - - if ($cmd === 'quit') - { - fclose($this->_smtp_connect); - } - - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * SMTP Authenticate - * - * @return bool - */ - protected function _smtp_authenticate() - { - if ( ! $this->_smtp_auth) - { - return TRUE; - } - - if ($this->smtp_user === '' && $this->smtp_pass === '') - { - $this->_set_error_message('lang:email_no_smtp_unpw'); - return FALSE; - } - - $this->_send_data('AUTH LOGIN'); - - $reply = $this->_get_smtp_data(); - - if (strpos($reply, '334') !== 0) - { - $this->_set_error_message('lang:email_failed_smtp_login', $reply); - return FALSE; - } - - $this->_send_data(base64_encode($this->smtp_user)); - - $reply = $this->_get_smtp_data(); - - if (strpos($reply, '334') !== 0) - { - $this->_set_error_message('lang:email_smtp_auth_un', $reply); - return FALSE; - } - - $this->_send_data(base64_encode($this->smtp_pass)); - - $reply = $this->_get_smtp_data(); - - if (strpos($reply, '235') !== 0) - { - $this->_set_error_message('lang:email_smtp_auth_pw', $reply); - return FALSE; - } - - return TRUE; - } + return TRUE; + } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 59209deaf2467372ba0deef5a4266758b366ca70 Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Mon, 18 Feb 2013 17:02:13 +0530 Subject: Fixed tab-indentation. Made appropriate entries in changelog --- system/libraries/Email.php | 565 ++++++++++++++++++++++----------------------- 1 file changed, 282 insertions(+), 283 deletions(-) (limited to 'system/libraries/Email.php') diff --git a/system/libraries/Email.php b/system/libraries/Email.php index 36cebfab5..7ea49a959 100644 --- a/system/libraries/Email.php +++ b/system/libraries/Email.php @@ -96,11 +96,11 @@ class CI_Email { public $smtp_timeout = 5; /** - * STMP Persistent connection - * - * @var bool - */ - public $smtp_keepalive = FALSE; + * SMTP persistent connection + * + * @var bool + */ + public $smtp_keepalive = FALSE; /** * SMTP Encryption @@ -406,19 +406,19 @@ class CI_Email { log_message('debug', 'Email Class Initialized'); } - + // -------------------------------------------------------------------- /** - * Destructor - Releases Resources - * - * @return void - */ - function __destruct() - { - if(is_resource($this->_smtp_connect)) - $this->_send_command('quit'); - } + * Destructor - Releases Resources + * + * @return void + */ + public function __destruct() + { + if(is_resource($this->_smtp_connect)) + $this->_send_command('quit'); + } // -------------------------------------------------------------------- @@ -789,22 +789,22 @@ class CI_Email { } // -------------------------------------------------------------------- - - /** - * Set Useragent - * - * @param string - * @return void - */ - public function set_useragent($type = '') - { - if( ! $type) - $this->useragent = isset($_SERVER['HTTP_USER_AGENT'])? $_SERVER['HTTP_USER_AGENT'] : 'PHP/'.phpversion(); - else - $this->useragent = $type; - return $this; - } + /** + * Set Useragent + * + * @param string + * @return void + */ + public function set_useragent($type = '') + { + if( ! $type) + $this->useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'PHP/'.phpversion(); + else + $this->useragent = $type; + return $this; + } + // -------------------------------------------------------------------- /** @@ -1803,260 +1803,259 @@ class CI_Email { * @return bool */ protected function _send_with_smtp() - { - if ($this->smtp_host === '') - { - $this->_set_error_message('lang:email_no_hostname'); - return FALSE; - } - - if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate()) - { - return FALSE; - } - - $this->_send_command('from', $this->clean_email($this->_headers['From'])); - - foreach ($this->_recipients as $val) - { - $this->_send_command('to', $val); - } - - if (count($this->_cc_array) > 0) - { - foreach ($this->_cc_array as $val) - { - if ($val !== '') - { - $this->_send_command('to', $val); - } - } - } - - if (count($this->_bcc_array) > 0) - { - foreach ($this->_bcc_array as $val) - { - if ($val !== '') - { - $this->_send_command('to', $val); - } - } - } - - $this->_send_command('data'); - - // perform dot transformation on any lines that begin with a dot - $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody)); - - $this->_send_data('.'); - - $reply = $this->_get_smtp_data(); - - $this->_set_error_message($reply); - - if (strpos($reply, '250') !== 0) - { - $this->_set_error_message('lang:email_smtp_error', $reply); - return FALSE; - } - - if($this->smtp_keepalive) - $this->_send_command('reset'); - else - $this->_send_command('quit'); - - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * SMTP Connect - * - * @param bool - * @return string - */ - protected function _smtp_connect($force=FALSE) - { - if( ! is_resource($this->_smtp_connect) || $force) - { - $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL; - - $this->_smtp_connect = fsockopen($ssl.$this->smtp_host, - $this->smtp_port, - $errno, - $errstr, - $this->smtp_timeout); - - if ( ! is_resource($this->_smtp_connect)) - { - $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr); - return FALSE; - } - - stream_set_timeout($this->_smtp_connect, $this->smtp_timeout); - $this->_set_error_message($this->_get_smtp_data()); - - if ($this->smtp_crypto === 'tls') - { - $this->_send_command('hello'); - $this->_send_command('starttls'); - - $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT); - - if ($crypto !== TRUE) - { - $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data()); - return FALSE; - } - } - - return $this->_send_command('hello'); - } - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Send SMTP command - * - * @param string - * @param string - * @return string - */ - protected function _send_command($cmd, $data = '') - { - switch ($cmd) - { - case 'hello' : - - if ($this->_smtp_auth OR $this->_get_encoding() === '8bit') - { - $this->_send_data('EHLO '.$this->_get_hostname()); - } - else - { - $this->_send_data('HELO '.$this->_get_hostname()); - } - - $resp = 250; - break; - case 'starttls' : - - $this->_send_data('STARTTLS'); - $resp = 220; - break; - case 'from' : - - $this->_send_data('MAIL FROM:<'.$data.'>'); - $resp = 250; - break; - case 'to' : - - if ($this->dsn) - { - $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data); - } - else - { - $this->_send_data('RCPT TO:<'.$data.'>'); - } - - $resp = 250; - break; - case 'data' : - - $this->_send_data('DATA'); - $resp = 354; - break; - case 'reset': - - $this->_send_data('RSET'); - - $resp = 250; - break; - case 'quit' : - - $this->_send_data('QUIT'); - $resp = 221; - break; - } - - $reply = $this->_get_smtp_data(); - - $this->_debug_msg[] = '
'.$cmd.': '.$reply.'
'; - - if ((int) substr($reply, 0, 3) !== $resp) - { - $this->_set_error_message('lang:email_smtp_error', $reply); - return FALSE; - } - - if ($cmd === 'quit') - { - fclose($this->_smtp_connect); - } - - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * SMTP Authenticate - * - * @return bool - */ - protected function _smtp_authenticate() - { - if ( ! $this->_smtp_auth) - { - return TRUE; - } - - if ($this->smtp_user === '' && $this->smtp_pass === '') - { - $this->_set_error_message('lang:email_no_smtp_unpw'); - return FALSE; - } - - $this->_send_data('AUTH LOGIN'); - - $reply = $this->_get_smtp_data(); - - if (strpos($reply, '503') === 0) // Already authenticated - return TRUE; - - if (strpos($reply, '334') !== 0) - { - $this->_set_error_message('lang:email_failed_smtp_login', $reply); - return FALSE; - } - - $this->_send_data(base64_encode($this->smtp_user)); - - $reply = $this->_get_smtp_data(); - - if (strpos($reply, '334') !== 0) - { - $this->_set_error_message('lang:email_smtp_auth_un', $reply); - return FALSE; - } - - $this->_send_data(base64_encode($this->smtp_pass)); - - $reply = $this->_get_smtp_data(); - - if (strpos($reply, '235') !== 0) - { - $this->_set_error_message('lang:email_smtp_auth_pw', $reply); - return FALSE; - } + { + if ($this->smtp_host === '') + { + $this->_set_error_message('lang:email_no_hostname'); + return FALSE; + } + + if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate()) + { + return FALSE; + } + + $this->_send_command('from', $this->clean_email($this->_headers['From'])); + + foreach ($this->_recipients as $val) + { + $this->_send_command('to', $val); + } + + if (count($this->_cc_array) > 0) + { + foreach ($this->_cc_array as $val) + { + if ($val !== '') + { + $this->_send_command('to', $val); + } + } + } - return TRUE; - } + if (count($this->_bcc_array) > 0) + { + foreach ($this->_bcc_array as $val) + { + if ($val !== '') + { + $this->_send_command('to', $val); + } + } + } + + $this->_send_command('data'); + + // perform dot transformation on any lines that begin with a dot + $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody)); + + $this->_send_data('.'); + + $reply = $this->_get_smtp_data(); + + $this->_set_error_message($reply); + + if (strpos($reply, '250') !== 0) + { + $this->_set_error_message('lang:email_smtp_error', $reply); + return FALSE; + } + + if($this->smtp_keepalive) + $this->_send_command('reset'); + else + $this->_send_command('quit'); + + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * SMTP Connect + * + * @param bool + * @return string + */ + protected function _smtp_connect($force=FALSE) + { + if( ! is_resource($this->_smtp_connect) || $force) + { + $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL; + + $this->_smtp_connect = fsockopen($ssl.$this->smtp_host, + $this->smtp_port, + $errno, + $errstr, + $this->smtp_timeout); + + if ( ! is_resource($this->_smtp_connect)) + { + $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr); + return FALSE; + } + + stream_set_timeout($this->_smtp_connect, $this->smtp_timeout); + $this->_set_error_message($this->_get_smtp_data()); + + if ($this->smtp_crypto === 'tls') + { + $this->_send_command('hello'); + $this->_send_command('starttls'); + + $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT); + + if ($crypto !== TRUE) + { + $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data()); + return FALSE; + } + } + + return $this->_send_command('hello'); + } + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Send SMTP command + * + * @param string + * @param string + * @return string + */ + protected function _send_command($cmd, $data = '') + { + switch ($cmd) + { + case 'hello' : + + if ($this->_smtp_auth OR $this->_get_encoding() === '8bit') + { + $this->_send_data('EHLO '.$this->_get_hostname()); + } + else + { + $this->_send_data('HELO '.$this->_get_hostname()); + } + + $resp = 250; + break; + case 'starttls' : + + $this->_send_data('STARTTLS'); + $resp = 220; + break; + case 'from' : + + $this->_send_data('MAIL FROM:<'.$data.'>'); + $resp = 250; + break; + case 'to' : + + if ($this->dsn) + { + $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data); + } + else + { + $this->_send_data('RCPT TO:<'.$data.'>'); + } + + $resp = 250; + break; + case 'data' : + + $this->_send_data('DATA'); + $resp = 354; + break; + case 'reset': + + $this->_send_data('RSET'); + $resp = 250; + break; + case 'quit' : + + $this->_send_data('QUIT'); + $resp = 221; + break; + } + + $reply = $this->_get_smtp_data(); + + $this->_debug_msg[] = '
'.$cmd.': '.$reply.'
'; + + if ((int) substr($reply, 0, 3) !== $resp) + { + $this->_set_error_message('lang:email_smtp_error', $reply); + return FALSE; + } + + if ($cmd === 'quit') + { + fclose($this->_smtp_connect); + } + + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * SMTP Authenticate + * + * @return bool + */ + protected function _smtp_authenticate() + { + if ( ! $this->_smtp_auth) + { + return TRUE; + } + + if ($this->smtp_user === '' && $this->smtp_pass === '') + { + $this->_set_error_message('lang:email_no_smtp_unpw'); + return FALSE; + } + + $this->_send_data('AUTH LOGIN'); + + $reply = $this->_get_smtp_data(); + + if (strpos($reply, '503') !== 0) // Already authenticated + return TRUE; + + if (strpos($reply, '334') !== 0) + { + $this->_set_error_message('lang:email_failed_smtp_login', $reply); + return FALSE; + } + + $this->_send_data(base64_encode($this->smtp_user)); + + $reply = $this->_get_smtp_data(); + + if (strpos($reply, '334') !== 0) + { + $this->_set_error_message('lang:email_smtp_auth_un', $reply); + return FALSE; + } + + $this->_send_data(base64_encode($this->smtp_pass)); + + $reply = $this->_get_smtp_data(); + + if (strpos($reply, '235') !== 0) + { + $this->_set_error_message('lang:email_smtp_auth_pw', $reply); + return FALSE; + } + + return TRUE; + } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 758f40c3d971837d739fa5bc47529bccc846dcfd Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Mon, 18 Feb 2013 17:18:51 +0530 Subject: Fixed curly braces. Removed redundant method set_useragent() --- system/libraries/Email.php | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) (limited to 'system/libraries/Email.php') diff --git a/system/libraries/Email.php b/system/libraries/Email.php index 7ea49a959..b0d0921eb 100644 --- a/system/libraries/Email.php +++ b/system/libraries/Email.php @@ -416,8 +416,10 @@ class CI_Email { */ public function __destruct() { - if(is_resource($this->_smtp_connect)) + if (is_resource($this->_smtp_connect)) + { $this->_send_command('quit'); + } } // -------------------------------------------------------------------- @@ -788,23 +790,6 @@ class CI_Email { return $this; } - // -------------------------------------------------------------------- - - /** - * Set Useragent - * - * @param string - * @return void - */ - public function set_useragent($type = '') - { - if( ! $type) - $this->useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'PHP/'.phpversion(); - else - $this->useragent = $type; - return $this; - } - // -------------------------------------------------------------------- /** @@ -1861,10 +1846,14 @@ class CI_Email { return FALSE; } - if($this->smtp_keepalive) + if ($this->smtp_keepalive) + { $this->_send_command('reset'); + } else + { $this->_send_command('quit'); + } return TRUE; } @@ -1879,7 +1868,7 @@ class CI_Email { */ protected function _smtp_connect($force=FALSE) { - if( ! is_resource($this->_smtp_connect) || $force) + if ( ! is_resource($this->_smtp_connect) || $force) { $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL; @@ -2026,7 +2015,9 @@ class CI_Email { $reply = $this->_get_smtp_data(); if (strpos($reply, '503') !== 0) // Already authenticated + { return TRUE; + } if (strpos($reply, '334') !== 0) { -- cgit v1.2.3-24-g4f1b From 193f0c76edbd85cf98ae730006b459c7c55a5b78 Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Mon, 18 Feb 2013 17:29:23 +0530 Subject: removed PR from the bug list --- system/libraries/Email.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/libraries/Email.php') diff --git a/system/libraries/Email.php b/system/libraries/Email.php index b0d0921eb..91c119e02 100644 --- a/system/libraries/Email.php +++ b/system/libraries/Email.php @@ -1868,7 +1868,7 @@ class CI_Email { */ protected function _smtp_connect($force=FALSE) { - if ( ! is_resource($this->_smtp_connect) || $force) + if ( ! is_resource($this->_smtp_connect) OR $force) { $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL; -- cgit v1.2.3-24-g4f1b From a44e6913324bd47e17bfd4109fc65b699c508006 Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Mon, 18 Feb 2013 18:07:03 +0530 Subject: Removed the unused $force paramter in Email::_smtp_connect() --- system/libraries/Email.php | 58 +++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 29 deletions(-) (limited to 'system/libraries/Email.php') diff --git a/system/libraries/Email.php b/system/libraries/Email.php index 91c119e02..1bf1da15e 100644 --- a/system/libraries/Email.php +++ b/system/libraries/Email.php @@ -1863,47 +1863,47 @@ class CI_Email { /** * SMTP Connect * - * @param bool * @return string */ - protected function _smtp_connect($force=FALSE) + protected function _smtp_connect() { - if ( ! is_resource($this->_smtp_connect) OR $force) + if (is_resource($this->_smtp_connect)) { - $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL; + return TRUE; + } - $this->_smtp_connect = fsockopen($ssl.$this->smtp_host, - $this->smtp_port, - $errno, - $errstr, - $this->smtp_timeout); + $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : NULL; - if ( ! is_resource($this->_smtp_connect)) - { - $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr); - return FALSE; - } + $this->_smtp_connect = fsockopen($ssl.$this->smtp_host, + $this->smtp_port, + $errno, + $errstr, + $this->smtp_timeout); - stream_set_timeout($this->_smtp_connect, $this->smtp_timeout); - $this->_set_error_message($this->_get_smtp_data()); + if ( ! is_resource($this->_smtp_connect)) + { + $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr); + return FALSE; + } - if ($this->smtp_crypto === 'tls') - { - $this->_send_command('hello'); - $this->_send_command('starttls'); + stream_set_timeout($this->_smtp_connect, $this->smtp_timeout); + $this->_set_error_message($this->_get_smtp_data()); - $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT); + if ($this->smtp_crypto === 'tls') + { + $this->_send_command('hello'); + $this->_send_command('starttls'); - if ($crypto !== TRUE) - { - $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data()); - return FALSE; - } - } + $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT); - return $this->_send_command('hello'); + if ($crypto !== TRUE) + { + $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data()); + return FALSE; + } } - return TRUE; + + return $this->_send_command('hello'); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b