4 +-----------------------------------------------------------------------+
5 | program/include/rcube_smtp.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Provide SMTP functionality using socket connections |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: rcube_smtp.inc 2291 2009-02-13 10:44:49Z alec $
23 * SMTP delivery functions
28 // include required PEAR classes
29 require_once('Net/SMTP.php');
32 // define headers delimiter
33 define('SMTP_MIME_CRLF', "\r\n");
38 * Function for sending mail using SMTP.
40 * @param string Sender e-Mail address
42 * @param mixed Either a comma-seperated list of recipients
43 * (RFC822 compliant), or an array of recipients,
44 * each RFC822 valid. This may contain recipients not
45 * specified in the headers, for Bcc:, resending
48 * @param mixed The message headers to send with the mail
49 * Either as an associative array or a finally
52 * @param string The full text of the message body, including any Mime parts, etc.
54 * @return bool Returns TRUE on success, or FALSE on error
56 function smtp_mail($from, $recipients, &$headers, &$body, &$response)
58 global $SMTP_CONN, $CONFIG, $RCMAIL;
60 $smtp_host = $CONFIG['smtp_server'];
61 $smtp_port = is_numeric($CONFIG['smtp_port']) ? $CONFIG['smtp_port'] : 25;
62 $smtp_host_url = parse_url($CONFIG['smtp_server']);
65 if (isset($smtp_host_url['host']) && isset($smtp_host_url['port']))
67 $smtp_host = $smtp_host_url['host'];
68 $smtp_port = $smtp_host_url['port'];
72 if (isset($smtp_host_url['host']) && isset($smtp_host_url['scheme']))
73 $smtp_host = sprintf('%s://%s', $smtp_host_url['scheme'], $smtp_host_url['host']);
76 // create Net_SMTP object and connect to server
77 if (!is_object($SMTP_CONN))
79 if (!empty($CONFIG['smtp_helo_host']))
80 $helo_host = $CONFIG['smtp_helo_host'];
81 else if (!empty($_SERVER['SERVER_NAME']))
82 $helo_host = preg_replace('/:\d+$/', '', $_SERVER['SERVER_NAME']);
84 $helo_host = 'localhost';
86 $SMTP_CONN = new Net_SMTP($smtp_host, $smtp_port, $helo_host);
88 // try to connect to server and exit on failure
89 $result = $SMTP_CONN->connect($smtp_timeout);
90 if (PEAR::isError($result))
93 $response[] = "Connection failed: ".$result->getMessage();
97 // attempt to authenticate to the SMTP server
98 if ($CONFIG['smtp_user'] && $CONFIG['smtp_pass'])
100 if (strstr($CONFIG['smtp_user'], '%u'))
101 $smtp_user = str_replace('%u', $_SESSION['username'], $CONFIG['smtp_user']);
103 $smtp_user = $CONFIG['smtp_user'];
105 if (strstr($CONFIG['smtp_pass'], '%p'))
106 $smtp_pass = str_replace('%p', $RCMAIL->decrypt_passwd($_SESSION['password']), $CONFIG['smtp_pass']);
108 $smtp_pass = $CONFIG['smtp_pass'];
110 $smtp_auth_type = empty($CONFIG['smtp_auth_type']) ? NULL : $CONFIG['smtp_auth_type'];
111 $result = $SMTP_CONN->auth($smtp_user, $smtp_pass, $smtp_auth_type);
113 if (PEAR::isError($result))
116 $response[] .= 'Authentication failure: ' . $result->getMessage() . ' (Code: ' . $result->getCode() . ')';
123 // prepare message headers as string
124 if (is_array($headers))
126 $headerElements = smtp_prepare_headers($headers);
127 if (!$headerElements)
133 list($from, $text_headers) = $headerElements;
135 else if (is_string($headers))
136 $text_headers = $headers;
140 $response[] .= "Invalid message headers";
144 // exit if no from address is given
148 $response[] .= "No From address has been provided";
154 if (PEAR::isError($SMTP_CONN->mailFrom($from)))
157 $response[] .= "Failed to set sender '$from'";
162 // prepare list of recipients
163 $recipients = smtp_parse_rfc822($recipients);
164 if (PEAR::isError($recipients))
171 // set mail recipients
172 foreach ($recipients as $recipient)
174 if (PEAR::isError($SMTP_CONN->rcptTo($recipient)))
177 $response[] .= "Failed to add recipient '$recipient'";
183 // Concatenate headers and body so it can be passed by reference to SMTP_CONN->data
184 // so preg_replace in SMTP_CONN->quotedata will store a reference instead of a copy.
185 // We are still forced to make another copy here for a couple ticks so we don't really
186 // get to save a copy in the method call.
187 $data = $text_headers . "\r\n" . $body;
189 // unset old vars to save data and so we can pass into SMTP_CONN->data by reference.
190 unset($text_headers, $body);
192 // Send the message's headers and the body as SMTP data.
193 if (PEAR::isError($SMTP_CONN->data($data)))
196 $response[] .= "Failed to send data";
200 $response[] = join(': ', $SMTP_CONN->getResponse());
207 * Reset the global SMTP connection
210 function smtp_reset()
214 if (is_object($SMTP_CONN))
224 * Disconnect the global SMTP connection and destroy object
227 function smtp_disconnect()
231 if (is_object($SMTP_CONN))
233 $SMTP_CONN->disconnect();
240 * Take an array of mail headers and return a string containing
241 * text usable in sending a message.
243 * @param array $headers The array of headers to prepare, in an associative
244 * array, where the array key is the header name (ie,
245 * 'Subject'), and the array value is the header
246 * value (ie, 'test'). The header produced from those
247 * values would be 'Subject: test'.
249 * @return mixed Returns false if it encounters a bad address,
250 * otherwise returns an array containing two
251 * elements: Any From: address found in the headers,
252 * and the plain text version of the headers.
255 function smtp_prepare_headers($headers)
260 foreach ($headers as $key => $value)
262 if (strcasecmp($key, 'From') === 0)
264 $addresses = smtp_parse_rfc822($value);
266 if (is_array($addresses))
267 $from = $addresses[0];
269 // Reject envelope From: addresses with spaces.
270 if (strstr($from, ' '))
274 $lines[] = $key . ': ' . $value;
276 else if (strcasecmp($key, 'Received') === 0)
279 if (is_array($value))
281 foreach ($value as $line)
282 $received[] = $key . ': ' . $line;
286 $received[] = $key . ': ' . $value;
289 // Put Received: headers at the top. Spam detectors often
290 // flag messages with Received: headers after the Subject:
292 $lines = array_merge($received, $lines);
297 // If $value is an array (i.e., a list of addresses), convert
298 // it to a comma-delimited string of its elements (addresses).
299 if (is_array($value))
300 $value = implode(', ', $value);
302 $lines[] = $key . ': ' . $value;
306 return array($from, join(SMTP_MIME_CRLF, $lines) . SMTP_MIME_CRLF);
312 * Take a set of recipients and parse them, returning an array of
313 * bare addresses (forward paths) that can be passed to sendmail
314 * or an smtp server with the rcpt to: command.
316 * @param mixed Either a comma-seperated list of recipients
317 * (RFC822 compliant), or an array of recipients,
320 * @return array An array of forward paths (bare addresses).
323 function smtp_parse_rfc822($recipients)
325 // if we're passed an array, assume addresses are valid and implode them before parsing.
326 if (is_array($recipients))
327 $recipients = implode(', ', $recipients);
329 $addresses = array();
330 $recipients = rcube_explode_quoted_string(',', $recipients);
333 while (list($k, $recipient) = each($recipients))
335 $a = explode(" ", $recipient);
336 while (list($k2, $word) = each($a))
338 if ((strpos($word, "@") > 0) && (strpos($word, "\"")===false))
340 $word = ereg_replace('^<|>$', '', trim($word));
341 if (in_array($word, $addresses)===false)
342 array_push($addresses, $word);