0
|
1 <?php
|
|
2
|
|
3 /**
|
|
4 +-----------------------------------------------------------------------+
|
|
5 | This file is part of the Roundcube Webmail client |
|
|
6 | Copyright (C) 2005-2012, The Roundcube Dev Team |
|
|
7 | |
|
|
8 | Licensed under the GNU General Public License version 3 or |
|
|
9 | any later version with exceptions for skins & plugins. |
|
|
10 | See the README file for a full license statement. |
|
|
11 | |
|
|
12 | PURPOSE: |
|
|
13 | Provide SMTP functionality using socket connections |
|
|
14 +-----------------------------------------------------------------------+
|
|
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
|
|
16 +-----------------------------------------------------------------------+
|
|
17 */
|
|
18
|
|
19 /**
|
|
20 * Class to provide SMTP functionality using PEAR Net_SMTP
|
|
21 *
|
|
22 * @package Framework
|
|
23 * @subpackage Mail
|
|
24 * @author Thomas Bruederli <roundcube@gmail.com>
|
|
25 * @author Aleksander Machniak <alec@alec.pl>
|
|
26 */
|
|
27 class rcube_smtp
|
|
28 {
|
|
29 private $conn;
|
|
30 private $response;
|
|
31 private $error;
|
|
32 private $anonymize_log = 0;
|
|
33
|
|
34 // define headers delimiter
|
|
35 const SMTP_MIME_CRLF = "\r\n";
|
|
36
|
|
37 const DEBUG_LINE_LENGTH = 4098; // 4KB + 2B for \r\n
|
|
38
|
|
39
|
|
40 /**
|
|
41 * SMTP Connection and authentication
|
|
42 *
|
|
43 * @param string Server host
|
|
44 * @param string Server port
|
|
45 * @param string User name
|
|
46 * @param string Password
|
|
47 *
|
|
48 * @return bool Returns true on success, or false on error
|
|
49 */
|
|
50 public function connect($host = null, $port = null, $user = null, $pass = null)
|
|
51 {
|
|
52 $rcube = rcube::get_instance();
|
|
53
|
|
54 // disconnect/destroy $this->conn
|
|
55 $this->disconnect();
|
|
56
|
|
57 // reset error/response var
|
|
58 $this->error = $this->response = null;
|
|
59
|
|
60 // let plugins alter smtp connection config
|
|
61 $CONFIG = $rcube->plugins->exec_hook('smtp_connect', array(
|
|
62 'smtp_server' => $host ?: $rcube->config->get('smtp_server'),
|
|
63 'smtp_port' => $port ?: $rcube->config->get('smtp_port', 25),
|
|
64 'smtp_user' => $user !== null ? $user : $rcube->config->get('smtp_user'),
|
|
65 'smtp_pass' => $pass !== null ? $pass : $rcube->config->get('smtp_pass'),
|
|
66 'smtp_auth_cid' => $rcube->config->get('smtp_auth_cid'),
|
|
67 'smtp_auth_pw' => $rcube->config->get('smtp_auth_pw'),
|
|
68 'smtp_auth_type' => $rcube->config->get('smtp_auth_type'),
|
|
69 'smtp_helo_host' => $rcube->config->get('smtp_helo_host'),
|
|
70 'smtp_timeout' => $rcube->config->get('smtp_timeout'),
|
|
71 'smtp_conn_options' => $rcube->config->get('smtp_conn_options'),
|
|
72 'smtp_auth_callbacks' => array(),
|
|
73 ));
|
|
74
|
|
75 $smtp_host = rcube_utils::parse_host($CONFIG['smtp_server']);
|
|
76 // when called from Installer it's possible to have empty $smtp_host here
|
|
77 if (!$smtp_host) $smtp_host = 'localhost';
|
|
78 $smtp_port = is_numeric($CONFIG['smtp_port']) ? $CONFIG['smtp_port'] : 25;
|
|
79 $smtp_host_url = parse_url($smtp_host);
|
|
80
|
|
81 // overwrite port
|
|
82 if (isset($smtp_host_url['host']) && isset($smtp_host_url['port'])) {
|
|
83 $smtp_host = $smtp_host_url['host'];
|
|
84 $smtp_port = $smtp_host_url['port'];
|
|
85 }
|
|
86
|
|
87 // re-write smtp host
|
|
88 if (isset($smtp_host_url['host']) && isset($smtp_host_url['scheme'])) {
|
|
89 $smtp_host = sprintf('%s://%s', $smtp_host_url['scheme'], $smtp_host_url['host']);
|
|
90 }
|
|
91
|
|
92 // remove TLS prefix and set flag for use in Net_SMTP::auth()
|
|
93 if (preg_match('#^tls://#i', $smtp_host)) {
|
|
94 $smtp_host = preg_replace('#^tls://#i', '', $smtp_host);
|
|
95 $use_tls = true;
|
|
96 }
|
|
97
|
|
98 // Handle per-host socket options
|
|
99 rcube_utils::parse_socket_options($CONFIG['smtp_conn_options'], $smtp_host);
|
|
100
|
|
101 if (!empty($CONFIG['smtp_helo_host'])) {
|
|
102 $helo_host = $CONFIG['smtp_helo_host'];
|
|
103 }
|
|
104 else if (!empty($_SERVER['SERVER_NAME'])) {
|
|
105 $helo_host = preg_replace('/:\d+$/', '', $_SERVER['SERVER_NAME']);
|
|
106 }
|
|
107 else {
|
|
108 $helo_host = 'localhost';
|
|
109 }
|
|
110
|
|
111 // IDNA Support
|
|
112 $smtp_host = rcube_utils::idn_to_ascii($smtp_host);
|
|
113
|
|
114 $this->conn = new Net_SMTP($smtp_host, $smtp_port, $helo_host, false, 0, $CONFIG['smtp_conn_options']);
|
|
115
|
|
116 if ($rcube->config->get('smtp_debug')) {
|
|
117 $this->conn->setDebug(true, array($this, 'debug_handler'));
|
|
118 $this->anonymize_log = 0;
|
|
119 }
|
|
120
|
|
121 // register authentication methods
|
|
122 if (!empty($CONFIG['smtp_auth_callbacks']) && method_exists($this->conn, 'setAuthMethod')) {
|
|
123 foreach ($CONFIG['smtp_auth_callbacks'] as $callback) {
|
|
124 $this->conn->setAuthMethod($callback['name'], $callback['function'],
|
|
125 isset($callback['prepend']) ? $callback['prepend'] : true);
|
|
126 }
|
|
127 }
|
|
128
|
|
129 // try to connect to server and exit on failure
|
|
130 $result = $this->conn->connect($CONFIG['smtp_timeout']);
|
|
131
|
|
132 if (is_a($result, 'PEAR_Error')) {
|
|
133 $this->response[] = "Connection failed: " . $result->getMessage();
|
|
134
|
|
135 list($code,) = $this->conn->getResponse();
|
|
136 $this->error = array('label' => 'smtpconnerror', 'vars' => array('code' => $code));
|
|
137 $this->conn = null;
|
|
138
|
|
139 return false;
|
|
140 }
|
|
141
|
|
142 // workaround for timeout bug in Net_SMTP 1.5.[0-1] (#1487843)
|
|
143 if (method_exists($this->conn, 'setTimeout')
|
|
144 && ($timeout = ini_get('default_socket_timeout'))
|
|
145 ) {
|
|
146 $this->conn->setTimeout($timeout);
|
|
147 }
|
|
148
|
|
149 $smtp_user = str_replace('%u', $rcube->get_user_name(), $CONFIG['smtp_user']);
|
|
150 $smtp_pass = str_replace('%p', $rcube->get_user_password(), $CONFIG['smtp_pass']);
|
|
151 $smtp_auth_type = $CONFIG['smtp_auth_type'] ?: null;
|
|
152
|
|
153 if (!empty($CONFIG['smtp_auth_cid'])) {
|
|
154 $smtp_authz = $smtp_user;
|
|
155 $smtp_user = $CONFIG['smtp_auth_cid'];
|
|
156 $smtp_pass = $CONFIG['smtp_auth_pw'];
|
|
157 }
|
|
158
|
|
159 // attempt to authenticate to the SMTP server
|
|
160 if ($smtp_user && $smtp_pass) {
|
|
161 // IDNA Support
|
|
162 if (strpos($smtp_user, '@')) {
|
|
163 $smtp_user = rcube_utils::idn_to_ascii($smtp_user);
|
|
164 }
|
|
165
|
|
166 $result = $this->conn->auth($smtp_user, $smtp_pass, $smtp_auth_type, $use_tls, $smtp_authz);
|
|
167
|
|
168 if (is_a($result, 'PEAR_Error')) {
|
|
169 list($code,) = $this->conn->getResponse();
|
|
170 $this->error = array('label' => 'smtpautherror', 'vars' => array('code' => $code));
|
|
171 $this->response[] = 'Authentication failure: ' . $result->getMessage()
|
|
172 . ' (Code: ' . $result->getCode() . ')';
|
|
173
|
|
174 $this->reset();
|
|
175 $this->disconnect();
|
|
176
|
|
177 return false;
|
|
178 }
|
|
179 }
|
|
180
|
|
181 return true;
|
|
182 }
|
|
183
|
|
184 /**
|
|
185 * Function for sending mail
|
|
186 *
|
|
187 * @param string Sender e-Mail address
|
|
188 *
|
|
189 * @param mixed Either a comma-separated list of recipients
|
|
190 * (RFC822 compliant), or an array of recipients,
|
|
191 * each RFC822 valid. This may contain recipients not
|
|
192 * specified in the headers, for Bcc:, resending
|
|
193 * messages, etc.
|
|
194 * @param mixed The message headers to send with the mail
|
|
195 * Either as an associative array or a finally
|
|
196 * formatted string
|
|
197 * @param mixed The full text of the message body, including any Mime parts
|
|
198 * or file handle
|
|
199 * @param array Delivery options (e.g. DSN request)
|
|
200 *
|
|
201 * @return bool Returns true on success, or false on error
|
|
202 */
|
|
203 public function send_mail($from, $recipients, &$headers, &$body, $opts=null)
|
|
204 {
|
|
205 if (!is_object($this->conn)) {
|
|
206 return false;
|
|
207 }
|
|
208
|
|
209 // prepare message headers as string
|
|
210 if (is_array($headers)) {
|
|
211 if (!($headerElements = $this->_prepare_headers($headers))) {
|
|
212 $this->reset();
|
|
213 return false;
|
|
214 }
|
|
215
|
|
216 list($from, $text_headers) = $headerElements;
|
|
217 }
|
|
218 else if (is_string($headers)) {
|
|
219 $text_headers = $headers;
|
|
220 }
|
|
221
|
|
222 // exit if no from address is given
|
|
223 if (!isset($from)) {
|
|
224 $this->reset();
|
|
225 $this->response[] = "No From address has been provided";
|
|
226 return false;
|
|
227 }
|
|
228
|
|
229 // RFC3461: Delivery Status Notification
|
|
230 if ($opts['dsn']) {
|
|
231 $exts = $this->conn->getServiceExtensions();
|
|
232
|
|
233 if (isset($exts['DSN'])) {
|
|
234 $from_params = 'RET=HDRS';
|
|
235 $recipient_params = 'NOTIFY=SUCCESS,FAILURE';
|
|
236 }
|
|
237 }
|
|
238
|
|
239 // RFC2298.3: remove envelope sender address
|
|
240 if (empty($opts['mdn_use_from'])
|
|
241 && preg_match('/Content-Type: multipart\/report/', $text_headers)
|
|
242 && preg_match('/report-type=disposition-notification/', $text_headers)
|
|
243 ) {
|
|
244 $from = '';
|
|
245 }
|
|
246
|
|
247 // set From: address
|
|
248 $result = $this->conn->mailFrom($from, $from_params);
|
|
249 if (is_a($result, 'PEAR_Error')) {
|
|
250 $err = $this->conn->getResponse();
|
|
251 $this->error = array('label' => 'smtpfromerror', 'vars' => array(
|
|
252 'from' => $from, 'code' => $err[0], 'msg' => $err[1]));
|
|
253 $this->response[] = "Failed to set sender '$from'. "
|
|
254 . $err[1] . ' (Code: ' . $err[0] . ')';
|
|
255 $this->reset();
|
|
256 return false;
|
|
257 }
|
|
258
|
|
259 // prepare list of recipients
|
|
260 $recipients = $this->_parse_rfc822($recipients);
|
|
261 if (is_a($recipients, 'PEAR_Error')) {
|
|
262 $this->error = array('label' => 'smtprecipientserror');
|
|
263 $this->reset();
|
|
264 return false;
|
|
265 }
|
|
266
|
|
267 // set mail recipients
|
|
268 foreach ($recipients as $recipient) {
|
|
269 $result = $this->conn->rcptTo($recipient, $recipient_params);
|
|
270 if (is_a($result, 'PEAR_Error')) {
|
|
271 $err = $this->conn->getResponse();
|
|
272 $this->error = array('label' => 'smtptoerror', 'vars' => array(
|
|
273 'to' => $recipient, 'code' => $err[0], 'msg' => $err[1]));
|
|
274 $this->response[] = "Failed to add recipient '$recipient'. "
|
|
275 . $err[1] . ' (Code: ' . $err[0] . ')';
|
|
276 $this->reset();
|
|
277 return false;
|
|
278 }
|
|
279 }
|
|
280
|
|
281 if (is_resource($body)) {
|
|
282 // file handle
|
|
283 $data = $body;
|
|
284
|
|
285 if ($text_headers) {
|
|
286 $text_headers = preg_replace('/[\r\n]+$/', '', $text_headers);
|
|
287 }
|
|
288 }
|
|
289 else {
|
|
290 // Concatenate headers and body so it can be passed by reference to SMTP_CONN->data
|
|
291 // so preg_replace in SMTP_CONN->quotedata will store a reference instead of a copy.
|
|
292 // We are still forced to make another copy here for a couple ticks so we don't really
|
|
293 // get to save a copy in the method call.
|
|
294 $data = $text_headers . "\r\n" . $body;
|
|
295
|
|
296 // unset old vars to save data and so we can pass into SMTP_CONN->data by reference.
|
|
297 unset($text_headers, $body);
|
|
298 }
|
|
299
|
|
300 // Send the message's headers and the body as SMTP data.
|
|
301 $result = $this->conn->data($data, $text_headers);
|
|
302 if (is_a($result, 'PEAR_Error')) {
|
|
303 $err = $this->conn->getResponse();
|
|
304 if (!in_array($err[0], array(354, 250, 221))) {
|
|
305 $msg = sprintf('[%d] %s', $err[0], $err[1]);
|
|
306 }
|
|
307 else {
|
|
308 $msg = $result->getMessage();
|
|
309 }
|
|
310
|
|
311 $this->error = array('label' => 'smtperror', 'vars' => array('msg' => $msg));
|
|
312 $this->response[] = "Failed to send data. " . $msg;
|
|
313 $this->reset();
|
|
314 return false;
|
|
315 }
|
|
316
|
|
317 $this->response[] = join(': ', $this->conn->getResponse());
|
|
318 return true;
|
|
319 }
|
|
320
|
|
321 /**
|
|
322 * Reset the global SMTP connection
|
|
323 */
|
|
324 public function reset()
|
|
325 {
|
|
326 if (is_object($this->conn)) {
|
|
327 $this->conn->rset();
|
|
328 }
|
|
329 }
|
|
330
|
|
331 /**
|
|
332 * Disconnect the global SMTP connection
|
|
333 */
|
|
334 public function disconnect()
|
|
335 {
|
|
336 if (is_object($this->conn)) {
|
|
337 $this->conn->disconnect();
|
|
338 $this->conn = null;
|
|
339 }
|
|
340 }
|
|
341
|
|
342 /**
|
|
343 * This is our own debug handler for the SMTP connection
|
|
344 */
|
|
345 public function debug_handler(&$smtp, $message)
|
|
346 {
|
|
347 // catch AUTH commands and set anonymization flag for subsequent sends
|
|
348 if (preg_match('/^Send: AUTH ([A-Z]+)/', $message, $m)) {
|
|
349 $this->anonymize_log = $m[1] == 'LOGIN' ? 2 : 1;
|
|
350 }
|
|
351 // anonymize this log entry
|
|
352 else if ($this->anonymize_log > 0 && strpos($message, 'Send:') === 0 && --$this->anonymize_log == 0) {
|
|
353 $message = sprintf('Send: ****** [%d]', strlen($message) - 8);
|
|
354 }
|
|
355
|
|
356 if (($len = strlen($message)) > self::DEBUG_LINE_LENGTH) {
|
|
357 $diff = $len - self::DEBUG_LINE_LENGTH;
|
|
358 $message = substr($message, 0, self::DEBUG_LINE_LENGTH)
|
|
359 . "... [truncated $diff bytes]";
|
|
360 }
|
|
361
|
|
362 rcube::write_log('smtp', preg_replace('/\r\n$/', '', $message));
|
|
363 }
|
|
364
|
|
365 /**
|
|
366 * Get error message
|
|
367 */
|
|
368 public function get_error()
|
|
369 {
|
|
370 return $this->error;
|
|
371 }
|
|
372
|
|
373 /**
|
|
374 * Get server response messages array
|
|
375 */
|
|
376 public function get_response()
|
|
377 {
|
|
378 return $this->response;
|
|
379 }
|
|
380
|
|
381 /**
|
|
382 * Take an array of mail headers and return a string containing
|
|
383 * text usable in sending a message.
|
|
384 *
|
|
385 * @param array $headers The array of headers to prepare, in an associative
|
|
386 * array, where the array key is the header name (ie,
|
|
387 * 'Subject'), and the array value is the header
|
|
388 * value (ie, 'test'). The header produced from those
|
|
389 * values would be 'Subject: test'.
|
|
390 *
|
|
391 * @return mixed Returns false if it encounters a bad address,
|
|
392 * otherwise returns an array containing two
|
|
393 * elements: Any From: address found in the headers,
|
|
394 * and the plain text version of the headers.
|
|
395 */
|
|
396 private function _prepare_headers($headers)
|
|
397 {
|
|
398 $lines = array();
|
|
399 $from = null;
|
|
400
|
|
401 foreach ($headers as $key => $value) {
|
|
402 if (strcasecmp($key, 'From') === 0) {
|
|
403 $addresses = $this->_parse_rfc822($value);
|
|
404
|
|
405 if (is_array($addresses)) {
|
|
406 $from = $addresses[0];
|
|
407 }
|
|
408
|
|
409 // Reject envelope From: addresses with spaces.
|
|
410 if (strpos($from, ' ') !== false) {
|
|
411 return false;
|
|
412 }
|
|
413
|
|
414 $lines[] = $key . ': ' . $value;
|
|
415 }
|
|
416 else if (strcasecmp($key, 'Received') === 0) {
|
|
417 $received = array();
|
|
418 if (is_array($value)) {
|
|
419 foreach ($value as $line) {
|
|
420 $received[] = $key . ': ' . $line;
|
|
421 }
|
|
422 }
|
|
423 else {
|
|
424 $received[] = $key . ': ' . $value;
|
|
425 }
|
|
426
|
|
427 // Put Received: headers at the top. Spam detectors often
|
|
428 // flag messages with Received: headers after the Subject:
|
|
429 // as spam.
|
|
430 $lines = array_merge($received, $lines);
|
|
431 }
|
|
432 else {
|
|
433 // If $value is an array (i.e., a list of addresses), convert
|
|
434 // it to a comma-delimited string of its elements (addresses).
|
|
435 if (is_array($value)) {
|
|
436 $value = implode(', ', $value);
|
|
437 }
|
|
438
|
|
439 $lines[] = $key . ': ' . $value;
|
|
440 }
|
|
441 }
|
|
442
|
|
443 return array($from, join(self::SMTP_MIME_CRLF, $lines) . self::SMTP_MIME_CRLF);
|
|
444 }
|
|
445
|
|
446 /**
|
|
447 * Take a set of recipients and parse them, returning an array of
|
|
448 * bare addresses (forward paths) that can be passed to sendmail
|
|
449 * or an smtp server with the rcpt to: command.
|
|
450 *
|
|
451 * @param mixed Either a comma-separated list of recipients
|
|
452 * (RFC822 compliant), or an array of recipients,
|
|
453 * each RFC822 valid.
|
|
454 *
|
|
455 * @return array An array of forward paths (bare addresses).
|
|
456 */
|
|
457 private function _parse_rfc822($recipients)
|
|
458 {
|
|
459 // if we're passed an array, assume addresses are valid and implode them before parsing.
|
|
460 if (is_array($recipients)) {
|
|
461 $recipients = implode(', ', $recipients);
|
|
462 }
|
|
463
|
|
464 $addresses = array();
|
|
465 $recipients = preg_replace('/[\s\t]*\r?\n/', '', $recipients);
|
|
466 $recipients = rcube_utils::explode_quoted_string(',', $recipients);
|
|
467
|
|
468 reset($recipients);
|
|
469 foreach ($recipients as $recipient) {
|
|
470 $a = rcube_utils::explode_quoted_string(' ', $recipient);
|
|
471 foreach ($a as $word) {
|
|
472 $word = trim($word);
|
|
473 $len = strlen($word);
|
|
474
|
|
475 if ($len && strpos($word, "@") > 0 && $word[$len-1] != '"') {
|
|
476 $word = preg_replace('/^<|>$/', '', $word);
|
|
477 if (!in_array($word, $addresses)) {
|
|
478 array_push($addresses, $word);
|
|
479 }
|
|
480 }
|
|
481 }
|
|
482 }
|
|
483
|
|
484 return $addresses;
|
|
485 }
|
|
486 }
|