1 | <?php |
---|
2 | /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ |
---|
3 | // +----------------------------------------------------------------------+ |
---|
4 | // | PHP Version 4 | |
---|
5 | // +----------------------------------------------------------------------+ |
---|
6 | // | Copyright (c) 1997-2003 The PHP Group | |
---|
7 | // +----------------------------------------------------------------------+ |
---|
8 | // | This source file is subject to version 2.02 of the PHP license, | |
---|
9 | // | that is bundled with this package in the file LICENSE, and is | |
---|
10 | // | available at through the world-wide-web at | |
---|
11 | // | http://www.php.net/license/2_02.txt. | |
---|
12 | // | If you did not receive a copy of the PHP license and are unable to | |
---|
13 | // | obtain it through the world-wide-web, please send a note to | |
---|
14 | // | license@php.net so we can mail you a copy immediately. | |
---|
15 | // +----------------------------------------------------------------------+ |
---|
16 | // | Authors: Chuck Hagenbuch <chuck@horde.org> | |
---|
17 | // | Jon Parise <jon@php.net> | |
---|
18 | // | Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar> | |
---|
19 | // +----------------------------------------------------------------------+ |
---|
20 | // |
---|
21 | // $Id: SMTP.php,v 1.58 2007/03/28 04:53:34 chagenbu Exp $ |
---|
22 | |
---|
23 | $include_dir = realpath(dirname( __FILE__)); |
---|
24 | require_once $include_dir . "/../PEAR.php"; |
---|
25 | require_once $include_dir . "/../Net/Socket.php"; |
---|
26 | |
---|
27 | /** |
---|
28 | * Provides an implementation of the SMTP protocol using PEAR's |
---|
29 | * Net_Socket:: class. |
---|
30 | * |
---|
31 | * @package Net_SMTP |
---|
32 | * @author Chuck Hagenbuch <chuck@horde.org> |
---|
33 | * @author Jon Parise <jon@php.net> |
---|
34 | * @author Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar> |
---|
35 | * |
---|
36 | * @example basic.php A basic implementation of the Net_SMTP package. |
---|
37 | */ |
---|
38 | class Net_SMTP |
---|
39 | { |
---|
40 | /** |
---|
41 | * The server to connect to. |
---|
42 | * @var string |
---|
43 | * @access public |
---|
44 | */ |
---|
45 | var $host = 'localhost'; |
---|
46 | |
---|
47 | /** |
---|
48 | * The port to connect to. |
---|
49 | * @var int |
---|
50 | * @access public |
---|
51 | */ |
---|
52 | var $port = 25; |
---|
53 | |
---|
54 | /** |
---|
55 | * The value to give when sending EHLO or HELO. |
---|
56 | * @var string |
---|
57 | * @access public |
---|
58 | */ |
---|
59 | var $localhost = 'localhost'; |
---|
60 | |
---|
61 | /** |
---|
62 | * List of supported authentication methods, in preferential order. |
---|
63 | * @var array |
---|
64 | * @access public |
---|
65 | */ |
---|
66 | var $auth_methods = array('DIGEST-MD5', 'CRAM-MD5', 'LOGIN', 'PLAIN'); |
---|
67 | |
---|
68 | /** |
---|
69 | * Should debugging output be enabled? |
---|
70 | * @var boolean |
---|
71 | * @access private |
---|
72 | */ |
---|
73 | var $_debug = false; |
---|
74 | |
---|
75 | /** |
---|
76 | * The socket resource being used to connect to the SMTP server. |
---|
77 | * @var resource |
---|
78 | * @access private |
---|
79 | */ |
---|
80 | var $_socket = null; |
---|
81 | |
---|
82 | /** |
---|
83 | * The most recent server response code. |
---|
84 | * @var int |
---|
85 | * @access private |
---|
86 | */ |
---|
87 | var $_code = -1; |
---|
88 | |
---|
89 | /** |
---|
90 | * The most recent server response arguments. |
---|
91 | * @var array |
---|
92 | * @access private |
---|
93 | */ |
---|
94 | var $_arguments = array(); |
---|
95 | |
---|
96 | /** |
---|
97 | * Stores detected features of the SMTP server. |
---|
98 | * @var array |
---|
99 | * @access private |
---|
100 | */ |
---|
101 | var $_esmtp = array(); |
---|
102 | |
---|
103 | /** |
---|
104 | * Instantiates a new Net_SMTP object, overriding any defaults |
---|
105 | * with parameters that are passed in. |
---|
106 | * |
---|
107 | * If you have SSL support in PHP, you can connect to a server |
---|
108 | * over SSL using an 'ssl://' prefix: |
---|
109 | * |
---|
110 | * // 465 is a common smtps port. |
---|
111 | * $smtp = new Net_SMTP('ssl://mail.host.com', 465); |
---|
112 | * $smtp->connect(); |
---|
113 | * |
---|
114 | * @param string $host The server to connect to. |
---|
115 | * @param integer $port The port to connect to. |
---|
116 | * @param string $localhost The value to give when sending EHLO or HELO. |
---|
117 | * |
---|
118 | * @access public |
---|
119 | * @since 1.0 |
---|
120 | */ |
---|
121 | function Net_SMTP($host = null, $port = null, $localhost = null) |
---|
122 | { |
---|
123 | if (isset($host)) $this->host = $host; |
---|
124 | if (isset($port)) $this->port = $port; |
---|
125 | if (isset($localhost)) $this->localhost = $localhost; |
---|
126 | |
---|
127 | $this->_socket = new Net_Socket(); |
---|
128 | |
---|
129 | /* |
---|
130 | * Include the Auth_SASL package. If the package is not available, |
---|
131 | * we disable the authentication methods that depend upon it. |
---|
132 | */ |
---|
133 | if ((@include_once '../Auth/SASL.php') === false) { |
---|
134 | $pos = array_search('DIGEST-MD5', $this->auth_methods); |
---|
135 | unset($this->auth_methods[$pos]); |
---|
136 | $pos = array_search('CRAM-MD5', $this->auth_methods); |
---|
137 | unset($this->auth_methods[$pos]); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * Set the value of the debugging flag. |
---|
143 | * |
---|
144 | * @param boolean $debug New value for the debugging flag. |
---|
145 | * |
---|
146 | * @access public |
---|
147 | * @since 1.1.0 |
---|
148 | */ |
---|
149 | function setDebug($debug) |
---|
150 | { |
---|
151 | $this->_debug = $debug; |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * Send the given string of data to the server. |
---|
156 | * |
---|
157 | * @param string $data The string of data to send. |
---|
158 | * |
---|
159 | * @return mixed True on success or a PEAR_Error object on failure. |
---|
160 | * |
---|
161 | * @access private |
---|
162 | * @since 1.1.0 |
---|
163 | */ |
---|
164 | function _send($data) |
---|
165 | { |
---|
166 | if ($this->_debug) { |
---|
167 | echo "DEBUG: Send: $data\n"; |
---|
168 | } |
---|
169 | |
---|
170 | if (PEAR::isError($error = $this->_socket->write($data))) { |
---|
171 | return PEAR::raiseError('Failed to write to socket: ' . |
---|
172 | $error->getMessage()); |
---|
173 | } |
---|
174 | |
---|
175 | return true; |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | * Send a command to the server with an optional string of |
---|
180 | * arguments. A carriage return / linefeed (CRLF) sequence will |
---|
181 | * be appended to each command string before it is sent to the |
---|
182 | * SMTP server - an error will be thrown if the command string |
---|
183 | * already contains any newline characters. Use _send() for |
---|
184 | * commands that must contain newlines. |
---|
185 | * |
---|
186 | * @param string $command The SMTP command to send to the server. |
---|
187 | * @param string $args A string of optional arguments to append |
---|
188 | * to the command. |
---|
189 | * |
---|
190 | * @return mixed The result of the _send() call. |
---|
191 | * |
---|
192 | * @access private |
---|
193 | * @since 1.1.0 |
---|
194 | */ |
---|
195 | function _put($command, $args = '') |
---|
196 | { |
---|
197 | if (!empty($args)) { |
---|
198 | $command .= ' ' . $args; |
---|
199 | } |
---|
200 | |
---|
201 | if (strcspn($command, "\r\n") !== strlen($command)) { |
---|
202 | return PEAR::raiseError('Commands cannot contain newlines'); |
---|
203 | } |
---|
204 | |
---|
205 | return $this->_send($command . "\r\n"); |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * Read a reply from the SMTP server. The reply consists of a response |
---|
210 | * code and a response message. |
---|
211 | * |
---|
212 | * @param mixed $valid The set of valid response codes. These |
---|
213 | * may be specified as an array of integer |
---|
214 | * values or as a single integer value. |
---|
215 | * |
---|
216 | * @return mixed True if the server returned a valid response code or |
---|
217 | * a PEAR_Error object is an error condition is reached. |
---|
218 | * |
---|
219 | * @access private |
---|
220 | * @since 1.1.0 |
---|
221 | * |
---|
222 | * @see getResponse |
---|
223 | */ |
---|
224 | function _parseResponse($valid) |
---|
225 | { |
---|
226 | $this->_code = -1; |
---|
227 | $this->_arguments = array(); |
---|
228 | |
---|
229 | while ($line = $this->_socket->readLine()) { |
---|
230 | if ($this->_debug) { |
---|
231 | echo "DEBUG: Recv: $line\n"; |
---|
232 | } |
---|
233 | |
---|
234 | /* If we receive an empty line, the connection has been closed. */ |
---|
235 | if (empty($line)) { |
---|
236 | $this->disconnect(); |
---|
237 | return PEAR::raiseError('Connection was unexpectedly closed'); |
---|
238 | } |
---|
239 | |
---|
240 | /* Read the code and store the rest in the arguments array. */ |
---|
241 | $code = substr($line, 0, 3); |
---|
242 | $this->_arguments[] = trim(substr($line, 4)); |
---|
243 | |
---|
244 | /* Check the syntax of the response code. */ |
---|
245 | if (is_numeric($code)) { |
---|
246 | $this->_code = (int)$code; |
---|
247 | } else { |
---|
248 | $this->_code = -1; |
---|
249 | break; |
---|
250 | } |
---|
251 | |
---|
252 | /* If this is not a multiline response, we're done. */ |
---|
253 | if (substr($line, 3, 1) != '-') { |
---|
254 | break; |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | /* Compare the server's response code with the valid code. */ |
---|
259 | if (is_int($valid) && ($this->_code === $valid)) { |
---|
260 | return true; |
---|
261 | } |
---|
262 | |
---|
263 | /* If we were given an array of valid response codes, check each one. */ |
---|
264 | if (is_array($valid)) { |
---|
265 | foreach ($valid as $valid_code) { |
---|
266 | if ($this->_code === $valid_code) { |
---|
267 | return true; |
---|
268 | } |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | return PEAR::raiseError('Invalid response code received from server', |
---|
273 | $this->_code); |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * Return a 2-tuple containing the last response from the SMTP server. |
---|
278 | * |
---|
279 | * @return array A two-element array: the first element contains the |
---|
280 | * response code as an integer and the second element |
---|
281 | * contains the response's arguments as a string. |
---|
282 | * |
---|
283 | * @access public |
---|
284 | * @since 1.1.0 |
---|
285 | */ |
---|
286 | function getResponse() |
---|
287 | { |
---|
288 | return array($this->_code, join("\n", $this->_arguments)); |
---|
289 | } |
---|
290 | |
---|
291 | /** |
---|
292 | * Attempt to connect to the SMTP server. |
---|
293 | * |
---|
294 | * @param int $timeout The timeout value (in seconds) for the |
---|
295 | * socket connection. |
---|
296 | * @param bool $persistent Should a persistent socket connection |
---|
297 | * be used? |
---|
298 | * |
---|
299 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
300 | * kind of failure, or true on success. |
---|
301 | * @access public |
---|
302 | * @since 1.0 |
---|
303 | */ |
---|
304 | function connect($timeout = null, $persistent = false) |
---|
305 | { |
---|
306 | $result = $this->_socket->connect($this->host, $this->port, |
---|
307 | $persistent, $timeout); |
---|
308 | if (PEAR::isError($result)) { |
---|
309 | return PEAR::raiseError('Failed to connect socket: ' . |
---|
310 | $result->getMessage()); |
---|
311 | } |
---|
312 | |
---|
313 | if (PEAR::isError($error = $this->_parseResponse(220))) { |
---|
314 | return $error; |
---|
315 | } |
---|
316 | if (PEAR::isError($error = $this->_negotiate())) { |
---|
317 | return $error; |
---|
318 | } |
---|
319 | |
---|
320 | return true; |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | * Attempt to disconnect from the SMTP server. |
---|
325 | * |
---|
326 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
327 | * kind of failure, or true on success. |
---|
328 | * @access public |
---|
329 | * @since 1.0 |
---|
330 | */ |
---|
331 | function disconnect() |
---|
332 | { |
---|
333 | if (PEAR::isError($error = $this->_put('QUIT'))) { |
---|
334 | return $error; |
---|
335 | } |
---|
336 | if (PEAR::isError($error = $this->_parseResponse(221))) { |
---|
337 | return $error; |
---|
338 | } |
---|
339 | if (PEAR::isError($error = $this->_socket->disconnect())) { |
---|
340 | return PEAR::raiseError('Failed to disconnect socket: ' . |
---|
341 | $error->getMessage()); |
---|
342 | } |
---|
343 | |
---|
344 | return true; |
---|
345 | } |
---|
346 | |
---|
347 | /** |
---|
348 | * Attempt to send the EHLO command and obtain a list of ESMTP |
---|
349 | * extensions available, and failing that just send HELO. |
---|
350 | * |
---|
351 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
352 | * kind of failure, or true on success. |
---|
353 | * |
---|
354 | * @access private |
---|
355 | * @since 1.1.0 |
---|
356 | */ |
---|
357 | function _negotiate() |
---|
358 | { |
---|
359 | if (PEAR::isError($error = $this->_put('EHLO', $this->localhost))) { |
---|
360 | return $error; |
---|
361 | } |
---|
362 | |
---|
363 | if (PEAR::isError($this->_parseResponse(250))) { |
---|
364 | /* If we receive a 503 response, we're already authenticated. */ |
---|
365 | if ($this->_code === 503) { |
---|
366 | return true; |
---|
367 | } |
---|
368 | |
---|
369 | /* If the EHLO failed, try the simpler HELO command. */ |
---|
370 | if (PEAR::isError($error = $this->_put('HELO', $this->localhost))) { |
---|
371 | return $error; |
---|
372 | } |
---|
373 | if (PEAR::isError($this->_parseResponse(250))) { |
---|
374 | return PEAR::raiseError('HELO was not accepted: ', $this->_code); |
---|
375 | } |
---|
376 | |
---|
377 | return true; |
---|
378 | } |
---|
379 | |
---|
380 | foreach ($this->_arguments as $argument) { |
---|
381 | $verb = strtok($argument, ' '); |
---|
382 | $arguments = substr($argument, strlen($verb) + 1, |
---|
383 | strlen($argument) - strlen($verb) - 1); |
---|
384 | $this->_esmtp[$verb] = $arguments; |
---|
385 | } |
---|
386 | |
---|
387 | return true; |
---|
388 | } |
---|
389 | |
---|
390 | /** |
---|
391 | * Returns the name of the best authentication method that the server |
---|
392 | * has advertised. |
---|
393 | * |
---|
394 | * @return mixed Returns a string containing the name of the best |
---|
395 | * supported authentication method or a PEAR_Error object |
---|
396 | * if a failure condition is encountered. |
---|
397 | * @access private |
---|
398 | * @since 1.1.0 |
---|
399 | */ |
---|
400 | function _getBestAuthMethod() |
---|
401 | { |
---|
402 | $available_methods = explode(' ', $this->_esmtp['AUTH']); |
---|
403 | |
---|
404 | foreach ($this->auth_methods as $method) { |
---|
405 | if (in_array($method, $available_methods)) { |
---|
406 | return $method; |
---|
407 | } |
---|
408 | } |
---|
409 | |
---|
410 | return PEAR::raiseError('No supported authentication methods'); |
---|
411 | } |
---|
412 | |
---|
413 | /** |
---|
414 | * Attempt to do SMTP authentication. |
---|
415 | * |
---|
416 | * @param string The userid to authenticate as. |
---|
417 | * @param string The password to authenticate with. |
---|
418 | * @param string The requested authentication method. If none is |
---|
419 | * specified, the best supported method will be used. |
---|
420 | * |
---|
421 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
422 | * kind of failure, or true on success. |
---|
423 | * @access public |
---|
424 | * @since 1.0 |
---|
425 | */ |
---|
426 | function auth($uid, $pwd , $method = '') |
---|
427 | { |
---|
428 | if (empty($this->_esmtp['AUTH'])) { |
---|
429 | if (version_compare(PHP_VERSION, '5.1.0', '>=')) { |
---|
430 | if (!isset($this->_esmtp['STARTTLS'])) { |
---|
431 | return PEAR::raiseError('SMTP server does not support authentication'); |
---|
432 | } |
---|
433 | if (PEAR::isError($result = $this->_put('STARTTLS'))) { |
---|
434 | return $result; |
---|
435 | } |
---|
436 | if (PEAR::isError($result = $this->_parseResponse(220))) { |
---|
437 | return $result; |
---|
438 | } |
---|
439 | if (PEAR::isError($result = $this->_socket->enableCrypto(true, STREAM_CRYPTO_METHOD_TLS_CLIENT))) { |
---|
440 | return $result; |
---|
441 | } elseif ($result !== true) { |
---|
442 | return PEAR::raiseError('STARTTLS failed'); |
---|
443 | } |
---|
444 | |
---|
445 | /* Send EHLO again to recieve the AUTH string from the |
---|
446 | * SMTP server. */ |
---|
447 | $this->_negotiate(); |
---|
448 | if (empty($this->_esmtp['AUTH'])) { |
---|
449 | return PEAR::raiseError('SMTP server does not support authentication'); |
---|
450 | } |
---|
451 | } else { |
---|
452 | return PEAR::raiseError('SMTP server does not support authentication'); |
---|
453 | } |
---|
454 | } |
---|
455 | |
---|
456 | /* If no method has been specified, get the name of the best |
---|
457 | * supported method advertised by the SMTP server. */ |
---|
458 | if (empty($method)) { |
---|
459 | if (PEAR::isError($method = $this->_getBestAuthMethod())) { |
---|
460 | /* Return the PEAR_Error object from _getBestAuthMethod(). */ |
---|
461 | return $method; |
---|
462 | } |
---|
463 | } else { |
---|
464 | $method = strtoupper($method); |
---|
465 | if (!in_array($method, $this->auth_methods)) { |
---|
466 | return PEAR::raiseError("$method is not a supported authentication method"); |
---|
467 | } |
---|
468 | } |
---|
469 | |
---|
470 | switch ($method) { |
---|
471 | case 'DIGEST-MD5': |
---|
472 | $result = $this->_authDigest_MD5($uid, $pwd); |
---|
473 | break; |
---|
474 | |
---|
475 | case 'CRAM-MD5': |
---|
476 | $result = $this->_authCRAM_MD5($uid, $pwd); |
---|
477 | break; |
---|
478 | |
---|
479 | case 'LOGIN': |
---|
480 | $result = $this->_authLogin($uid, $pwd); |
---|
481 | break; |
---|
482 | |
---|
483 | case 'PLAIN': |
---|
484 | $result = $this->_authPlain($uid, $pwd); |
---|
485 | break; |
---|
486 | |
---|
487 | default: |
---|
488 | $result = PEAR::raiseError("$method is not a supported authentication method"); |
---|
489 | break; |
---|
490 | } |
---|
491 | |
---|
492 | /* If an error was encountered, return the PEAR_Error object. */ |
---|
493 | if (PEAR::isError($result)) { |
---|
494 | return $result; |
---|
495 | } |
---|
496 | |
---|
497 | return true; |
---|
498 | } |
---|
499 | |
---|
500 | /** |
---|
501 | * Authenticates the user using the DIGEST-MD5 method. |
---|
502 | * |
---|
503 | * @param string The userid to authenticate as. |
---|
504 | * @param string The password to authenticate with. |
---|
505 | * |
---|
506 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
507 | * kind of failure, or true on success. |
---|
508 | * @access private |
---|
509 | * @since 1.1.0 |
---|
510 | */ |
---|
511 | function _authDigest_MD5($uid, $pwd) |
---|
512 | { |
---|
513 | if (PEAR::isError($error = $this->_put('AUTH', 'DIGEST-MD5'))) { |
---|
514 | return $error; |
---|
515 | } |
---|
516 | /* 334: Continue authentication request */ |
---|
517 | if (PEAR::isError($error = $this->_parseResponse(334))) { |
---|
518 | /* 503: Error: already authenticated */ |
---|
519 | if ($this->_code === 503) { |
---|
520 | return true; |
---|
521 | } |
---|
522 | return $error; |
---|
523 | } |
---|
524 | |
---|
525 | $challenge = base64_decode($this->_arguments[0]); |
---|
526 | $digest = &Auth_SASL::factory('digestmd5'); |
---|
527 | $auth_str = base64_encode($digest->getResponse($uid, $pwd, $challenge, |
---|
528 | $this->host, "smtp")); |
---|
529 | |
---|
530 | if (PEAR::isError($error = $this->_put($auth_str))) { |
---|
531 | return $error; |
---|
532 | } |
---|
533 | /* 334: Continue authentication request */ |
---|
534 | if (PEAR::isError($error = $this->_parseResponse(334))) { |
---|
535 | return $error; |
---|
536 | } |
---|
537 | |
---|
538 | /* We don't use the protocol's third step because SMTP doesn't |
---|
539 | * allow subsequent authentication, so we just silently ignore |
---|
540 | * it. */ |
---|
541 | if (PEAR::isError($error = $this->_put(' '))) { |
---|
542 | return $error; |
---|
543 | } |
---|
544 | /* 235: Authentication successful */ |
---|
545 | if (PEAR::isError($error = $this->_parseResponse(235))) { |
---|
546 | return $error; |
---|
547 | } |
---|
548 | } |
---|
549 | |
---|
550 | /** |
---|
551 | * Authenticates the user using the CRAM-MD5 method. |
---|
552 | * |
---|
553 | * @param string The userid to authenticate as. |
---|
554 | * @param string The password to authenticate with. |
---|
555 | * |
---|
556 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
557 | * kind of failure, or true on success. |
---|
558 | * @access private |
---|
559 | * @since 1.1.0 |
---|
560 | */ |
---|
561 | function _authCRAM_MD5($uid, $pwd) |
---|
562 | { |
---|
563 | if (PEAR::isError($error = $this->_put('AUTH', 'CRAM-MD5'))) { |
---|
564 | return $error; |
---|
565 | } |
---|
566 | /* 334: Continue authentication request */ |
---|
567 | if (PEAR::isError($error = $this->_parseResponse(334))) { |
---|
568 | /* 503: Error: already authenticated */ |
---|
569 | if ($this->_code === 503) { |
---|
570 | return true; |
---|
571 | } |
---|
572 | return $error; |
---|
573 | } |
---|
574 | |
---|
575 | $challenge = base64_decode($this->_arguments[0]); |
---|
576 | $cram = &Auth_SASL::factory('crammd5'); |
---|
577 | $auth_str = base64_encode($cram->getResponse($uid, $pwd, $challenge)); |
---|
578 | |
---|
579 | if (PEAR::isError($error = $this->_put($auth_str))) { |
---|
580 | return $error; |
---|
581 | } |
---|
582 | |
---|
583 | /* 235: Authentication successful */ |
---|
584 | if (PEAR::isError($error = $this->_parseResponse(235))) { |
---|
585 | return $error; |
---|
586 | } |
---|
587 | } |
---|
588 | |
---|
589 | /** |
---|
590 | * Authenticates the user using the LOGIN method. |
---|
591 | * |
---|
592 | * @param string The userid to authenticate as. |
---|
593 | * @param string The password to authenticate with. |
---|
594 | * |
---|
595 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
596 | * kind of failure, or true on success. |
---|
597 | * @access private |
---|
598 | * @since 1.1.0 |
---|
599 | */ |
---|
600 | function _authLogin($uid, $pwd) |
---|
601 | { |
---|
602 | if (PEAR::isError($error = $this->_put('AUTH', 'LOGIN'))) { |
---|
603 | return $error; |
---|
604 | } |
---|
605 | /* 334: Continue authentication request */ |
---|
606 | if (PEAR::isError($error = $this->_parseResponse(334))) { |
---|
607 | /* 503: Error: already authenticated */ |
---|
608 | if ($this->_code === 503) { |
---|
609 | return true; |
---|
610 | } |
---|
611 | return $error; |
---|
612 | } |
---|
613 | |
---|
614 | if (PEAR::isError($error = $this->_put(base64_encode($uid)))) { |
---|
615 | return $error; |
---|
616 | } |
---|
617 | /* 334: Continue authentication request */ |
---|
618 | if (PEAR::isError($error = $this->_parseResponse(334))) { |
---|
619 | return $error; |
---|
620 | } |
---|
621 | |
---|
622 | if (PEAR::isError($error = $this->_put(base64_encode($pwd)))) { |
---|
623 | return $error; |
---|
624 | } |
---|
625 | |
---|
626 | /* 235: Authentication successful */ |
---|
627 | if (PEAR::isError($error = $this->_parseResponse(235))) { |
---|
628 | return $error; |
---|
629 | } |
---|
630 | |
---|
631 | return true; |
---|
632 | } |
---|
633 | |
---|
634 | /** |
---|
635 | * Authenticates the user using the PLAIN method. |
---|
636 | * |
---|
637 | * @param string The userid to authenticate as. |
---|
638 | * @param string The password to authenticate with. |
---|
639 | * |
---|
640 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
641 | * kind of failure, or true on success. |
---|
642 | * @access private |
---|
643 | * @since 1.1.0 |
---|
644 | */ |
---|
645 | function _authPlain($uid, $pwd) |
---|
646 | { |
---|
647 | if (PEAR::isError($error = $this->_put('AUTH', 'PLAIN'))) { |
---|
648 | return $error; |
---|
649 | } |
---|
650 | /* 334: Continue authentication request */ |
---|
651 | if (PEAR::isError($error = $this->_parseResponse(334))) { |
---|
652 | /* 503: Error: already authenticated */ |
---|
653 | if ($this->_code === 503) { |
---|
654 | return true; |
---|
655 | } |
---|
656 | return $error; |
---|
657 | } |
---|
658 | |
---|
659 | $auth_str = base64_encode(chr(0) . $uid . chr(0) . $pwd); |
---|
660 | |
---|
661 | if (PEAR::isError($error = $this->_put($auth_str))) { |
---|
662 | return $error; |
---|
663 | } |
---|
664 | |
---|
665 | /* 235: Authentication successful */ |
---|
666 | if (PEAR::isError($error = $this->_parseResponse(235))) { |
---|
667 | return $error; |
---|
668 | } |
---|
669 | |
---|
670 | return true; |
---|
671 | } |
---|
672 | |
---|
673 | /** |
---|
674 | * Send the HELO command. |
---|
675 | * |
---|
676 | * @param string The domain name to say we are. |
---|
677 | * |
---|
678 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
679 | * kind of failure, or true on success. |
---|
680 | * @access public |
---|
681 | * @since 1.0 |
---|
682 | */ |
---|
683 | function helo($domain) |
---|
684 | { |
---|
685 | if (PEAR::isError($error = $this->_put('HELO', $domain))) { |
---|
686 | return $error; |
---|
687 | } |
---|
688 | if (PEAR::isError($error = $this->_parseResponse(250))) { |
---|
689 | return $error; |
---|
690 | } |
---|
691 | |
---|
692 | return true; |
---|
693 | } |
---|
694 | |
---|
695 | /** |
---|
696 | * Send the MAIL FROM: command. |
---|
697 | * |
---|
698 | * @param string $sender The sender (reverse path) to set. |
---|
699 | * @param string $params String containing additional MAIL parameters, |
---|
700 | * such as the NOTIFY flags defined by RFC 1891 |
---|
701 | * or the VERP protocol. |
---|
702 | * |
---|
703 | * If $params is an array, only the 'verp' option |
---|
704 | * is supported. If 'verp' is true, the XVERP |
---|
705 | * parameter is appended to the MAIL command. If |
---|
706 | * the 'verp' value is a string, the full |
---|
707 | * XVERP=value parameter is appended. |
---|
708 | * |
---|
709 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
710 | * kind of failure, or true on success. |
---|
711 | * @access public |
---|
712 | * @since 1.0 |
---|
713 | */ |
---|
714 | function mailFrom($sender, $params = null) |
---|
715 | { |
---|
716 | $args = "FROM:<$sender>"; |
---|
717 | |
---|
718 | /* Support the deprecated array form of $params. */ |
---|
719 | if (is_array($params) && isset($params['verp'])) { |
---|
720 | /* XVERP */ |
---|
721 | if ($params['verp'] === true) { |
---|
722 | $args .= ' XVERP'; |
---|
723 | |
---|
724 | /* XVERP=something */ |
---|
725 | } elseif (trim($params['verp'])) { |
---|
726 | $args .= ' XVERP=' . $params['verp']; |
---|
727 | } |
---|
728 | } elseif (is_string($params)) { |
---|
729 | $args .= ' ' . $params; |
---|
730 | } |
---|
731 | |
---|
732 | if (PEAR::isError($error = $this->_put('MAIL', $args))) { |
---|
733 | return $error; |
---|
734 | } |
---|
735 | if (PEAR::isError($error = $this->_parseResponse(250))) { |
---|
736 | return $error; |
---|
737 | } |
---|
738 | |
---|
739 | return true; |
---|
740 | } |
---|
741 | |
---|
742 | /** |
---|
743 | * Send the RCPT TO: command. |
---|
744 | * |
---|
745 | * @param string $recipient The recipient (forward path) to add. |
---|
746 | * @param string $params String containing additional RCPT parameters, |
---|
747 | * such as the NOTIFY flags defined by RFC 1891. |
---|
748 | * |
---|
749 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
750 | * kind of failure, or true on success. |
---|
751 | * |
---|
752 | * @access public |
---|
753 | * @since 1.0 |
---|
754 | */ |
---|
755 | function rcptTo($recipient, $params = null) |
---|
756 | { |
---|
757 | $args = "TO:<$recipient>"; |
---|
758 | if (is_string($params)) { |
---|
759 | $args .= ' ' . $params; |
---|
760 | } |
---|
761 | |
---|
762 | if (PEAR::isError($error = $this->_put('RCPT', $args))) { |
---|
763 | return $error; |
---|
764 | } |
---|
765 | if (PEAR::isError($error = $this->_parseResponse(array(250, 251)))) { |
---|
766 | return $error; |
---|
767 | } |
---|
768 | |
---|
769 | return true; |
---|
770 | } |
---|
771 | |
---|
772 | /** |
---|
773 | * Quote the data so that it meets SMTP standards. |
---|
774 | * |
---|
775 | * This is provided as a separate public function to facilitate |
---|
776 | * easier overloading for the cases where it is desirable to |
---|
777 | * customize the quoting behavior. |
---|
778 | * |
---|
779 | * @param string $data The message text to quote. The string must be passed |
---|
780 | * by reference, and the text will be modified in place. |
---|
781 | * |
---|
782 | * @access public |
---|
783 | * @since 1.2 |
---|
784 | */ |
---|
785 | function quotedata(&$data) |
---|
786 | { |
---|
787 | /* Change Unix (\n) and Mac (\r) linefeeds into |
---|
788 | * Internet-standard CRLF (\r\n) linefeeds. */ |
---|
789 | $data = preg_replace(array('/(?<!\r)\n/','/\r(?!\n)/'), "\r\n", $data); |
---|
790 | |
---|
791 | /* Because a single leading period (.) signifies an end to the |
---|
792 | * data, legitimate leading periods need to be "doubled" |
---|
793 | * (e.g. '..'). */ |
---|
794 | $data = str_replace("\n.", "\n..", $data); |
---|
795 | } |
---|
796 | |
---|
797 | /** |
---|
798 | * Send the DATA command. |
---|
799 | * |
---|
800 | * @param string $data The message body to send. |
---|
801 | * |
---|
802 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
803 | * kind of failure, or true on success. |
---|
804 | * @access public |
---|
805 | * @since 1.0 |
---|
806 | */ |
---|
807 | function data($data) |
---|
808 | { |
---|
809 | /* RFC 1870, section 3, subsection 3 states "a value of zero |
---|
810 | * indicates that no fixed maximum message size is in force". |
---|
811 | * Furthermore, it says that if "the parameter is omitted no |
---|
812 | * information is conveyed about the server's fixed maximum |
---|
813 | * message size". */ |
---|
814 | if (isset($this->_esmtp['SIZE']) && ($this->_esmtp['SIZE'] > 0)) { |
---|
815 | if (strlen($data) >= $this->_esmtp['SIZE']) { |
---|
816 | $this->disconnect(); |
---|
817 | return PEAR::raiseError('Message size excedes the server limit'); |
---|
818 | } |
---|
819 | } |
---|
820 | |
---|
821 | /* Quote the data based on the SMTP standards. */ |
---|
822 | $this->quotedata($data); |
---|
823 | |
---|
824 | if (PEAR::isError($error = $this->_put('DATA'))) { |
---|
825 | return $error; |
---|
826 | } |
---|
827 | if (PEAR::isError($error = $this->_parseResponse(354))) { |
---|
828 | return $error; |
---|
829 | } |
---|
830 | |
---|
831 | if (PEAR::isError($result = $this->_send($data . "\r\n.\r\n"))) { |
---|
832 | return $result; |
---|
833 | } |
---|
834 | if (PEAR::isError($error = $this->_parseResponse(250))) { |
---|
835 | return $error; |
---|
836 | } |
---|
837 | |
---|
838 | return true; |
---|
839 | } |
---|
840 | |
---|
841 | /** |
---|
842 | * Send the SEND FROM: command. |
---|
843 | * |
---|
844 | * @param string The reverse path to send. |
---|
845 | * |
---|
846 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
847 | * kind of failure, or true on success. |
---|
848 | * @access public |
---|
849 | * @since 1.2.6 |
---|
850 | */ |
---|
851 | function sendFrom($path) |
---|
852 | { |
---|
853 | if (PEAR::isError($error = $this->_put('SEND', "FROM:<$path>"))) { |
---|
854 | return $error; |
---|
855 | } |
---|
856 | if (PEAR::isError($error = $this->_parseResponse(250))) { |
---|
857 | return $error; |
---|
858 | } |
---|
859 | |
---|
860 | return true; |
---|
861 | } |
---|
862 | |
---|
863 | /** |
---|
864 | * Backwards-compatibility wrapper for sendFrom(). |
---|
865 | * |
---|
866 | * @param string The reverse path to send. |
---|
867 | * |
---|
868 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
869 | * kind of failure, or true on success. |
---|
870 | * |
---|
871 | * @access public |
---|
872 | * @since 1.0 |
---|
873 | * @deprecated 1.2.6 |
---|
874 | */ |
---|
875 | function send_from($path) |
---|
876 | { |
---|
877 | return sendFrom($path); |
---|
878 | } |
---|
879 | |
---|
880 | /** |
---|
881 | * Send the SOML FROM: command. |
---|
882 | * |
---|
883 | * @param string The reverse path to send. |
---|
884 | * |
---|
885 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
886 | * kind of failure, or true on success. |
---|
887 | * @access public |
---|
888 | * @since 1.2.6 |
---|
889 | */ |
---|
890 | function somlFrom($path) |
---|
891 | { |
---|
892 | if (PEAR::isError($error = $this->_put('SOML', "FROM:<$path>"))) { |
---|
893 | return $error; |
---|
894 | } |
---|
895 | if (PEAR::isError($error = $this->_parseResponse(250))) { |
---|
896 | return $error; |
---|
897 | } |
---|
898 | |
---|
899 | return true; |
---|
900 | } |
---|
901 | |
---|
902 | /** |
---|
903 | * Backwards-compatibility wrapper for somlFrom(). |
---|
904 | * |
---|
905 | * @param string The reverse path to send. |
---|
906 | * |
---|
907 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
908 | * kind of failure, or true on success. |
---|
909 | * |
---|
910 | * @access public |
---|
911 | * @since 1.0 |
---|
912 | * @deprecated 1.2.6 |
---|
913 | */ |
---|
914 | function soml_from($path) |
---|
915 | { |
---|
916 | return somlFrom($path); |
---|
917 | } |
---|
918 | |
---|
919 | /** |
---|
920 | * Send the SAML FROM: command. |
---|
921 | * |
---|
922 | * @param string The reverse path to send. |
---|
923 | * |
---|
924 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
925 | * kind of failure, or true on success. |
---|
926 | * @access public |
---|
927 | * @since 1.2.6 |
---|
928 | */ |
---|
929 | function samlFrom($path) |
---|
930 | { |
---|
931 | if (PEAR::isError($error = $this->_put('SAML', "FROM:<$path>"))) { |
---|
932 | return $error; |
---|
933 | } |
---|
934 | if (PEAR::isError($error = $this->_parseResponse(250))) { |
---|
935 | return $error; |
---|
936 | } |
---|
937 | |
---|
938 | return true; |
---|
939 | } |
---|
940 | |
---|
941 | /** |
---|
942 | * Backwards-compatibility wrapper for samlFrom(). |
---|
943 | * |
---|
944 | * @param string The reverse path to send. |
---|
945 | * |
---|
946 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
947 | * kind of failure, or true on success. |
---|
948 | * |
---|
949 | * @access public |
---|
950 | * @since 1.0 |
---|
951 | * @deprecated 1.2.6 |
---|
952 | */ |
---|
953 | function saml_from($path) |
---|
954 | { |
---|
955 | return samlFrom($path); |
---|
956 | } |
---|
957 | |
---|
958 | /** |
---|
959 | * Send the RSET command. |
---|
960 | * |
---|
961 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
962 | * kind of failure, or true on success. |
---|
963 | * @access public |
---|
964 | * @since 1.0 |
---|
965 | */ |
---|
966 | function rset() |
---|
967 | { |
---|
968 | if (PEAR::isError($error = $this->_put('RSET'))) { |
---|
969 | return $error; |
---|
970 | } |
---|
971 | if (PEAR::isError($error = $this->_parseResponse(250))) { |
---|
972 | return $error; |
---|
973 | } |
---|
974 | |
---|
975 | return true; |
---|
976 | } |
---|
977 | |
---|
978 | /** |
---|
979 | * Send the VRFY command. |
---|
980 | * |
---|
981 | * @param string The string to verify |
---|
982 | * |
---|
983 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
984 | * kind of failure, or true on success. |
---|
985 | * @access public |
---|
986 | * @since 1.0 |
---|
987 | */ |
---|
988 | function vrfy($string) |
---|
989 | { |
---|
990 | /* Note: 251 is also a valid response code */ |
---|
991 | if (PEAR::isError($error = $this->_put('VRFY', $string))) { |
---|
992 | return $error; |
---|
993 | } |
---|
994 | if (PEAR::isError($error = $this->_parseResponse(array(250, 252)))) { |
---|
995 | return $error; |
---|
996 | } |
---|
997 | |
---|
998 | return true; |
---|
999 | } |
---|
1000 | |
---|
1001 | /** |
---|
1002 | * Send the NOOP command. |
---|
1003 | * |
---|
1004 | * @return mixed Returns a PEAR_Error with an error message on any |
---|
1005 | * kind of failure, or true on success. |
---|
1006 | * @access public |
---|
1007 | * @since 1.0 |
---|
1008 | */ |
---|
1009 | function noop() |
---|
1010 | { |
---|
1011 | if (PEAR::isError($error = $this->_put('NOOP'))) { |
---|
1012 | return $error; |
---|
1013 | } |
---|
1014 | if (PEAR::isError($error = $this->_parseResponse(250))) { |
---|
1015 | return $error; |
---|
1016 | } |
---|
1017 | |
---|
1018 | return true; |
---|
1019 | } |
---|
1020 | |
---|
1021 | /** |
---|
1022 | * Backwards-compatibility method. identifySender()'s functionality is |
---|
1023 | * now handled internally. |
---|
1024 | * |
---|
1025 | * @return boolean This method always return true. |
---|
1026 | * |
---|
1027 | * @access public |
---|
1028 | * @since 1.0 |
---|
1029 | */ |
---|
1030 | function identifySender() |
---|
1031 | { |
---|
1032 | return true; |
---|
1033 | } |
---|
1034 | |
---|
1035 | } |
---|