4 +-----------------------------------------------------------------------+
5 | program/include/rcube_smtp.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005, 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 266 2006-06-26 18:38:03Z thomasb $
23 // include required PEAR classes
24 require_once('Net/SMTP.php');
27 // define headers delimiter
28 define('SMTP_MIME_CRLF', "\r\n");
33 * Function for sending mail using SMTP.
35 * @param string Sender e-Mail address
37 * @param mixed Either a comma-seperated list of recipients
38 * (RFC822 compliant), or an array of recipients,
39 * each RFC822 valid. This may contain recipients not
40 * specified in the headers, for Bcc:, resending
43 * @param mixed The message headers to send with the mail
44 * Either as an associative array or a finally
47 * @param string The full text of the message body, including any Mime parts, etc.
49 * @return bool Returns TRUE on success, or FALSE on error
52 function smtp_mail($from, $recipients, &$headers, &$body)
54 global $SMTP_CONN, $CONFIG, $SMTP_ERROR;
56 $smtp_host = $CONFIG['smtp_server'];
57 $smtp_port = is_numeric($CONFIG['smtp_port']) ? $CONFIG['smtp_port'] : 25;
58 $smtp_host_url = parse_url($CONFIG['smtp_server']);
61 if ($smtp_host_url['host'] && $smtp_host_url['port'])
63 $smtp_host = $smtp_host_url['host'];
64 $smtp_port = $smtp_host_url['port'];
68 if ($smtp_host_url['host'] && $smtp_host_url['scheme'])
69 $smtp_host = sprintf('%s://%s', $smtp_host_url['scheme'], $smtp_host_url['host']);
72 // create Net_SMTP object and connect to server
73 if (!is_object($smtp_conn))
75 $helo_host = !empty($_SERVER['server_name']) ? $_SERVER['server_name'] : 'localhost';
76 $SMTP_CONN = new Net_SMTP($smtp_host, $smtp_port, $helo_host);
79 if ($CONFIG['debug_level'] & 8)
80 $SMTP_CONN->setDebug(TRUE);
83 // try to connect to server and exit on failure
84 $result = $SMTP_CONN->connect($smtp_timeout);
85 if (PEAR::isError($result))
88 $SMTP_ERROR .= "Connection failed: ".$result->getMessage()."\n";
92 // attempt to authenticate to the SMTP server
93 if ($CONFIG['smtp_user'] && $CONFIG['smtp_pass'])
95 if (strstr($CONFIG['smtp_user'], '%u'))
96 $smtp_user = str_replace('%u', $_SESSION['username'], $CONFIG['smtp_user']);
98 $smtp_user = $CONFIG['smtp_user'];
100 if (strstr($CONFIG['smtp_pass'], '%p'))
101 $smtp_pass = str_replace('%p', decrypt_passwd($_SESSION['password']), $CONFIG['smtp_pass']);
103 $smtp_pass = $CONFIG['smtp_pass'];
105 $smtp_auth_type = empty($CONFIG['smtp_auth_type']) ? NULL : $CONFIG['smtp_auth_type'];
106 $result = $SMTP_CONN->auth($smtp_user, $smtp_pass, $smtp_auth_type);
108 if (PEAR::isError($result))
111 $SMTP_ERROR .= "Authentication failure: ".$result->getMessage()."\n";
118 // prepare message headers as string
119 if (is_array($headers))
121 $headerElements = smtp_prepare_headers($headers);
122 if (!$headerElements)
128 list($from, $text_headers) = $headerElements;
130 else if (is_string($headers))
131 $text_headers = $headers;
135 $SMTP_ERROR .= "Invalid message headers\n";
139 // exit if no from address is given
143 $SMTP_ERROR .= "No From address has been provided\n";
149 if (PEAR::isError($SMTP_CONN->mailFrom($from)))
152 $SMTP_ERROR .= "Failed to set sender '$from'\n";
157 // prepare list of recipients
158 $recipients = smtp_parse_rfc822($recipients);
159 if (PEAR::isError($recipients))
166 // set mail recipients
167 foreach ($recipients as $recipient)
169 if (PEAR::isError($SMTP_CONN->rcptTo($recipient)))
172 $SMTP_ERROR .= "Failed to add recipient '$recipient'\n";
178 // Send the message's headers and the body as SMTP data.
179 if (PEAR::isError($SMTP_CONN->data("$text_headers\r\n$body")))
182 $SMTP_ERROR .= "Failed to send data\n";
193 * Reset the global SMTP connection
196 function smtp_reset()
200 if (is_object($SMTP_CONN))
210 * Disconnect the global SMTP connection and destroy object
213 function smtp_disconnect()
217 if (is_object($SMTP_CONN))
219 $SMTP_CONN->disconnect();
226 * Take an array of mail headers and return a string containing
227 * text usable in sending a message.
229 * @param array $headers The array of headers to prepare, in an associative
230 * array, where the array key is the header name (ie,
231 * 'Subject'), and the array value is the header
232 * value (ie, 'test'). The header produced from those
233 * values would be 'Subject: test'.
235 * @return mixed Returns false if it encounters a bad address,
236 * otherwise returns an array containing two
237 * elements: Any From: address found in the headers,
238 * and the plain text version of the headers.
241 function smtp_prepare_headers($headers)
246 foreach ($headers as $key => $value)
248 if (strcasecmp($key, 'From') === 0)
250 $addresses = smtp_parse_rfc822($value);
252 if (is_array($addresses))
253 $from = $addresses[0];
255 // Reject envelope From: addresses with spaces.
256 if (strstr($from, ' '))
260 $lines[] = $key . ': ' . $value;
262 else if (strcasecmp($key, 'Received') === 0)
265 if (is_array($value))
267 foreach ($value as $line)
268 $received[] = $key . ': ' . $line;
272 $received[] = $key . ': ' . $value;
275 // Put Received: headers at the top. Spam detectors often
276 // flag messages with Received: headers after the Subject:
278 $lines = array_merge($received, $lines);
283 // If $value is an array (i.e., a list of addresses), convert
284 // it to a comma-delimited string of its elements (addresses).
285 if (is_array($value))
286 $value = implode(', ', $value);
288 $lines[] = $key . ': ' . $value;
292 return array($from, join(SMTP_MIME_CRLF, $lines) . SMTP_MIME_CRLF);
298 * Take a set of recipients and parse them, returning an array of
299 * bare addresses (forward paths) that can be passed to sendmail
300 * or an smtp server with the rcpt to: command.
302 * @param mixed Either a comma-seperated list of recipients
303 * (RFC822 compliant), or an array of recipients,
306 * @return array An array of forward paths (bare addresses).
309 function smtp_parse_rfc822($recipients)
311 // if we're passed an array, assume addresses are valid and implode them before parsing.
312 if (is_array($recipients))
313 $recipients = implode(', ', $recipients);
315 $addresses = array();
316 $recipients = smtp_explode_quoted_str(",", $recipients);
319 while (list($k, $recipient) = each($recipients))
321 $a = explode(" ", $recipient);
322 while (list($k2, $word) = each($a))
324 if ((strpos($word, "@") > 0) && (strpos($word, "\"")===false))
326 $word = ereg_replace('^<|>$', '', trim($word));
327 if (in_array($word, $addresses)===false)
328 array_push($addresses, $word);
336 function smtp_explode_quoted_str($delimiter, $string)
338 $quotes=explode("\"", $string);
339 while ( list($key, $val) = each($quotes))
341 $quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]);
342 $string=implode("\"", $quotes);
344 $result=explode($delimiter, $string);
345 while (list($key, $val) = each($result))
346 $result[$key] = str_replace("_!@!_", $delimiter, $result[$key]);