These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * PHPMailer RFC821 SMTP email transport class. |
||
4 | * PHP Version 5.5 |
||
5 | * |
||
6 | * @package PHPMailer |
||
7 | * @see https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project |
||
8 | * @author Marcus Bointon (Synchro/coolbru) <[email protected]> |
||
9 | * @author Jim Jagielski (jimjag) <[email protected]> |
||
10 | * @author Andy Prevost (codeworxtech) <[email protected]> |
||
11 | * @author Brent R. Matzelle (original founder) |
||
12 | * @copyright 2012 - 2016 Marcus Bointon |
||
13 | * @copyright 2010 - 2012 Jim Jagielski |
||
14 | * @copyright 2004 - 2009 Andy Prevost |
||
15 | * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
||
16 | * @note This program is distributed in the hope that it will be useful - WITHOUT |
||
17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
||
18 | * FITNESS FOR A PARTICULAR PURPOSE. |
||
19 | */ |
||
20 | |||
21 | namespace PHPMailer\PHPMailer; |
||
22 | |||
23 | /** |
||
24 | * PHPMailer RFC821 SMTP email transport class. |
||
25 | * Implements RFC 821 SMTP commands and provides some utility methods for sending mail to an SMTP server. |
||
26 | * |
||
27 | * @package PHPMailer |
||
28 | * @author Chris Ryan |
||
29 | * @author Marcus Bointon <[email protected]> |
||
30 | */ |
||
31 | class SMTP |
||
32 | { |
||
33 | /** |
||
34 | * The PHPMailer SMTP version number. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | const VERSION = '6.0.0'; |
||
39 | |||
40 | /** |
||
41 | * SMTP line break constant. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | const LE = "\r\n"; |
||
46 | |||
47 | /** |
||
48 | * The SMTP port to use if one is not specified. |
||
49 | * |
||
50 | * @var integer |
||
51 | */ |
||
52 | const DEFAULT_PORT = 25; |
||
53 | |||
54 | /** |
||
55 | * The maximum line length allowed by RFC 2822 section 2.1.1 |
||
56 | * |
||
57 | * @var integer |
||
58 | */ |
||
59 | const MAX_LINE_LENGTH = 998; |
||
60 | |||
61 | /** |
||
62 | * Debug level for no output |
||
63 | */ |
||
64 | const DEBUG_OFF = 0; |
||
65 | |||
66 | /** |
||
67 | * Debug level to show client -> server messages |
||
68 | */ |
||
69 | const DEBUG_CLIENT = 1; |
||
70 | |||
71 | /** |
||
72 | * Debug level to show client -> server and server -> client messages |
||
73 | */ |
||
74 | const DEBUG_SERVER = 2; |
||
75 | |||
76 | /** |
||
77 | * Debug level to show connection status, client -> server and server -> client messages |
||
78 | */ |
||
79 | const DEBUG_CONNECTION = 3; |
||
80 | |||
81 | /** |
||
82 | * Debug level to show all messages |
||
83 | */ |
||
84 | const DEBUG_LOWLEVEL = 4; |
||
85 | |||
86 | /** |
||
87 | * Debug output level. |
||
88 | * Options: |
||
89 | * * self::DEBUG_OFF (`0`) No debug output, default |
||
90 | * * self::DEBUG_CLIENT (`1`) Client commands |
||
91 | * * self::DEBUG_SERVER (`2`) Client commands and server responses |
||
92 | * * self::DEBUG_CONNECTION (`3`) As DEBUG_SERVER plus connection status |
||
93 | * * self::DEBUG_LOWLEVEL (`4`) Low-level data output, all messages |
||
94 | * |
||
95 | * @var integer |
||
96 | */ |
||
97 | public $do_debug = self::DEBUG_OFF; |
||
98 | |||
99 | /** |
||
100 | * How to handle debug output. |
||
101 | * Options: |
||
102 | * * `echo` Output plain-text as-is, appropriate for CLI |
||
103 | * * `html` Output escaped, line breaks converted to `<br>`, appropriate for browser output |
||
104 | * * `error_log` Output to error log as configured in php.ini |
||
105 | * Alternatively, you can provide a callable expecting two params: a message string and the debug level: |
||
106 | * <code> |
||
107 | * $smtp->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; |
||
108 | * </code> |
||
109 | * Alternatively, you can pass in an instance of a PSR-3 compatible logger, though only `debug` |
||
110 | * level output is used: |
||
111 | * <code> |
||
112 | * $mail->Debugoutput = new myPsr3Logger; |
||
113 | * </code> |
||
114 | * |
||
115 | * @var string|callable|Psr\Log\LoggerInterface |
||
116 | */ |
||
117 | public $Debugoutput = 'echo'; |
||
118 | |||
119 | /** |
||
120 | * Whether to use VERP. |
||
121 | * |
||
122 | * @see http://en.wikipedia.org/wiki/Variable_envelope_return_path |
||
123 | * @see http://www.postfix.org/VERP_README.html Info on VERP |
||
124 | * @var boolean |
||
125 | */ |
||
126 | public $do_verp = false; |
||
127 | |||
128 | /** |
||
129 | * The timeout value for connection, in seconds. |
||
130 | * Default of 5 minutes (300sec) is from RFC2821 section 4.5.3.2 |
||
131 | * This needs to be quite high to function correctly with hosts using greetdelay as an anti-spam measure. |
||
132 | * |
||
133 | * @see http://tools.ietf.org/html/rfc2821#section-4.5.3.2 |
||
134 | * @var integer |
||
135 | */ |
||
136 | public $Timeout = 300; |
||
137 | |||
138 | /** |
||
139 | * How long to wait for commands to complete, in seconds. |
||
140 | * Default of 5 minutes (300sec) is from RFC2821 section 4.5.3.2 |
||
141 | * |
||
142 | * @var integer |
||
143 | */ |
||
144 | public $Timelimit = 300; |
||
145 | |||
146 | /** |
||
147 | * @var array Patterns to extract an SMTP transaction id from reply to a DATA command. |
||
148 | * The first capture group in each regex will be used as the ID. |
||
149 | * MS ESMTP returns the message ID, which may not be correct for internal tracking. |
||
150 | */ |
||
151 | protected $smtp_transaction_id_patterns = [ |
||
152 | 'exim' => '/[0-9]{3} OK id=(.*)/', |
||
153 | 'sendmail' => '/[0-9]{3} 2.0.0 (.*) Message/', |
||
154 | 'postfix' => '/[0-9]{3} 2.0.0 Ok: queued as (.*)/', |
||
155 | 'Microsoft_ESMTP' => '/[0-9]{3} 2.[0-9].0 (.*)@(?:.*) Queued mail for delivery/' |
||
156 | ]; |
||
157 | |||
158 | /** |
||
159 | * @var string The last transaction ID issued in response to a DATA command, |
||
160 | * if one was detected |
||
161 | */ |
||
162 | protected $last_smtp_transaction_id; |
||
163 | |||
164 | /** |
||
165 | * The socket for the server connection. |
||
166 | * |
||
167 | * @var resource |
||
168 | */ |
||
169 | protected $smtp_conn; |
||
170 | |||
171 | /** |
||
172 | * Error information, if any, for the last SMTP command. |
||
173 | * |
||
174 | * @var array |
||
175 | */ |
||
176 | protected $error = [ |
||
177 | 'error' => '', |
||
178 | 'detail' => '', |
||
179 | 'smtp_code' => '', |
||
180 | 'smtp_code_ex' => '' |
||
181 | ]; |
||
182 | |||
183 | /** |
||
184 | * The reply the server sent to us for HELO. |
||
185 | * If null, no HELO string has yet been received. |
||
186 | * |
||
187 | * @var string|null |
||
188 | */ |
||
189 | protected $helo_rply = null; |
||
190 | |||
191 | /** |
||
192 | * The set of SMTP extensions sent in reply to EHLO command. |
||
193 | * Indexes of the array are extension names. |
||
194 | * Value at index 'HELO' or 'EHLO' (according to command that was sent) |
||
195 | * represents the server name. In case of HELO it is the only element of the array. |
||
196 | * Other values can be boolean TRUE or an array containing extension options. |
||
197 | * If null, no HELO/EHLO string has yet been received. |
||
198 | * |
||
199 | * @var array|null |
||
200 | */ |
||
201 | protected $server_caps = null; |
||
202 | |||
203 | /** |
||
204 | * The most recent reply received from the server. |
||
205 | * |
||
206 | * @var string |
||
207 | */ |
||
208 | protected $last_reply = ''; |
||
209 | |||
210 | /** |
||
211 | * Output debugging info via a user-selected method. |
||
212 | * |
||
213 | * @param string $str Debug string to output |
||
214 | * @param integer $level The debug level of this message; see DEBUG_* constants |
||
215 | * |
||
216 | * @see SMTP::$Debugoutput |
||
217 | * @see SMTP::$do_debug |
||
218 | */ |
||
219 | protected function edebug($str, $level = 0) |
||
220 | { |
||
221 | if ($level > $this->do_debug) { |
||
222 | return; |
||
223 | } |
||
224 | //Is this a PSR-3 logger? |
||
225 | if (is_a($this->Debugoutput, 'Psr\Log\LoggerInterface')) { |
||
226 | $this->Debugoutput->debug($str); |
||
227 | return; |
||
228 | } |
||
229 | //Avoid clash with built-in function names |
||
230 | if (!in_array($this->Debugoutput, ['error_log', 'html', 'echo']) and is_callable($this->Debugoutput)) { |
||
231 | call_user_func($this->Debugoutput, $str, $level); |
||
232 | return; |
||
233 | } |
||
234 | switch ($this->Debugoutput) { |
||
235 | case 'error_log': |
||
236 | //Don't output, just log |
||
237 | error_log($str); |
||
238 | break; |
||
239 | case 'html': |
||
240 | //Cleans up output a bit for a better looking, HTML-safe output |
||
241 | echo gmdate('Y-m-d H:i:s'), ' ', htmlentities( |
||
242 | preg_replace('/[\r\n]+/', '', $str), |
||
243 | ENT_QUOTES, |
||
244 | 'UTF-8' |
||
245 | ), "<br>\n"; |
||
246 | break; |
||
247 | case 'echo': |
||
248 | default: |
||
249 | //Normalize line breaks |
||
250 | $str = preg_replace('/\r\n|\r/ms', "\n", $str); |
||
251 | echo gmdate('Y-m-d H:i:s'), |
||
252 | "\t", |
||
253 | //Trim trailing space |
||
254 | trim( |
||
255 | //Indent for readability, except for trailing break |
||
256 | str_replace( |
||
257 | "\n", |
||
258 | "\n \t ", |
||
259 | trim($str) |
||
260 | ) |
||
261 | ), |
||
262 | "\n"; |
||
263 | } |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Connect to an SMTP server. |
||
268 | * |
||
269 | * @param string $host SMTP server IP or host name |
||
270 | * @param integer $port The port number to connect to |
||
271 | * @param integer $timeout How long to wait for the connection to open |
||
272 | * @param array $options An array of options for stream_context_create() |
||
273 | * |
||
274 | * @return boolean |
||
275 | */ |
||
276 | public function connect($host, $port = null, $timeout = 30, $options = []) |
||
277 | { |
||
278 | static $streamok; |
||
279 | //This is enabled by default since 5.0.0 but some providers disable it |
||
280 | //Check this once and cache the result |
||
281 | if (is_null($streamok)) { |
||
282 | $streamok = function_exists('stream_socket_client'); |
||
283 | } |
||
284 | // Clear errors to avoid confusion |
||
285 | $this->setError(''); |
||
286 | // Make sure we are __not__ connected |
||
287 | if ($this->connected()) { |
||
288 | // Already connected, generate error |
||
289 | $this->setError('Already connected to a server'); |
||
290 | return false; |
||
291 | } |
||
292 | if (empty($port)) { |
||
293 | $port = self::DEFAULT_PORT; |
||
294 | } |
||
295 | // Connect to the SMTP server |
||
296 | $this->edebug( |
||
297 | "Connection: opening to $host:$port, timeout=$timeout, options=" . |
||
298 | (count($options) > 0 ? var_export($options, true): 'array()'), |
||
299 | self::DEBUG_CONNECTION |
||
300 | ); |
||
301 | $errno = 0; |
||
302 | $errstr = ''; |
||
303 | if ($streamok) { |
||
304 | $socket_context = stream_context_create($options); |
||
305 | set_error_handler([$this, 'errorHandler']); |
||
306 | $this->smtp_conn = stream_socket_client( |
||
307 | $host . ":" . $port, |
||
308 | $errno, |
||
309 | $errstr, |
||
310 | $timeout, |
||
311 | STREAM_CLIENT_CONNECT, |
||
312 | $socket_context |
||
313 | ); |
||
314 | restore_error_handler(); |
||
315 | } else { |
||
316 | //Fall back to fsockopen which should work in more places, but is missing some features |
||
317 | $this->edebug( |
||
318 | "Connection: stream_socket_client not available, falling back to fsockopen", |
||
319 | self::DEBUG_CONNECTION |
||
320 | ); |
||
321 | set_error_handler([$this, 'errorHandler']); |
||
322 | $this->smtp_conn = fsockopen( |
||
323 | $host, |
||
324 | $port, |
||
325 | $errno, |
||
326 | $errstr, |
||
327 | $timeout |
||
328 | ); |
||
329 | restore_error_handler(); |
||
330 | } |
||
331 | // Verify we connected properly |
||
332 | if (!is_resource($this->smtp_conn)) { |
||
333 | $this->setError( |
||
334 | 'Failed to connect to server', |
||
335 | $errno, |
||
336 | $errstr |
||
337 | ); |
||
338 | $this->edebug( |
||
339 | 'SMTP ERROR: ' . $this->error['error'] |
||
340 | . ": $errstr ($errno)", |
||
341 | self::DEBUG_CLIENT |
||
342 | ); |
||
343 | return false; |
||
344 | } |
||
345 | $this->edebug('Connection: opened', self::DEBUG_CONNECTION); |
||
346 | // SMTP server can take longer to respond, give longer timeout for first read |
||
347 | // Windows does not have support for this timeout function |
||
348 | if (substr(PHP_OS, 0, 3) != 'WIN') { |
||
349 | $max = ini_get('max_execution_time'); |
||
350 | // Don't bother if unlimited |
||
351 | if (0 != $max and $timeout > $max) { |
||
352 | @set_time_limit($timeout); |
||
353 | } |
||
354 | stream_set_timeout($this->smtp_conn, $timeout, 0); |
||
355 | } |
||
356 | // Get any announcement |
||
357 | $announce = $this->get_lines(); |
||
358 | $this->edebug('SERVER -> CLIENT: ' . $announce, self::DEBUG_SERVER); |
||
359 | return true; |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Initiate a TLS (encrypted) session. |
||
364 | * |
||
365 | * @return boolean |
||
366 | */ |
||
367 | public function startTLS() |
||
368 | { |
||
369 | if (!$this->sendCommand('STARTTLS', 'STARTTLS', 220)) { |
||
370 | return false; |
||
371 | } |
||
372 | |||
373 | //Allow the best TLS version(s) we can |
||
374 | $crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT; |
||
375 | |||
376 | //PHP 5.6.7 dropped inclusion of TLS 1.1 and 1.2 in STREAM_CRYPTO_METHOD_TLS_CLIENT |
||
377 | //so add them back in manually if we can |
||
378 | if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) { |
||
379 | $crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT; |
||
380 | $crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT; |
||
381 | } |
||
382 | |||
383 | // Begin encrypted connection |
||
384 | set_error_handler([$this, 'errorHandler']); |
||
385 | $crypto_ok = stream_socket_enable_crypto( |
||
386 | $this->smtp_conn, |
||
387 | true, |
||
388 | $crypto_method |
||
389 | ); |
||
390 | restore_error_handler(); |
||
391 | return (boolean)$crypto_ok; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Perform SMTP authentication. |
||
396 | * Must be run after hello(). |
||
397 | * |
||
398 | * @param string $username The user name |
||
399 | * @param string $password The password |
||
400 | * @param string $authtype The auth type (CRAM-MD5, PLAIN, LOGIN, XOAUTH2) |
||
401 | * @param OAuth $OAuth An optional OAuth instance for XOAUTH2 authentication |
||
402 | * |
||
403 | * @return boolean True if successfully authenticated. |
||
404 | * @see hello() |
||
405 | */ |
||
406 | public function authenticate( |
||
407 | $username, |
||
408 | $password, |
||
409 | $authtype = null, |
||
410 | $OAuth = null |
||
411 | ) { |
||
412 | if (!$this->server_caps) { |
||
413 | $this->setError('Authentication is not allowed before HELO/EHLO'); |
||
414 | return false; |
||
415 | } |
||
416 | |||
417 | if (array_key_exists('EHLO', $this->server_caps)) { |
||
418 | // SMTP extensions are available; try to find a proper authentication method |
||
419 | if (!array_key_exists('AUTH', $this->server_caps)) { |
||
420 | $this->setError('Authentication is not allowed at this stage'); |
||
421 | // 'at this stage' means that auth may be allowed after the stage changes |
||
422 | // e.g. after STARTTLS |
||
423 | return false; |
||
424 | } |
||
425 | |||
426 | $this->edebug('Auth method requested: ' . ($authtype ? $authtype : 'UNKNOWN'), self::DEBUG_LOWLEVEL); |
||
427 | $this->edebug( |
||
428 | 'Auth methods available on the server: ' . implode(',', $this->server_caps['AUTH']), |
||
429 | self::DEBUG_LOWLEVEL |
||
430 | ); |
||
431 | |||
432 | //If we have requested a specific auth type, check the server supports it before trying others |
||
433 | if (!in_array($authtype, $this->server_caps['AUTH'])) { |
||
434 | $this->edebug('Requested auth method not available: ' . $authtype, self::DEBUG_LOWLEVEL); |
||
435 | $authtype = null; |
||
436 | } |
||
437 | |||
438 | if (empty($authtype)) { |
||
439 | //If no auth mechanism is specified, attempt to use these, in this order |
||
440 | //Try CRAM-MD5 first as it's more secure than the others |
||
441 | foreach (['CRAM-MD5', 'LOGIN', 'PLAIN', 'XOAUTH2'] as $method) { |
||
442 | if (in_array($method, $this->server_caps['AUTH'])) { |
||
443 | $authtype = $method; |
||
444 | break; |
||
445 | } |
||
446 | } |
||
447 | if (empty($authtype)) { |
||
448 | $this->setError('No supported authentication methods found'); |
||
449 | return false; |
||
450 | } |
||
451 | self::edebug('Auth method selected: ' . $authtype, self::DEBUG_LOWLEVEL); |
||
452 | } |
||
453 | |||
454 | if (!in_array($authtype, $this->server_caps['AUTH'])) { |
||
455 | $this->setError("The requested authentication method \"$authtype\" is not supported by the server"); |
||
456 | return false; |
||
457 | } |
||
458 | } elseif (empty($authtype)) { |
||
459 | $authtype = 'LOGIN'; |
||
460 | } |
||
461 | switch ($authtype) { |
||
462 | case 'PLAIN': |
||
463 | // Start authentication |
||
464 | if (!$this->sendCommand('AUTH', 'AUTH PLAIN', 334)) { |
||
465 | return false; |
||
466 | } |
||
467 | // Send encoded username and password |
||
468 | if (!$this->sendCommand( |
||
469 | 'User & Password', |
||
470 | base64_encode("\0" . $username . "\0" . $password), |
||
471 | 235 |
||
472 | ) |
||
473 | ) { |
||
474 | return false; |
||
475 | } |
||
476 | break; |
||
477 | case 'LOGIN': |
||
478 | // Start authentication |
||
479 | if (!$this->sendCommand('AUTH', 'AUTH LOGIN', 334)) { |
||
480 | return false; |
||
481 | } |
||
482 | if (!$this->sendCommand("Username", base64_encode($username), 334)) { |
||
483 | return false; |
||
484 | } |
||
485 | if (!$this->sendCommand("Password", base64_encode($password), 235)) { |
||
486 | return false; |
||
487 | } |
||
488 | break; |
||
489 | case 'CRAM-MD5': |
||
490 | // Start authentication |
||
491 | if (!$this->sendCommand('AUTH CRAM-MD5', 'AUTH CRAM-MD5', 334)) { |
||
492 | return false; |
||
493 | } |
||
494 | // Get the challenge |
||
495 | $challenge = base64_decode(substr($this->last_reply, 4)); |
||
496 | |||
497 | // Build the response |
||
498 | $response = $username . ' ' . $this->hmac($challenge, $password); |
||
499 | |||
500 | // send encoded credentials |
||
501 | return $this->sendCommand('Username', base64_encode($response), 235); |
||
502 | case 'XOAUTH2': |
||
503 | //The OAuth instance must be set up prior to requesting auth. |
||
504 | if (is_null($OAuth)) { |
||
505 | return false; |
||
506 | } |
||
507 | $oauth = $OAuth->getOauth64(); |
||
508 | |||
509 | // Start authentication |
||
510 | if (!$this->sendCommand('AUTH', 'AUTH XOAUTH2 ' . $oauth, 235)) { |
||
511 | return false; |
||
512 | } |
||
513 | break; |
||
514 | default: |
||
515 | $this->setError("Authentication method \"$authtype\" is not supported"); |
||
516 | return false; |
||
517 | } |
||
518 | return true; |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * Calculate an MD5 HMAC hash. |
||
523 | * Works like hash_hmac('md5', $data, $key) |
||
524 | * in case that function is not available |
||
525 | * |
||
526 | * @param string $data The data to hash |
||
527 | * @param string $key The key to hash with |
||
528 | * |
||
529 | * @return string |
||
530 | */ |
||
531 | protected function hmac($data, $key) |
||
532 | { |
||
533 | if (function_exists('hash_hmac')) { |
||
534 | return hash_hmac('md5', $data, $key); |
||
535 | } |
||
536 | |||
537 | // The following borrowed from |
||
538 | // http://php.net/manual/en/function.mhash.php#27225 |
||
539 | |||
540 | // RFC 2104 HMAC implementation for php. |
||
541 | // Creates an md5 HMAC. |
||
542 | // Eliminates the need to install mhash to compute a HMAC |
||
543 | // by Lance Rushing |
||
544 | |||
545 | $bytelen = 64; // byte length for md5 |
||
546 | if (strlen($key) > $bytelen) { |
||
547 | $key = pack('H*', md5($key)); |
||
548 | } |
||
549 | $key = str_pad($key, $bytelen, chr(0x00)); |
||
550 | $ipad = str_pad('', $bytelen, chr(0x36)); |
||
551 | $opad = str_pad('', $bytelen, chr(0x5c)); |
||
552 | $k_ipad = $key ^ $ipad; |
||
553 | $k_opad = $key ^ $opad; |
||
554 | |||
555 | return md5($k_opad . pack('H*', md5($k_ipad . $data))); |
||
556 | } |
||
557 | |||
558 | /** |
||
559 | * Check connection state. |
||
560 | * |
||
561 | * @return boolean True if connected. |
||
562 | */ |
||
563 | public function connected() |
||
564 | { |
||
565 | if (is_resource($this->smtp_conn)) { |
||
566 | $sock_status = stream_get_meta_data($this->smtp_conn); |
||
567 | if ($sock_status['eof']) { |
||
568 | // The socket is valid but we are not connected |
||
569 | $this->edebug( |
||
570 | 'SMTP NOTICE: EOF caught while checking if connected', |
||
571 | self::DEBUG_CLIENT |
||
572 | ); |
||
573 | $this->close(); |
||
574 | return false; |
||
575 | } |
||
576 | return true; // everything looks good |
||
577 | } |
||
578 | return false; |
||
579 | } |
||
580 | |||
581 | /** |
||
582 | * Close the socket and clean up the state of the class. |
||
583 | * Don't use this function without first trying to use QUIT. |
||
584 | * |
||
585 | * @see quit() |
||
586 | */ |
||
587 | public function close() |
||
588 | { |
||
589 | $this->setError(''); |
||
590 | $this->server_caps = null; |
||
591 | $this->helo_rply = null; |
||
592 | if (is_resource($this->smtp_conn)) { |
||
593 | // close the connection and cleanup |
||
594 | fclose($this->smtp_conn); |
||
595 | $this->smtp_conn = null; //Makes for cleaner serialization |
||
596 | $this->edebug('Connection: closed', self::DEBUG_CONNECTION); |
||
597 | } |
||
598 | } |
||
599 | |||
600 | /** |
||
601 | * Send an SMTP DATA command. |
||
602 | * Issues a data command and sends the msg_data to the server, |
||
603 | * finializing the mail transaction. $msg_data is the message |
||
604 | * that is to be send with the headers. Each header needs to be |
||
605 | * on a single line followed by a <CRLF> with the message headers |
||
606 | * and the message body being separated by an additional <CRLF>. |
||
607 | * Implements RFC 821: DATA <CRLF> |
||
608 | * |
||
609 | * @param string $msg_data Message data to send |
||
610 | * |
||
611 | * @return boolean |
||
612 | */ |
||
613 | public function data($msg_data) |
||
614 | { |
||
615 | //This will use the standard timelimit |
||
616 | if (!$this->sendCommand('DATA', 'DATA', 354)) { |
||
617 | return false; |
||
618 | } |
||
619 | |||
620 | /* The server is ready to accept data! |
||
621 | * According to rfc821 we should not send more than 1000 characters on a single line (including the LE) |
||
622 | * so we will break the data up into lines by \r and/or \n then if needed we will break each of those into |
||
623 | * smaller lines to fit within the limit. |
||
624 | * We will also look for lines that start with a '.' and prepend an additional '.'. |
||
625 | * NOTE: this does not count towards line-length limit. |
||
626 | */ |
||
627 | |||
628 | // Normalize line breaks before exploding |
||
629 | $lines = explode("\n", str_replace(["\r\n", "\r"], "\n", $msg_data)); |
||
630 | |||
631 | /* To distinguish between a complete RFC822 message and a plain message body, we check if the first field |
||
632 | * of the first line (':' separated) does not contain a space then it _should_ be a header and we will |
||
633 | * process all lines before a blank line as headers. |
||
634 | */ |
||
635 | |||
636 | $field = substr($lines[0], 0, strpos($lines[0], ':')); |
||
637 | $in_headers = false; |
||
638 | if (!empty($field) and strpos($field, ' ') === false) { |
||
639 | $in_headers = true; |
||
640 | } |
||
641 | |||
642 | foreach ($lines as $line) { |
||
643 | $lines_out = []; |
||
644 | if ($in_headers and $line == '') { |
||
645 | $in_headers = false; |
||
646 | } |
||
647 | //Break this line up into several smaller lines if it's too long |
||
648 | //Micro-optimisation: isset($str[$len]) is faster than (strlen($str) > $len), |
||
649 | while (isset($line[self::MAX_LINE_LENGTH])) { |
||
650 | //Working backwards, try to find a space within the last MAX_LINE_LENGTH chars of the line to break on |
||
651 | //so as to avoid breaking in the middle of a word |
||
652 | $pos = strrpos(substr($line, 0, self::MAX_LINE_LENGTH), ' '); |
||
653 | //Deliberately matches both false and 0 |
||
654 | if (!$pos) { |
||
655 | //No nice break found, add a hard break |
||
656 | $pos = self::MAX_LINE_LENGTH - 1; |
||
657 | $lines_out[] = substr($line, 0, $pos); |
||
658 | $line = substr($line, $pos); |
||
659 | } else { |
||
660 | //Break at the found point |
||
661 | $lines_out[] = substr($line, 0, $pos); |
||
662 | //Move along by the amount we dealt with |
||
663 | $line = substr($line, $pos + 1); |
||
664 | } |
||
665 | //If processing headers add a LWSP-char to the front of new line RFC822 section 3.1.1 |
||
666 | if ($in_headers) { |
||
667 | $line = "\t" . $line; |
||
668 | } |
||
669 | } |
||
670 | $lines_out[] = $line; |
||
671 | |||
672 | //Send the lines to the server |
||
673 | foreach ($lines_out as $line_out) { |
||
674 | //RFC2821 section 4.5.2 |
||
675 | if (!empty($line_out) and $line_out[0] == '.') { |
||
676 | $line_out = '.' . $line_out; |
||
677 | } |
||
678 | $this->client_send($line_out . static::LE); |
||
679 | } |
||
680 | } |
||
681 | |||
682 | //Message data has been sent, complete the command |
||
683 | //Increase timelimit for end of DATA command |
||
684 | $savetimelimit = $this->Timelimit; |
||
685 | $this->Timelimit = $this->Timelimit * 2; |
||
686 | $result = $this->sendCommand('DATA END', '.', 250); |
||
687 | $this->recordLastTransactionID(); |
||
688 | //Restore timelimit |
||
689 | $this->Timelimit = $savetimelimit; |
||
690 | return $result; |
||
691 | } |
||
692 | |||
693 | /** |
||
694 | * Send an SMTP HELO or EHLO command. |
||
695 | * Used to identify the sending server to the receiving server. |
||
696 | * This makes sure that client and server are in a known state. |
||
697 | * Implements RFC 821: HELO <SP> <domain> <CRLF> |
||
698 | * and RFC 2821 EHLO. |
||
699 | * |
||
700 | * @param string $host The host name or IP to connect to |
||
701 | * |
||
702 | * @return boolean |
||
703 | */ |
||
704 | public function hello($host = '') |
||
705 | { |
||
706 | //Try extended hello first (RFC 2821) |
||
707 | return (boolean)($this->sendHello('EHLO', $host) or $this->sendHello('HELO', $host)); |
||
708 | } |
||
709 | |||
710 | /** |
||
711 | * Send an SMTP HELO or EHLO command. |
||
712 | * Low-level implementation used by hello() |
||
713 | * |
||
714 | * @param string $hello The HELO string |
||
715 | * @param string $host The hostname to say we are |
||
716 | * |
||
717 | * @return boolean |
||
718 | * @see hello() |
||
719 | */ |
||
720 | protected function sendHello($hello, $host) |
||
721 | { |
||
722 | $noerror = $this->sendCommand($hello, $hello . ' ' . $host, 250); |
||
723 | $this->helo_rply = $this->last_reply; |
||
724 | if ($noerror) { |
||
725 | $this->parseHelloFields($hello); |
||
726 | } else { |
||
727 | $this->server_caps = null; |
||
728 | } |
||
729 | return $noerror; |
||
730 | } |
||
731 | |||
732 | /** |
||
733 | * Parse a reply to HELO/EHLO command to discover server extensions. |
||
734 | * In case of HELO, the only parameter that can be discovered is a server name. |
||
735 | * |
||
736 | * @param string $type `HELO` or `EHLO` |
||
737 | */ |
||
738 | protected function parseHelloFields($type) |
||
739 | { |
||
740 | $this->server_caps = []; |
||
741 | $lines = explode("\n", $this->helo_rply); |
||
742 | |||
743 | foreach ($lines as $n => $s) { |
||
744 | //First 4 chars contain response code followed by - or space |
||
745 | $s = trim(substr($s, 4)); |
||
746 | if (empty($s)) { |
||
747 | continue; |
||
748 | } |
||
749 | $fields = explode(' ', $s); |
||
750 | if (!empty($fields)) { |
||
751 | if (!$n) { |
||
752 | $name = $type; |
||
753 | $fields = $fields[0]; |
||
754 | } else { |
||
755 | $name = array_shift($fields); |
||
756 | switch ($name) { |
||
757 | case 'SIZE': |
||
758 | $fields = ($fields ? $fields[0] : 0); |
||
759 | break; |
||
760 | case 'AUTH': |
||
761 | if (!is_array($fields)) { |
||
762 | $fields = []; |
||
763 | } |
||
764 | break; |
||
765 | default: |
||
766 | $fields = true; |
||
767 | } |
||
768 | } |
||
769 | $this->server_caps[$name] = $fields; |
||
770 | } |
||
771 | } |
||
772 | } |
||
773 | |||
774 | /** |
||
775 | * Send an SMTP MAIL command. |
||
776 | * Starts a mail transaction from the email address specified in |
||
777 | * $from. Returns true if successful or false otherwise. If True |
||
778 | * the mail transaction is started and then one or more recipient |
||
779 | * commands may be called followed by a data command. |
||
780 | * Implements RFC 821: MAIL <SP> FROM:<reverse-path> <CRLF> |
||
781 | * |
||
782 | * @param string $from Source address of this message |
||
783 | * |
||
784 | * @return boolean |
||
785 | */ |
||
786 | public function mail($from) |
||
787 | { |
||
788 | $useVerp = ($this->do_verp ? ' XVERP' : ''); |
||
789 | return $this->sendCommand( |
||
790 | 'MAIL FROM', |
||
791 | 'MAIL FROM:<' . $from . '>' . $useVerp, |
||
792 | 250 |
||
793 | ); |
||
794 | } |
||
795 | |||
796 | /** |
||
797 | * Send an SMTP QUIT command. |
||
798 | * Closes the socket if there is no error or the $close_on_error argument is true. |
||
799 | * Implements from RFC 821: QUIT <CRLF> |
||
800 | * |
||
801 | * @param boolean $close_on_error Should the connection close if an error occurs? |
||
802 | * |
||
803 | * @return boolean |
||
804 | */ |
||
805 | public function quit($close_on_error = true) |
||
806 | { |
||
807 | $noerror = $this->sendCommand('QUIT', 'QUIT', 221); |
||
808 | $err = $this->error; //Save any error |
||
809 | if ($noerror or $close_on_error) { |
||
810 | $this->close(); |
||
811 | $this->error = $err; //Restore any error from the quit command |
||
812 | } |
||
813 | return $noerror; |
||
814 | } |
||
815 | |||
816 | /** |
||
817 | * Send an SMTP RCPT command. |
||
818 | * Sets the TO argument to $toaddr. |
||
819 | * Returns true if the recipient was accepted false if it was rejected. |
||
820 | * Implements from RFC 821: RCPT <SP> TO:<forward-path> <CRLF> |
||
821 | * |
||
822 | * @param string $address The address the message is being sent to |
||
823 | * |
||
824 | * @return boolean |
||
825 | */ |
||
826 | public function recipient($address) |
||
827 | { |
||
828 | return $this->sendCommand( |
||
829 | 'RCPT TO', |
||
830 | 'RCPT TO:<' . $address . '>', |
||
831 | [250, 251] |
||
832 | ); |
||
833 | } |
||
834 | |||
835 | /** |
||
836 | * Send an SMTP RSET command. |
||
837 | * Abort any transaction that is currently in progress. |
||
838 | * Implements RFC 821: RSET <CRLF> |
||
839 | * |
||
840 | * @return boolean True on success. |
||
841 | */ |
||
842 | public function reset() |
||
843 | { |
||
844 | return $this->sendCommand('RSET', 'RSET', 250); |
||
845 | } |
||
846 | |||
847 | /** |
||
848 | * Send a command to an SMTP server and check its return code. |
||
849 | * |
||
850 | * @param string $command The command name - not sent to the server |
||
851 | * @param string $commandstring The actual command to send |
||
852 | * @param integer|array $expect One or more expected integer success codes |
||
853 | * |
||
854 | * @return boolean True on success. |
||
855 | */ |
||
856 | protected function sendCommand($command, $commandstring, $expect) |
||
857 | { |
||
858 | if (!$this->connected()) { |
||
859 | $this->setError("Called $command without being connected"); |
||
860 | return false; |
||
861 | } |
||
862 | //Reject line breaks in all commands |
||
863 | if (strpos($commandstring, "\n") !== false or strpos($commandstring, "\r") !== false) { |
||
864 | $this->setError("Command '$command' contained line breaks"); |
||
865 | return false; |
||
866 | } |
||
867 | $this->client_send($commandstring . static::LE); |
||
868 | |||
869 | $this->last_reply = $this->get_lines(); |
||
870 | // Fetch SMTP code and possible error code explanation |
||
871 | $matches = []; |
||
872 | if (preg_match("/^([0-9]{3})[ -](?:([0-9]\\.[0-9]\\.[0-9]) )?/", $this->last_reply, $matches)) { |
||
873 | $code = $matches[1]; |
||
874 | $code_ex = (count($matches) > 2 ? $matches[2] : null); |
||
875 | // Cut off error code from each response line |
||
876 | $detail = preg_replace( |
||
877 | "/{$code}[ -]" . |
||
878 | ($code_ex ? str_replace('.', '\\.', $code_ex) . ' ' : '') . "/m", |
||
879 | '', |
||
880 | $this->last_reply |
||
881 | ); |
||
882 | } else { |
||
883 | // Fall back to simple parsing if regex fails |
||
884 | $code = substr($this->last_reply, 0, 3); |
||
885 | $code_ex = null; |
||
886 | $detail = substr($this->last_reply, 4); |
||
887 | } |
||
888 | |||
889 | $this->edebug('SERVER -> CLIENT: ' . $this->last_reply, self::DEBUG_SERVER); |
||
890 | |||
891 | if (!in_array($code, (array)$expect)) { |
||
892 | $this->setError( |
||
893 | "$command command failed", |
||
894 | $detail, |
||
895 | $code, |
||
896 | $code_ex |
||
897 | ); |
||
898 | $this->edebug( |
||
899 | 'SMTP ERROR: ' . $this->error['error'] . ': ' . $this->last_reply, |
||
900 | self::DEBUG_CLIENT |
||
901 | ); |
||
902 | return false; |
||
903 | } |
||
904 | |||
905 | $this->setError(''); |
||
906 | return true; |
||
907 | } |
||
908 | |||
909 | /** |
||
910 | * Send an SMTP SAML command. |
||
911 | * Starts a mail transaction from the email address specified in $from. |
||
912 | * Returns true if successful or false otherwise. If True |
||
913 | * the mail transaction is started and then one or more recipient |
||
914 | * commands may be called followed by a data command. This command |
||
915 | * will send the message to the users terminal if they are logged |
||
916 | * in and send them an email. |
||
917 | * Implements RFC 821: SAML <SP> FROM:<reverse-path> <CRLF> |
||
918 | * |
||
919 | * @param string $from The address the message is from |
||
920 | * |
||
921 | * @return boolean |
||
922 | */ |
||
923 | public function sendAndMail($from) |
||
924 | { |
||
925 | return $this->sendCommand('SAML', "SAML FROM:$from", 250); |
||
926 | } |
||
927 | |||
928 | /** |
||
929 | * Send an SMTP VRFY command. |
||
930 | * |
||
931 | * @param string $name The name to verify |
||
932 | * |
||
933 | * @return boolean |
||
934 | */ |
||
935 | public function verify($name) |
||
936 | { |
||
937 | return $this->sendCommand('VRFY', "VRFY $name", [250, 251]); |
||
938 | } |
||
939 | |||
940 | /** |
||
941 | * Send an SMTP NOOP command. |
||
942 | * Used to keep keep-alives alive, doesn't actually do anything |
||
943 | * |
||
944 | * @return boolean |
||
945 | */ |
||
946 | public function noop() |
||
947 | { |
||
948 | return $this->sendCommand('NOOP', 'NOOP', 250); |
||
949 | } |
||
950 | |||
951 | /** |
||
952 | * Send an SMTP TURN command. |
||
953 | * This is an optional command for SMTP that this class does not support. |
||
954 | * This method is here to make the RFC821 Definition complete for this class |
||
955 | * and _may_ be implemented in future |
||
956 | * Implements from RFC 821: TURN <CRLF> |
||
957 | * |
||
958 | * @return boolean |
||
959 | */ |
||
960 | public function turn() |
||
961 | { |
||
962 | $this->setError('The SMTP TURN command is not implemented'); |
||
963 | $this->edebug('SMTP NOTICE: ' . $this->error['error'], self::DEBUG_CLIENT); |
||
964 | return false; |
||
965 | } |
||
966 | |||
967 | /** |
||
968 | * Send raw data to the server. |
||
969 | * |
||
970 | * @param string $data The data to send |
||
971 | * |
||
972 | * @return integer|boolean The number of bytes sent to the server or false on error |
||
973 | */ |
||
974 | public function client_send($data) |
||
975 | { |
||
976 | $this->edebug("CLIENT -> SERVER: $data", self::DEBUG_CLIENT); |
||
977 | set_error_handler([$this, 'errorHandler']); |
||
978 | $result = fwrite($this->smtp_conn, $data); |
||
979 | restore_error_handler(); |
||
980 | return $result; |
||
981 | } |
||
982 | |||
983 | /** |
||
984 | * Get the latest error. |
||
985 | * |
||
986 | * @return array |
||
987 | */ |
||
988 | public function getError() |
||
989 | { |
||
990 | return $this->error; |
||
991 | } |
||
992 | |||
993 | /** |
||
994 | * Get SMTP extensions available on the server |
||
995 | * |
||
996 | * @return array|null |
||
997 | */ |
||
998 | public function getServerExtList() |
||
999 | { |
||
1000 | return $this->server_caps; |
||
1001 | } |
||
1002 | |||
1003 | /** |
||
1004 | * A multipurpose method |
||
1005 | * The method works in three ways, dependent on argument value and current state |
||
1006 | * 1. HELO/EHLO was not sent - returns null and set up $this->error |
||
1007 | * 2. HELO was sent |
||
1008 | * $name = 'HELO': returns server name |
||
1009 | * $name = 'EHLO': returns boolean false |
||
1010 | * $name = any string: returns null and set up $this->error |
||
1011 | * 3. EHLO was sent |
||
1012 | * $name = 'HELO'|'EHLO': returns server name |
||
1013 | * $name = any string: if extension $name exists, returns boolean True |
||
1014 | * or its options. Otherwise returns boolean False |
||
1015 | * In other words, one can use this method to detect 3 conditions: |
||
1016 | * - null returned: handshake was not or we don't know about ext (refer to $this->error) |
||
1017 | * - false returned: the requested feature exactly not exists |
||
1018 | * - positive value returned: the requested feature exists |
||
1019 | * |
||
1020 | * @param string $name Name of SMTP extension or 'HELO'|'EHLO' |
||
1021 | * |
||
1022 | * @return mixed |
||
1023 | */ |
||
1024 | public function getServerExt($name) |
||
1025 | { |
||
1026 | if (!$this->server_caps) { |
||
1027 | $this->setError('No HELO/EHLO was sent'); |
||
1028 | return null; |
||
1029 | } |
||
1030 | |||
1031 | // the tight logic knot ;) |
||
1032 | if (!array_key_exists($name, $this->server_caps)) { |
||
1033 | if ('HELO' == $name) { |
||
1034 | return $this->server_caps['EHLO']; |
||
1035 | } |
||
1036 | if ('EHLO' == $name || array_key_exists('EHLO', $this->server_caps)) { |
||
1037 | return false; |
||
1038 | } |
||
1039 | $this->setError('HELO handshake was used. Client knows nothing about server extensions'); |
||
1040 | return null; |
||
1041 | } |
||
1042 | |||
1043 | return $this->server_caps[$name]; |
||
1044 | } |
||
1045 | |||
1046 | /** |
||
1047 | * Get the last reply from the server. |
||
1048 | * |
||
1049 | * @return string |
||
1050 | */ |
||
1051 | public function getLastReply() |
||
1052 | { |
||
1053 | return $this->last_reply; |
||
1054 | } |
||
1055 | |||
1056 | /** |
||
1057 | * Read the SMTP server's response. |
||
1058 | * Either before eof or socket timeout occurs on the operation. |
||
1059 | * With SMTP we can tell if we have more lines to read if the |
||
1060 | * 4th character is '-' symbol. If it is a space then we don't |
||
1061 | * need to read anything else. |
||
1062 | * |
||
1063 | * @return string |
||
1064 | */ |
||
1065 | protected function get_lines() |
||
1066 | { |
||
1067 | // If the connection is bad, give up straight away |
||
1068 | if (!is_resource($this->smtp_conn)) { |
||
1069 | return ''; |
||
1070 | } |
||
1071 | $data = ''; |
||
1072 | $endtime = 0; |
||
1073 | stream_set_timeout($this->smtp_conn, $this->Timeout); |
||
1074 | if ($this->Timelimit > 0) { |
||
1075 | $endtime = time() + $this->Timelimit; |
||
1076 | } |
||
1077 | $selR = [$this->smtp_conn]; |
||
1078 | $selW = null; |
||
1079 | while (is_resource($this->smtp_conn) and !feof($this->smtp_conn)) { |
||
1080 | //Must pass vars in here as params are by reference |
||
1081 | if (!stream_select($selR, $selW, $selW, $this->Timelimit)) { |
||
1082 | $this->edebug( |
||
1083 | 'SMTP -> get_lines(): timed-out (' . $this->Timeout . ' sec)', |
||
1084 | self::DEBUG_LOWLEVEL |
||
1085 | ); |
||
1086 | break; |
||
1087 | } |
||
1088 | //Deliberate noise suppression - errors are handled afterwards |
||
1089 | $str = @fgets($this->smtp_conn, 515); |
||
1090 | $this->edebug("SMTP INBOUND: \"". trim($str).'"', self::DEBUG_LOWLEVEL); |
||
1091 | $data .= $str; |
||
1092 | // If response is only 3 chars (not valid, but RFC5321 S4.2 says it must be handled), |
||
1093 | // or 4th character is a space, we are done reading, break the loop, |
||
1094 | // string array access is a micro-optimisation over strlen |
||
1095 | if (!isset($str[3]) or (isset($str[3]) and $str[3] == ' ')) { |
||
1096 | break; |
||
1097 | } |
||
1098 | // Timed-out? Log and break |
||
1099 | $info = stream_get_meta_data($this->smtp_conn); |
||
1100 | if ($info['timed_out']) { |
||
1101 | $this->edebug( |
||
1102 | 'SMTP -> get_lines(): timed-out (' . $this->Timeout . ' sec)', |
||
1103 | self::DEBUG_LOWLEVEL |
||
1104 | ); |
||
1105 | break; |
||
1106 | } |
||
1107 | // Now check if reads took too long |
||
1108 | if ($endtime and time() > $endtime) { |
||
1109 | $this->edebug( |
||
1110 | 'SMTP -> get_lines(): timelimit reached (' . |
||
1111 | $this->Timelimit . ' sec)', |
||
1112 | self::DEBUG_LOWLEVEL |
||
1113 | ); |
||
1114 | break; |
||
1115 | } |
||
1116 | } |
||
1117 | return $data; |
||
1118 | } |
||
1119 | |||
1120 | /** |
||
1121 | * Enable or disable VERP address generation. |
||
1122 | * |
||
1123 | * @param boolean $enabled |
||
1124 | */ |
||
1125 | public function setVerp($enabled = false) |
||
1126 | { |
||
1127 | $this->do_verp = $enabled; |
||
1128 | } |
||
1129 | |||
1130 | /** |
||
1131 | * Get VERP address generation mode. |
||
1132 | * |
||
1133 | * @return boolean |
||
1134 | */ |
||
1135 | public function getVerp() |
||
1136 | { |
||
1137 | return $this->do_verp; |
||
1138 | } |
||
1139 | |||
1140 | /** |
||
1141 | * Set error messages and codes. |
||
1142 | * |
||
1143 | * @param string $message The error message |
||
1144 | * @param string $detail Further detail on the error |
||
1145 | * @param string $smtp_code An associated SMTP error code |
||
1146 | * @param string $smtp_code_ex Extended SMTP code |
||
1147 | */ |
||
1148 | protected function setError($message, $detail = '', $smtp_code = '', $smtp_code_ex = '') |
||
1149 | { |
||
1150 | $this->error = [ |
||
1151 | 'error' => $message, |
||
1152 | 'detail' => $detail, |
||
1153 | 'smtp_code' => $smtp_code, |
||
1154 | 'smtp_code_ex' => $smtp_code_ex |
||
1155 | ]; |
||
1156 | } |
||
1157 | |||
1158 | /** |
||
1159 | * Set debug output method. |
||
1160 | * |
||
1161 | * @param string|callable $method The name of the mechanism to use for debugging output, or a callable to handle it. |
||
1162 | */ |
||
1163 | public function setDebugOutput($method = 'echo') |
||
1164 | { |
||
1165 | $this->Debugoutput = $method; |
||
1166 | } |
||
1167 | |||
1168 | /** |
||
1169 | * Get debug output method. |
||
1170 | * |
||
1171 | * @return string |
||
1172 | */ |
||
1173 | public function getDebugOutput() |
||
1174 | { |
||
1175 | return $this->Debugoutput; |
||
1176 | } |
||
1177 | |||
1178 | /** |
||
1179 | * Set debug output level. |
||
1180 | * |
||
1181 | * @param integer $level |
||
1182 | */ |
||
1183 | public function setDebugLevel($level = 0) |
||
1184 | { |
||
1185 | $this->do_debug = $level; |
||
1186 | } |
||
1187 | |||
1188 | /** |
||
1189 | * Get debug output level. |
||
1190 | * |
||
1191 | * @return integer |
||
1192 | */ |
||
1193 | public function getDebugLevel() |
||
1194 | { |
||
1195 | return $this->do_debug; |
||
1196 | } |
||
1197 | |||
1198 | /** |
||
1199 | * Set SMTP timeout. |
||
1200 | * |
||
1201 | * @param integer $timeout |
||
1202 | */ |
||
1203 | public function setTimeout($timeout = 0) |
||
1204 | { |
||
1205 | $this->Timeout = $timeout; |
||
1206 | } |
||
1207 | |||
1208 | /** |
||
1209 | * Get SMTP timeout. |
||
1210 | * |
||
1211 | * @return integer |
||
1212 | */ |
||
1213 | public function getTimeout() |
||
1214 | { |
||
1215 | return $this->Timeout; |
||
1216 | } |
||
1217 | |||
1218 | /** |
||
1219 | * Reports an error number and string. |
||
1220 | * |
||
1221 | * @param integer $errno The error number returned by PHP. |
||
1222 | * @param string $errmsg The error message returned by PHP. |
||
1223 | * @param string $errfile The file the error occurred in |
||
1224 | * @param integer $errline The line number the error occurred on |
||
1225 | */ |
||
1226 | protected function errorHandler($errno, $errmsg, $errfile = '', $errline = 0) |
||
1227 | { |
||
1228 | $notice = 'Connection failed.'; |
||
1229 | $this->setError( |
||
1230 | $notice, |
||
1231 | $errno, |
||
1232 | $errmsg |
||
1233 | ); |
||
1234 | $this->edebug( |
||
1235 | $notice . ' Error #' . $errno . ': ' . $errmsg . " [$errfile line $errline]", |
||
1236 | self::DEBUG_CONNECTION |
||
1237 | ); |
||
1238 | } |
||
1239 | |||
1240 | /** |
||
1241 | * Extract and return the ID of the last SMTP transaction based on |
||
1242 | * a list of patterns provided in SMTP::$smtp_transaction_id_patterns. |
||
1243 | * Relies on the host providing the ID in response to a DATA command. |
||
1244 | * If no reply has been received yet, it will return null. |
||
1245 | * If no pattern was matched, it will return false. |
||
1246 | * @return bool|null|string |
||
1247 | */ |
||
1248 | protected function recordLastTransactionID() |
||
1249 | { |
||
1250 | $reply = $this->getLastReply(); |
||
1251 | |||
1252 | if (empty($reply)) { |
||
1253 | $this->last_smtp_transaction_id = null; |
||
1254 | } else { |
||
1255 | $this->last_smtp_transaction_id = false; |
||
0 ignored issues
–
show
|
|||
1256 | foreach ($this->smtp_transaction_id_patterns as $smtp_transaction_id_pattern) { |
||
1257 | if (preg_match($smtp_transaction_id_pattern, $reply, $matches)) { |
||
1258 | $this->last_smtp_transaction_id = $matches[1]; |
||
1259 | } |
||
1260 | } |
||
1261 | } |
||
1262 | |||
1263 | return $this->last_smtp_transaction_id; |
||
1264 | } |
||
1265 | |||
1266 | /** |
||
1267 | * Get the queue/transaction ID of the last SMTP transaction |
||
1268 | * If no reply has been received yet, it will return null. |
||
1269 | * If no pattern was matched, it will return false. |
||
1270 | * @return bool|null|string |
||
1271 | * @see recordLastTransactionID() |
||
1272 | */ |
||
1273 | public function getLastTransactionID() |
||
1274 | { |
||
1275 | return $this->last_smtp_transaction_id; |
||
1276 | } |
||
1277 | } |
||
1278 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.