commit | author | age
|
520c36
|
1 |
<?php |
968bdc
|
2 |
|
T |
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
|
5 |
| program/include/rcube_smtp.inc | |
|
6 |
| | |
|
7 |
| This file is part of the RoundCube Webmail client | |
|
8 |
| Copyright (C) 2005, RoundCube Dev. - Switzerland | |
|
9 |
| Licensed under the GNU GPL | |
|
10 |
| | |
|
11 |
| PURPOSE: | |
|
12 |
| Provide SMTP functionality using socket connections | |
|
13 |
| | |
|
14 |
+-----------------------------------------------------------------------+ |
|
15 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
|
16 |
+-----------------------------------------------------------------------+ |
|
17 |
|
|
18 |
$Id$ |
|
19 |
|
|
20 |
*/ |
|
21 |
|
|
22 |
|
|
23 |
// include required PEAR classes |
|
24 |
require_once('Net/SMTP.php'); |
|
25 |
|
|
26 |
|
|
27 |
// define headers delimiter |
|
28 |
define('SMTP_MIME_CRLF', "\r\n"); |
|
29 |
|
|
30 |
$SMTP_CONN = null; |
|
31 |
|
|
32 |
/** |
|
33 |
* Function for sending mail using SMTP. |
|
34 |
* |
|
35 |
* @param string Sender e-Mail address |
|
36 |
* |
|
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 |
|
41 |
* messages, etc. |
|
42 |
* |
|
43 |
* @param mixed The message headers to send with the mail |
|
44 |
* Either as an associative array or a finally |
|
45 |
* formatted string |
|
46 |
* |
|
47 |
* @param string The full text of the message body, including any Mime parts, etc. |
|
48 |
* |
|
49 |
* @return bool Returns TRUE on success, or FALSE on error |
|
50 |
* @access public |
|
51 |
*/ |
e0ed97
|
52 |
function smtp_mail($from, $recipients, $headers, &$body) |
968bdc
|
53 |
{ |
T |
54 |
global $SMTP_CONN, $CONFIG, $SMTP_ERROR; |
|
55 |
$smtp_timeout = null; |
fd8c50
|
56 |
$smtp_host = $CONFIG['smtp_server']; |
968bdc
|
57 |
$smtp_port = is_numeric($CONFIG['smtp_port']) ? $CONFIG['smtp_port'] : 25; |
fd8c50
|
58 |
$smtp_host_url = parse_url($CONFIG['smtp_server']); |
968bdc
|
59 |
|
fd8c50
|
60 |
// overwrite port |
T |
61 |
if ($smtp_host_url['host'] && $smtp_host_url['port']) |
|
62 |
{ |
|
63 |
$smtp_host = $smtp_host_url['host']; |
|
64 |
$smtp_port = $smtp_host_url['port']; |
|
65 |
} |
|
66 |
|
|
67 |
// re-write smtp host |
|
68 |
if ($smtp_host_url['host'] && $smtp_host_url['scheme']) |
|
69 |
$smtp_host = sprintf('%s://%s', $smtp_host_url['scheme'], $smtp_host_url['host']); |
|
70 |
|
|
71 |
|
968bdc
|
72 |
// create Net_SMTP object and connect to server |
T |
73 |
if (!is_object($smtp_conn)) |
|
74 |
{ |
977a29
|
75 |
$helo_host = !empty($_SERVER['server_name']) ? $_SERVER['server_name'] : 'localhost'; |
T |
76 |
$SMTP_CONN = new Net_SMTP($smtp_host, $smtp_port, $helo_host); |
968bdc
|
77 |
|
T |
78 |
// set debugging |
|
79 |
if ($CONFIG['debug_level'] & 8) |
|
80 |
$SMTP_CONN->setDebug(TRUE); |
|
81 |
|
|
82 |
|
|
83 |
// try to connect to server and exit on failure |
|
84 |
if (PEAR::isError($SMTP_CONN->connect($smtp_timeout))) |
|
85 |
{ |
|
86 |
$SMTP_CONN = null; |
|
87 |
$SMTP_ERROR .= "Connection failed\n"; |
|
88 |
return FALSE; |
|
89 |
} |
d206c1
|
90 |
|
968bdc
|
91 |
// attempt to authenticate to the SMTP server |
T |
92 |
if ($CONFIG['smtp_user'] && $CONFIG['smtp_pass']) |
|
93 |
{ |
4b0d50
|
94 |
if (strstr($CONFIG['smtp_user'], '%u')) |
T |
95 |
$smtp_user = str_replace('%u', $_SESSION['username'], $CONFIG['smtp_user']); |
d206c1
|
96 |
else |
S |
97 |
$smtp_user = $CONFIG['smtp_user']; |
4b0d50
|
98 |
|
T |
99 |
if (strstr($CONFIG['smtp_pass'], '%p')) |
|
100 |
$smtp_pass = str_replace('%p', decrypt_passwd($_SESSION['password']), $CONFIG['smtp_pass']); |
d206c1
|
101 |
else |
S |
102 |
$smtp_pass = $CONFIG['smtp_pass']; |
|
103 |
|
4301b0
|
104 |
$smtp_auth_type = empty($CONFIG['smtp_auth_type']) ? NULL : $CONFIG['smtp_auth_type']; |
d206c1
|
105 |
|
S |
106 |
if (PEAR::isError($SMTP_CONN->auth($smtp_user, $smtp_pass, $smtp_auth_type))) |
968bdc
|
107 |
{ |
T |
108 |
smtp_reset(); |
|
109 |
$SMTP_ERROR .= "authentication failure\n"; |
|
110 |
return FALSE; |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
|
|
115 |
|
|
116 |
// prepare message headers as string |
|
117 |
if (is_array($headers)) |
|
118 |
{ |
|
119 |
$headerElements = smtp_prepare_headers($headers); |
|
120 |
if (!$headerElements) |
|
121 |
{ |
|
122 |
smtp_reset(); |
|
123 |
return FALSE; |
|
124 |
} |
|
125 |
|
|
126 |
list($from, $text_headers) = $headerElements; |
|
127 |
} |
|
128 |
else if (is_string($headers)) |
|
129 |
$text_headers = $headers; |
|
130 |
else |
|
131 |
{ |
|
132 |
smtp_reset(); |
|
133 |
$SMTP_ERROR .= "Invalid message headers\n"; |
|
134 |
return FALSE; |
|
135 |
} |
|
136 |
|
|
137 |
// exit if no from address is given |
|
138 |
if (!isset($from)) |
|
139 |
{ |
|
140 |
smtp_reset(); |
|
141 |
$SMTP_ERROR .= "No From address has been provided\n"; |
|
142 |
return FALSE; |
|
143 |
} |
|
144 |
|
|
145 |
|
|
146 |
// set From: address |
|
147 |
if (PEAR::isError($SMTP_CONN->mailFrom($from))) |
|
148 |
{ |
|
149 |
smtp_reset(); |
|
150 |
$SMTP_ERROR .= "Failed to set sender '$from'\n"; |
|
151 |
return FALSE; |
|
152 |
} |
|
153 |
|
|
154 |
|
|
155 |
// prepare list of recipients |
|
156 |
$recipients = smtp_parse_rfc822($recipients); |
|
157 |
if (PEAR::isError($recipients)) |
|
158 |
{ |
|
159 |
smtp_reset(); |
|
160 |
return FALSE; |
|
161 |
} |
|
162 |
|
|
163 |
|
|
164 |
// set mail recipients |
|
165 |
foreach ($recipients as $recipient) |
|
166 |
{ |
|
167 |
if (PEAR::isError($SMTP_CONN->rcptTo($recipient))) |
|
168 |
{ |
|
169 |
smtp_reset(); |
|
170 |
$SMTP_ERROR .= "Failed to add recipient '$recipient'\n"; |
|
171 |
return FALSE; |
|
172 |
} |
|
173 |
} |
|
174 |
|
|
175 |
|
|
176 |
// Send the message's headers and the body as SMTP data. |
|
177 |
if (PEAR::isError($SMTP_CONN->data("$text_headers\r\n$body"))) |
|
178 |
{ |
|
179 |
smtp_reset(); |
|
180 |
$SMTP_ERROR .= "Failed to send data\n"; |
|
181 |
return FALSE; |
|
182 |
} |
|
183 |
|
|
184 |
|
|
185 |
return TRUE; |
|
186 |
} |
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
/** |
|
191 |
* Reset the global SMTP connection |
|
192 |
* @access public |
|
193 |
*/ |
|
194 |
function smtp_reset() |
|
195 |
{ |
|
196 |
global $SMTP_CONN; |
|
197 |
|
|
198 |
if (is_object($SMTP_CONN)) |
|
199 |
{ |
|
200 |
$SMTP_CONN->rset(); |
|
201 |
smtp_disconnect(); |
|
202 |
} |
|
203 |
} |
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
/** |
|
208 |
* Disconnect the global SMTP connection and destroy object |
|
209 |
* @access public |
|
210 |
*/ |
|
211 |
function smtp_disconnect() |
|
212 |
{ |
|
213 |
global $SMTP_CONN; |
|
214 |
|
|
215 |
if (is_object($SMTP_CONN)) |
|
216 |
{ |
|
217 |
$SMTP_CONN->disconnect(); |
|
218 |
$SMTP_CONN = null; |
|
219 |
} |
|
220 |
} |
|
221 |
|
|
222 |
|
|
223 |
/** |
|
224 |
* Take an array of mail headers and return a string containing |
|
225 |
* text usable in sending a message. |
|
226 |
* |
|
227 |
* @param array $headers The array of headers to prepare, in an associative |
|
228 |
* array, where the array key is the header name (ie, |
|
229 |
* 'Subject'), and the array value is the header |
|
230 |
* value (ie, 'test'). The header produced from those |
|
231 |
* values would be 'Subject: test'. |
|
232 |
* |
|
233 |
* @return mixed Returns false if it encounters a bad address, |
|
234 |
* otherwise returns an array containing two |
|
235 |
* elements: Any From: address found in the headers, |
|
236 |
* and the plain text version of the headers. |
|
237 |
* @access private |
|
238 |
*/ |
|
239 |
function smtp_prepare_headers($headers) |
|
240 |
{ |
|
241 |
$lines = array(); |
|
242 |
$from = null; |
|
243 |
|
|
244 |
foreach ($headers as $key => $value) |
|
245 |
{ |
|
246 |
if (strcasecmp($key, 'From') === 0) |
|
247 |
{ |
|
248 |
$addresses = smtp_parse_rfc822($value); |
|
249 |
|
|
250 |
if (is_array($addresses)) |
|
251 |
$from = $addresses[0]; |
|
252 |
|
|
253 |
// Reject envelope From: addresses with spaces. |
|
254 |
if (strstr($from, ' ')) |
|
255 |
return FALSE; |
|
256 |
|
|
257 |
|
|
258 |
$lines[] = $key . ': ' . $value; |
|
259 |
} |
|
260 |
else if (strcasecmp($key, 'Received') === 0) |
|
261 |
{ |
|
262 |
$received = array(); |
|
263 |
if (is_array($value)) |
|
264 |
{ |
|
265 |
foreach ($value as $line) |
|
266 |
$received[] = $key . ': ' . $line; |
|
267 |
} |
|
268 |
else |
|
269 |
{ |
|
270 |
$received[] = $key . ': ' . $value; |
|
271 |
} |
|
272 |
|
|
273 |
// Put Received: headers at the top. Spam detectors often |
|
274 |
// flag messages with Received: headers after the Subject: |
|
275 |
// as spam. |
|
276 |
$lines = array_merge($received, $lines); |
|
277 |
} |
|
278 |
|
|
279 |
else |
|
280 |
{ |
|
281 |
// If $value is an array (i.e., a list of addresses), convert |
|
282 |
// it to a comma-delimited string of its elements (addresses). |
|
283 |
if (is_array($value)) |
|
284 |
$value = implode(', ', $value); |
|
285 |
|
|
286 |
$lines[] = $key . ': ' . $value; |
|
287 |
} |
|
288 |
} |
|
289 |
|
|
290 |
return array($from, join(SMTP_MIME_CRLF, $lines) . SMTP_MIME_CRLF); |
|
291 |
} |
|
292 |
|
|
293 |
|
|
294 |
|
|
295 |
/** |
|
296 |
* Take a set of recipients and parse them, returning an array of |
|
297 |
* bare addresses (forward paths) that can be passed to sendmail |
|
298 |
* or an smtp server with the rcpt to: command. |
|
299 |
* |
|
300 |
* @param mixed Either a comma-seperated list of recipients |
|
301 |
* (RFC822 compliant), or an array of recipients, |
|
302 |
* each RFC822 valid. |
|
303 |
* |
|
304 |
* @return array An array of forward paths (bare addresses). |
|
305 |
* @access private |
|
306 |
*/ |
|
307 |
function smtp_parse_rfc822($recipients) |
|
308 |
{ |
|
309 |
// if we're passed an array, assume addresses are valid and implode them before parsing. |
|
310 |
if (is_array($recipients)) |
|
311 |
$recipients = implode(', ', $recipients); |
|
312 |
|
|
313 |
$addresses = array(); |
|
314 |
$recipients = smtp_explode_quoted_str(",", $recipients); |
|
315 |
|
|
316 |
reset($recipients); |
|
317 |
while (list($k, $recipient) = each($recipients)) |
|
318 |
{ |
|
319 |
$a = explode(" ", $recipient); |
|
320 |
while (list($k2, $word) = each($a)) |
|
321 |
{ |
|
322 |
if ((strpos($word, "@") > 0) && (strpos($word, "\"")===false)) |
|
323 |
{ |
|
324 |
$word = ereg_replace('^<|>$', '', trim($word)); |
|
325 |
if (in_array($word, $addresses)===false) |
|
326 |
array_push($addresses, $word); |
|
327 |
} |
|
328 |
} |
|
329 |
} |
|
330 |
return $addresses; |
|
331 |
} |
|
332 |
|
|
333 |
|
|
334 |
function smtp_explode_quoted_str($delimiter, $string) |
|
335 |
{ |
|
336 |
$quotes=explode("\"", $string); |
|
337 |
while ( list($key, $val) = each($quotes)) |
|
338 |
if (($key % 2) == 1) |
|
339 |
$quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]); |
|
340 |
$string=implode("\"", $quotes); |
|
341 |
|
|
342 |
$result=explode($delimiter, $string); |
|
343 |
while (list($key, $val) = each($result)) |
|
344 |
$result[$key] = str_replace("_!@!_", $delimiter, $result[$key]); |
|
345 |
|
|
346 |
return $result; |
|
347 |
} |
|
348 |
|
|
349 |
|
|
350 |
?> |