From 3441a6dd54b3358f05b91383a672c44cf052adc5 Mon Sep 17 00:00:00 2001 From: Synchro Date: Thu, 12 Sep 2013 14:35:29 +0200 Subject: [PATCH] Increase timeout - fixes #104 Improve debug options --- changelog.md | 4 ++++ class.smtp.php | 39 +++++++++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/changelog.md b/changelog.md index 4caa7778a..1187fd674 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ # ChangeLog +* Increase timeout to match RFC2821 section 4.5.3.2 and thus not fail greetdelays, fixes #104 +* Add timestamps to default debug output +* Add connection events and new level 3 to debug output options + ## Version 5.2.7 (September 12th 2013) * Add Ukranian translation from @Krezalis * Support for do_verp diff --git a/class.smtp.php b/class.smtp.php index e6b45222d..6366724a7 100644 --- a/class.smtp.php +++ b/class.smtp.php @@ -74,7 +74,12 @@ class SMTP /** * Debug output level. - * Options: 0 for no output, 1 for commands, 2 for data and commands + * Options: + * 0: no output + * 1: commands + * 2: data and commands + * 3: as 2 plus connection status + * 4: low level data output * @type int */ public $do_debug = 0; @@ -93,10 +98,11 @@ class SMTP public $do_verp = false; /** - * The SMTP timeout value for reads, in seconds. + * The timeout value for connection, in seconds. + * Default of 5 minutes (300sec) is from RFC2821 section 4.5.3.2 * @type int */ - public $Timeout = 15; + public $Timeout = 300; /** * The SMTP timelimit value for reads, in seconds. @@ -164,8 +170,7 @@ protected function edebug($str) break; case 'echo': default: - //Just echoes whatever was received - echo $str; + echo gmdate('Y-m-d H:i:s')."\t".trim($str)."\n"; } } @@ -195,6 +200,10 @@ public function connect($host, $port = null, $timeout = 30, $options = array()) } // Connect to the SMTP server + if ($this->do_debug >= 3) { + $this->edebug('Connection: opening'); + } + $errno = 0; $errstr = ''; $socket_context = stream_context_create($options); @@ -217,12 +226,15 @@ public function connect($host, $port = null, $timeout = 30, $options = array()) ); if ($this->do_debug >= 1) { $this->edebug( - 'SMTP -> ERROR: ' . $this->error['error'] + 'SMTP ERROR: ' . $this->error['error'] . ": $errstr ($errno)" ); } return false; } + if ($this->do_debug >= 3) { + $this->edebug('Connection: opened'); + } // SMTP server can take longer to respond, give longer timeout for first read // Windows does not have support for this timeout function @@ -238,7 +250,7 @@ public function connect($host, $port = null, $timeout = 30, $options = array()) $announce = $this->get_lines(); if ($this->do_debug >= 2) { - $this->edebug('SMTP -> FROM SERVER:' . $announce); + $this->edebug('SERVER -> CLIENT: ' . $announce); } return true; @@ -437,7 +449,7 @@ public function connected() // the socket is valid but we are not connected if ($this->do_debug >= 1) { $this->edebug( - 'SMTP -> NOTICE: EOF caught while checking if connected' + 'SMTP NOTICE: EOF caught while checking if connected' ); } $this->close(); @@ -462,6 +474,9 @@ public function close() if (!empty($this->smtp_conn)) { // close the connection and cleanup fclose($this->smtp_conn); + if ($this->do_debug >= 3) { + $this->edebug('Connection: closed'); + } $this->smtp_conn = 0; } } @@ -691,7 +706,7 @@ protected function sendCommand($command, $commandstring, $expect) $code = substr($reply, 0, 3); if ($this->do_debug >= 2) { - $this->edebug('SMTP -> FROM SERVER:' . $reply); + $this->edebug('SERVER -> CLIENT: ' . $reply); } if (!in_array($code, (array)$expect)) { @@ -703,7 +718,7 @@ protected function sendCommand($command, $commandstring, $expect) ); if ($this->do_debug >= 1) { $this->edebug( - 'SMTP -> ERROR: ' . $this->error['error'] . ': ' . $reply + 'SMTP ERROR: ' . $this->error['error'] . ': ' . $reply ); } return false; @@ -769,7 +784,7 @@ public function turn() 'error' => 'The SMTP TURN command is not implemented' ); if ($this->do_debug >= 1) { - $this->edebug('SMTP -> NOTICE: ' . $this->error['error']); + $this->edebug('SMTP NOTICE: ' . $this->error['error']); } return false; } @@ -783,7 +798,7 @@ public function turn() public function client_send($data) { if ($this->do_debug >= 1) { - $this->edebug("CLIENT -> SMTP: $data"); + $this->edebug("CLIENT -> SERVER: $data"); } return fwrite($this->smtp_conn, $data); }