59177427 |
1 | <?php |
2ba13803 |
2 | |
7350889b |
3 | /** |
4 | * smtp.php |
5 | * |
6 | * Copyright (c) 1999-2001 The Squirrelmail Development Team |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This contains all the functions needed to send messages through |
10 | * an smtp server or sendmail. |
11 | * |
12 | * $Id$ |
13 | */ |
b8ea4ed6 |
14 | |
0fc2aca0 |
15 | require_once('../functions/addressbook.php'); |
16 | require_once('../functions/plugin.php'); |
f435778e |
17 | |
18 | global $username, $popuser, $domain; |
465db5d7 |
19 | |
31afdefb |
20 | // This should most probably go to some initialization... |
21 | if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) { |
22 | $popuser = $usernamedata[1]; |
23 | $domain = $usernamedata[2]; |
7350889b |
24 | unset($usernamedata); |
31afdefb |
25 | } else { |
26 | $popuser = $username; |
27 | } |
28 | // We need domain for smtp |
29 | if (!$domain) |
ec9f1c41 |
30 | $domain = getenv('HOSTNAME'); |
31afdefb |
31 | |
df15de21 |
32 | // Returns true only if this message is multipart |
33 | function isMultipart () { |
4ba45d11 |
34 | global $attachments; |
35 | |
36 | if (count($attachments)>0) |
37 | return true; |
38 | else |
39 | return false; |
df15de21 |
40 | } |
41 | |
a4102fd7 |
42 | // looks up aliases in the addressbook and expands them to |
43 | // the full address. |
cb13511f |
44 | // Adds @$domain if it wasn't in the address book and if it |
45 | // doesn't have an @ symbol in it |
a4102fd7 |
46 | function expandAddrs ($array) { |
cb13511f |
47 | global $domain; |
48 | |
44bace96 |
49 | // don't show errors -- kinda critical that we don't see |
50 | // them here since the redirect won't work if we do show them |
51 | $abook = addressbook_init(false); |
a4102fd7 |
52 | for ($i=0; $i < count($array); $i++) { |
53 | $result = $abook->lookup($array[$i]); |
54 | $ret = ""; |
55 | if (isset($result['email'])) { |
56 | if (isset($result['name'])) { |
57 | $ret = '"'.$result['name'].'" '; |
58 | } |
59 | $ret .= '<'.$result['email'].'>'; |
60 | $array[$i] = $ret; |
61 | } |
18e206dd |
62 | else |
63 | { |
912c949d |
64 | if (strpos($array[$i], '@') === false) |
65 | $array[$i] .= '@' . $domain; |
18e206dd |
66 | $array[$i] = '<' . $array[$i] . '>'; |
67 | } |
a4102fd7 |
68 | } |
69 | return $array; |
70 | } |
71 | |
93555af8 |
72 | |
73 | // looks up aliases in the addressbook and expands them to |
74 | // the RFC 821 valid RCPT address. ie <user@example.com> |
75 | // Adds @$domain if it wasn't in the address book and if it |
76 | // doesn't have an @ symbol in it |
77 | function expandRcptAddrs ($array) { |
78 | global $domain; |
79 | |
80 | // don't show errors -- kinda critical that we don't see |
81 | // them here since the redirect won't work if we do show them |
82 | $abook = addressbook_init(false); |
83 | for ($i=0; $i < count($array); $i++) { |
84 | $result = $abook->lookup($array[$i]); |
85 | $ret = ""; |
86 | if (isset($result['email'])) { |
87 | $ret = '<'.$result['email'].'>'; |
88 | $array[$i] = $ret; |
89 | } |
90 | else |
91 | { |
92 | if (strpos($array[$i], '@') === false) |
93 | $array[$i] .= '@' . $domain; |
94 | $array[$i] = '<' . $array[$i] . '>'; |
95 | } |
96 | } |
97 | return $array; |
98 | } |
99 | |
100 | |
df15de21 |
101 | // Attach the files that are due to be attached |
4ba45d11 |
102 | function attachFiles ($fp) { |
c3c37167 |
103 | global $attachments, $attachment_dir; |
4ba45d11 |
104 | |
7b67334e |
105 | $length = 0; |
106 | |
a7d75834 |
107 | if (isMultipart()) { |
f972eb46 |
108 | foreach ($attachments as $info) |
109 | { |
4f601b14 |
110 | if (isset($info['type'])) |
7d50f13c |
111 | $filetype = $info['type']; |
112 | else |
ec9f1c41 |
113 | $filetype = 'application/octet-stream'; |
a7d75834 |
114 | |
ec9f1c41 |
115 | $header = '--'.mimeBoundary()."\r\n"; |
f972eb46 |
116 | $header .= "Content-Type: $filetype; name=\"" . |
117 | $info['remotefilename'] . "\"\r\n"; |
118 | $header .= "Content-Disposition: attachment; filename=\"" . |
119 | $info['remotefilename'] . "\"\r\n"; |
a7d75834 |
120 | |
f61de966 |
121 | // Use 'rb' for NT systems -- read binary |
122 | // Unix doesn't care -- everything's binary! :-) |
123 | $file = fopen ($attachment_dir . $info['localfilename'], 'rb'); |
aaf9abef |
124 | if (substr($filetype, 0, 5) == 'text/' || |
125 | $filetype == 'message/rfc822') { |
126 | $header .= "\r\n"; |
127 | fputs ($fp, $header); |
128 | $length += strlen($header); |
129 | while ($tmp = fgets($file, 4096)) { |
130 | $tmp = str_replace("\r\n", "\n", $tmp); |
131 | $tmp = str_replace("\r", "\n", $tmp); |
132 | $tmp = str_replace("\n", "\r\n", $tmp); |
e9eca7fe |
133 | if (feof($fp) && substr($tmp, -2) != "\r\n") |
134 | $tmp .= "\r\n"; |
aaf9abef |
135 | fputs($fp, $tmp); |
136 | $length += strlen($tmp); |
137 | } |
138 | } else { |
139 | $header .= "Content-Transfer-Encoding: base64\r\n\r\n"; |
140 | fputs ($fp, $header); |
141 | $length += strlen($header); |
142 | while ($tmp = fread($file, 570)) { |
143 | $encoded = chunk_split(base64_encode($tmp)); |
144 | $length += strlen($encoded); |
145 | fputs ($fp, $encoded); |
146 | } |
147 | } |
a7d75834 |
148 | fclose ($file); |
7b67334e |
149 | } |
4ba45d11 |
150 | } |
7b67334e |
151 | |
152 | return $length; |
df15de21 |
153 | } |
154 | |
a7d75834 |
155 | // Delete files that are uploaded for attaching |
156 | function deleteAttachments() { |
157 | global $attachments, $attachment_dir; |
158 | |
159 | if (isMultipart()) { |
160 | reset($attachments); |
161 | while (list($localname, $remotename) = each($attachments)) { |
162 | if (!ereg ("\\/", $localname)) { |
163 | unlink ($attachment_dir.$localname); |
ec9f1c41 |
164 | unlink ($attachment_dir.$localname.'.info'); |
a7d75834 |
165 | } |
166 | } |
167 | } |
168 | } |
169 | |
df15de21 |
170 | // Return a nice MIME-boundary |
171 | function mimeBoundary () { |
7b67334e |
172 | static $mimeBoundaryString; |
df15de21 |
173 | |
174 | if ($mimeBoundaryString == "") { |
ed865adf |
175 | $mimeBoundaryString = "----=_" . |
fe53c3b9 |
176 | GenerateRandomString(60, '\'()+,-./:=?_', 7); |
465db5d7 |
177 | } |
df15de21 |
178 | |
179 | return $mimeBoundaryString; |
465db5d7 |
180 | } |
181 | |
24397423 |
182 | /* Time offset for correct timezone */ |
183 | function timezone () { |
b0d1b51e |
184 | global $invert_time; |
185 | |
ec9f1c41 |
186 | $diff_second = date('Z'); |
d47b2518 |
187 | if ($invert_time) |
188 | $diff_second = - $diff_second; |
24397423 |
189 | if ($diff_second > 0) |
ec9f1c41 |
190 | $sign = '+'; |
24397423 |
191 | else |
ec9f1c41 |
192 | $sign = '-'; |
24397423 |
193 | |
194 | $diff_second = abs($diff_second); |
195 | |
196 | $diff_hour = floor ($diff_second / 3600); |
197 | $diff_minute = floor (($diff_second-3600*$diff_hour) / 60); |
198 | |
ec9f1c41 |
199 | $zonename = '('.strftime('%Z').')'; |
24397423 |
200 | $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename); |
201 | return ($result); |
202 | } |
203 | |
df15de21 |
204 | /* Print all the needed RFC822 headers */ |
60994e13 |
205 | function write822Header ($fp, $t, $c, $b, $subject, $more_headers) { |
a7d75834 |
206 | global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT; |
31afdefb |
207 | global $data_dir, $username, $popuser, $domain, $version, $useSendmail; |
9949206d |
208 | global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR; |
aaf9abef |
209 | global $REMOTE_HOST, $identity; |
465db5d7 |
210 | |
7b67334e |
211 | // Storing the header to make sure the header is the same |
212 | // everytime the header is printed. |
213 | static $header, $headerlength; |
214 | |
ec9f1c41 |
215 | if ($header == '') { |
a4102fd7 |
216 | $to = expandAddrs(parseAddrs($t)); |
217 | $cc = expandAddrs(parseAddrs($c)); |
218 | $bcc = expandAddrs(parseAddrs($b)); |
aaf9abef |
219 | if (isset($identity) && $identity != 'default') |
220 | { |
221 | $reply_to = getPref($data_dir, $username, 'reply_to' . $identity); |
222 | $from = getPref($data_dir, $username, 'full_name' . $identity); |
223 | $from_addr = getPref($data_dir, $username, 'email_address' . $identity); |
224 | } |
225 | else |
226 | { |
227 | $reply_to = getPref($data_dir, $username, 'reply_to'); |
228 | $from = getPref($data_dir, $username, 'full_name'); |
229 | $from_addr = getPref($data_dir, $username, 'email_address'); |
230 | } |
31afdefb |
231 | |
ec9f1c41 |
232 | if ($from_addr == '') |
233 | $from_addr = $popuser.'@'.$domain; |
7b67334e |
234 | |
235 | $to_list = getLineOfAddrs($to); |
236 | $cc_list = getLineOfAddrs($cc); |
237 | $bcc_list = getLineOfAddrs($bcc); |
8ae331c2 |
238 | |
78e230b7 |
239 | /* Encoding 8-bit characters and making from line */ |
d51894be |
240 | $subject = encodeHeader($subject); |
ec9f1c41 |
241 | if ($from == '') |
7b67334e |
242 | $from = "<$from_addr>"; |
243 | else |
ec9f1c41 |
244 | $from = '"' . encodeHeader($from) . "\" <$from_addr>"; |
7b67334e |
245 | |
a7d75834 |
246 | /* This creates an RFC 822 date */ |
7b67334e |
247 | $date = date("D, j M Y H:i:s ", mktime()) . timezone(); |
a7d75834 |
248 | |
249 | /* Create a message-id */ |
ec9f1c41 |
250 | $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.'; |
251 | $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>'; |
7b67334e |
252 | |
253 | /* Make an RFC822 Received: line */ |
a5363806 |
254 | if (isset($REMOTE_HOST)) |
8a2848f0 |
255 | $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])"; |
a5363806 |
256 | else |
8a2848f0 |
257 | $received_from = $REMOTE_ADDR; |
a5363806 |
258 | |
9949206d |
259 | if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) { |
ec9f1c41 |
260 | if ($HTTP_X_FORWARDED_FOR == '') |
261 | $HTTP_X_FORWARDED_FOR = 'unknown'; |
9949206d |
262 | $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)"; |
263 | } |
a5363806 |
264 | |
8a2848f0 |
265 | $header = "Received: from $received_from\r\n"; |
266 | $header .= " (SquirrelMail authenticated user $username)\r\n"; |
267 | $header .= " by $SERVER_NAME with HTTP;\r\n"; |
268 | $header .= " $date\r\n"; |
7b67334e |
269 | |
a7d75834 |
270 | /* Insert the rest of the header fields */ |
271 | $header .= "Message-ID: $message_id\r\n"; |
7b67334e |
272 | $header .= "Date: $date\r\n"; |
273 | $header .= "Subject: $subject\r\n"; |
274 | $header .= "From: $from\r\n"; |
18e206dd |
275 | $header .= "To: $to_list\r\n"; // Who it's TO |
60994e13 |
276 | |
277 | /* Insert headers from the $more_headers array */ |
278 | if(is_array($more_headers)) { |
279 | reset($more_headers); |
280 | while(list($h_name, $h_val) = each($more_headers)) { |
281 | $header .= sprintf("%s: %s\r\n", $h_name, $h_val); |
282 | } |
283 | } |
284 | |
7b67334e |
285 | if ($cc_list) { |
286 | $header .= "Cc: $cc_list\r\n"; // Who the CCs are |
df15de21 |
287 | } |
7b67334e |
288 | |
ec9f1c41 |
289 | if ($reply_to != '') |
7b67334e |
290 | $header .= "Reply-To: $reply_to\r\n"; |
291 | |
292 | if ($useSendmail) { |
293 | if ($bcc_list) { |
294 | // BCCs is removed from header by sendmail |
295 | $header .= "Bcc: $bcc_list\r\n"; |
296 | } |
297 | } |
298 | |
299 | $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail |
300 | |
301 | // Do the MIME-stuff |
f8308f16 |
302 | $header .= "MIME-Version: 1.0\r\n"; |
7b67334e |
303 | |
304 | if (isMultipart()) { |
ec9f1c41 |
305 | $header .= 'Content-Type: multipart/mixed; boundary="'; |
7b67334e |
306 | $header .= mimeBoundary(); |
307 | $header .= "\"\r\n"; |
308 | } else { |
ec9f1c41 |
309 | if ($default_charset != '') |
17ce8467 |
310 | $header .= "Content-Type: text/plain; charset=$default_charset\r\n"; |
311 | else |
312 | $header .= "Content-Type: text/plain;\r\n"; |
7b67334e |
313 | $header .= "Content-Transfer-Encoding: 8bit\r\n"; |
314 | } |
315 | $header .= "\r\n"; // One blank line to separate header and body |
df15de21 |
316 | |
7b67334e |
317 | $headerlength = strlen($header); |
318 | } |
319 | |
320 | // Write the header |
321 | fputs ($fp, $header); |
df15de21 |
322 | |
7b67334e |
323 | return $headerlength; |
324 | } |
df15de21 |
325 | |
7b67334e |
326 | // Send the body |
327 | function writeBody ($fp, $passedBody) { |
17ce8467 |
328 | global $default_charset; |
329 | |
7b67334e |
330 | $attachmentlength = 0; |
331 | |
df15de21 |
332 | if (isMultipart()) { |
ec9f1c41 |
333 | $body = '--'.mimeBoundary()."\r\n"; |
17ce8467 |
334 | |
335 | if ($default_charset != "") |
336 | $body .= "Content-Type: text/plain; charset=$default_charset\r\n"; |
337 | else |
338 | $body .= "Content-Type: text/plain\r\n"; |
339 | |
7b67334e |
340 | $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n"; |
d51894be |
341 | $body .= $passedBody . "\r\n\r\n"; |
7b67334e |
342 | fputs ($fp, $body); |
343 | |
6441f7c6 |
344 | $attachmentlength = attachFiles($fp); |
7b67334e |
345 | |
f8cfeb5c |
346 | if (!isset($postbody)) $postbody = ""; |
7b67334e |
347 | $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n"; |
348 | fputs ($fp, $postbody); |
df15de21 |
349 | } else { |
d51894be |
350 | $body = $passedBody . "\r\n"; |
7b67334e |
351 | fputs ($fp, $body); |
352 | $postbody = "\r\n"; |
353 | fputs ($fp, $postbody); |
df15de21 |
354 | } |
df15de21 |
355 | |
7b67334e |
356 | return (strlen($body) + strlen($postbody) + $attachmentlength); |
c99c5b31 |
357 | } |
465db5d7 |
358 | |
c99c5b31 |
359 | // Send mail using the sendmail command |
60994e13 |
360 | function sendSendmail($t, $c, $b, $subject, $body, $more_headers) { |
31afdefb |
361 | global $sendmail_path, $popuser, $username, $domain; |
7b67334e |
362 | |
6ebf8b30 |
363 | // Build envelope sender address. Make sure it doesn't contain |
364 | // spaces or other "weird" chars that would allow a user to |
365 | // exploit the shell/pipe it is used in. |
31afdefb |
366 | $envelopefrom = "$popuser@$domain"; |
ec9f1c41 |
367 | $envelopefrom = ereg_replace("[[:blank:]]",'', $envelopefrom); |
368 | $envelopefrom = ereg_replace("[[:space:]]",'', $envelopefrom); |
369 | $envelopefrom = ereg_replace("[[:cntrl:]]",'', $envelopefrom); |
6ebf8b30 |
370 | |
e957cd4a |
371 | // open pipe to sendmail or qmail-inject (qmail-inject doesn't accept -t param) |
372 | if (strstr($sendmail_path, "qmail-inject")) { |
373 | $fp = popen (escapeshellcmd("$sendmail_path -f$envelopefrom"), "w"); |
374 | } else { |
375 | $fp = popen (escapeshellcmd("$sendmail_path -t -f$envelopefrom"), "w"); |
376 | } |
377 | |
60994e13 |
378 | $headerlength = write822Header ($fp, $t, $c, $b, $subject, $more_headers); |
7b67334e |
379 | $bodylength = writeBody($fp, $body); |
465db5d7 |
380 | |
381 | pclose($fp); |
7b67334e |
382 | |
0775b17f |
383 | return ($headerlength + $bodylength); |
465db5d7 |
384 | } |
385 | |
b8ea4ed6 |
386 | function smtpReadData($smtpConnection) { |
387 | $read = fgets($smtpConnection, 1024); |
388 | $counter = 0; |
389 | while ($read) { |
ec9f1c41 |
390 | echo $read . '<BR>'; |
b8ea4ed6 |
391 | $data[$counter] = $read; |
392 | $read = fgets($smtpConnection, 1024); |
393 | $counter++; |
394 | } |
395 | } |
396 | |
60994e13 |
397 | function sendSMTP($t, $c, $b, $subject, $body, $more_headers) { |
8de3c34d |
398 | global $username, $popuser, $domain, $version, $smtpServerAddress, |
399 | $smtpPort, $data_dir, $color, $use_authenticated_smtp, $identity, |
400 | $key, $onetimepad; |
7831268e |
401 | |
93555af8 |
402 | $to = expandRcptAddrs(parseAddrs($t)); |
403 | $cc = expandRcptAddrs(parseAddrs($c)); |
404 | $bcc = expandRcptAddrs(parseAddrs($b)); |
aaf9abef |
405 | if (isset($identity) && $identity != 'default') |
406 | $from_addr = getPref($data_dir, $username, 'email_address' . $identity); |
407 | else |
408 | $from_addr = getPref($data_dir, $username, 'email_address'); |
356d7825 |
409 | |
31afdefb |
410 | if (!$from_addr) |
411 | $from_addr = "$popuser@$domain"; |
d3cdb279 |
412 | |
c175e2e4 |
413 | $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString); |
b8ea4ed6 |
414 | if (!$smtpConnection) { |
ec9f1c41 |
415 | echo 'Error connecting to SMTP Server.<br>'; |
b8ea4ed6 |
416 | echo "$errorNumber : $errorString<br>"; |
417 | exit; |
418 | } |
f8a9394d |
419 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
420 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
b8ea4ed6 |
421 | |
40ee9452 |
422 | $to_list = getLineOfAddrs($to); |
423 | $cc_list = getLineOfAddrs($cc); |
424 | |
425 | /** Lets introduce ourselves */ |
d402e33e |
426 | if (! isset ($use_authenticated_smtp) || $use_authenticated_smtp == false) { |
d2f4c914 |
427 | fputs($smtpConnection, "HELO $domain\r\n"); |
428 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
429 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
430 | } else { |
431 | fputs($smtpConnection, "EHLO $domain\r\n"); |
432 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
433 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
434 | |
435 | fputs($smtpConnection, "AUTH LOGIN\r\n"); |
436 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
437 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
438 | |
439 | fputs($smtpConnection, base64_encode ($username) . "\r\n"); |
440 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
441 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
442 | |
16cad51a |
443 | fputs($smtpConnection, base64_encode (OneTimePadDecrypt($key, $onetimepad)) . "\r\n"); |
d2f4c914 |
444 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
445 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
446 | } |
7831268e |
447 | |
40ee9452 |
448 | /** Ok, who is sending the message? */ |
f89627f3 |
449 | fputs($smtpConnection, "MAIL FROM: <$from_addr>\r\n"); |
f8a9394d |
450 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
451 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
b8ea4ed6 |
452 | |
40ee9452 |
453 | /** send who the recipients are */ |
454 | for ($i = 0; $i < count($to); $i++) { |
18e206dd |
455 | fputs($smtpConnection, "RCPT TO: $to[$i]\r\n"); |
f8a9394d |
456 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
457 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
40ee9452 |
458 | } |
459 | for ($i = 0; $i < count($cc); $i++) { |
b4338e67 |
460 | fputs($smtpConnection, "RCPT TO: $cc[$i]\r\n"); |
f8a9394d |
461 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
462 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
40ee9452 |
463 | } |
464 | for ($i = 0; $i < count($bcc); $i++) { |
b4338e67 |
465 | fputs($smtpConnection, "RCPT TO: $bcc[$i]\r\n"); |
f8a9394d |
466 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
467 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
40ee9452 |
468 | } |
b8ea4ed6 |
469 | |
40ee9452 |
470 | /** Lets start sending the actual message */ |
3379d740 |
471 | fputs($smtpConnection, "DATA\r\n"); |
f8a9394d |
472 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
473 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
7831268e |
474 | |
7b67334e |
475 | // Send the message |
60994e13 |
476 | $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject, $more_headers); |
7b67334e |
477 | $bodylength = writeBody($smtpConnection, $body); |
7831268e |
478 | |
3379d740 |
479 | fputs($smtpConnection, ".\r\n"); // end the DATA part |
f8a9394d |
480 | $tmp = fgets($smtpConnection, 1024); |
354eb441 |
481 | $num = errorCheck($tmp, $smtpConnection, true); |
3021b626 |
482 | if ($num != 250) { |
d5f34863 |
483 | $tmp = nl2br(htmlspecialchars($tmp)); |
484 | displayPageHeader($color, 'None'); |
0fc2aca0 |
485 | include_once('../functions/display_messages.php'); |
d5f34863 |
486 | $msg = "Message not sent!<br>\nReason given: $tmp"; |
487 | plain_error_message($msg, $color); |
488 | return(0); |
3021b626 |
489 | } |
d3cdb279 |
490 | |
3379d740 |
491 | fputs($smtpConnection, "QUIT\r\n"); // log off |
78509c54 |
492 | |
493 | fclose($smtpConnection); |
7b67334e |
494 | |
495 | return ($headerlength + $bodylength); |
b8ea4ed6 |
496 | } |
3021b626 |
497 | |
498 | |
354eb441 |
499 | function errorCheck($line, $smtpConnection, $verbose = false) { |
28010579 |
500 | global $color; |
776c7431 |
501 | |
f8a9394d |
502 | // Read new lines on a multiline response |
503 | $lines = $line; |
504 | while(ereg("^[0-9]+-", $line)) { |
505 | $line = fgets($smtpConnection, 1024); |
506 | $lines .= $line; |
507 | } |
508 | |
3021b626 |
509 | // Status: 0 = fatal |
510 | // 5 = ok |
511 | |
512 | $err_num = substr($line, 0, strpos($line, " ")); |
513 | switch ($err_num) { |
ec9f1c41 |
514 | case 500: $message = 'Syntax error; command not recognized'; |
3021b626 |
515 | $status = 0; |
516 | break; |
ec9f1c41 |
517 | case 501: $message = 'Syntax error in parameters or arguments'; |
3021b626 |
518 | $status = 0; |
519 | break; |
ec9f1c41 |
520 | case 502: $message = 'Command not implemented'; |
3021b626 |
521 | $status = 0; |
522 | break; |
ec9f1c41 |
523 | case 503: $message = 'Bad sequence of commands'; |
3021b626 |
524 | $status = 0; |
525 | break; |
ec9f1c41 |
526 | case 504: $message = 'Command parameter not implemented'; |
3021b626 |
527 | $status = 0; |
528 | break; |
529 | |
530 | |
ec9f1c41 |
531 | case 211: $message = 'System status, or system help reply'; |
3021b626 |
532 | $status = 5; |
533 | break; |
ec9f1c41 |
534 | case 214: $message = 'Help message'; |
3021b626 |
535 | $status = 5; |
536 | break; |
537 | |
538 | |
ec9f1c41 |
539 | case 220: $message = 'Service ready'; |
3021b626 |
540 | $status = 5; |
541 | break; |
ec9f1c41 |
542 | case 221: $message = 'Service closing transmission channel'; |
3021b626 |
543 | $status = 5; |
544 | break; |
ec9f1c41 |
545 | case 421: $message = 'Service not available, closing chanel'; |
3021b626 |
546 | $status = 0; |
547 | break; |
548 | |
d5f34863 |
549 | case 235: return(5); break; |
ec9f1c41 |
550 | case 250: $message = 'Requested mail action okay, completed'; |
3021b626 |
551 | $status = 5; |
552 | break; |
ec9f1c41 |
553 | case 251: $message = 'User not local; will forward'; |
3021b626 |
554 | $status = 5; |
555 | break; |
d5f34863 |
556 | case 334: return(5); break; |
ec9f1c41 |
557 | case 450: $message = 'Requested mail action not taken: mailbox unavailable'; |
3021b626 |
558 | $status = 0; |
559 | break; |
ec9f1c41 |
560 | case 550: $message = 'Requested action not taken: mailbox unavailable'; |
3021b626 |
561 | $status = 0; |
562 | break; |
ec9f1c41 |
563 | case 451: $message = 'Requested action aborted: error in processing'; |
3021b626 |
564 | $status = 0; |
565 | break; |
ec9f1c41 |
566 | case 551: $message = 'User not local; please try forwarding'; |
3021b626 |
567 | $status = 0; |
568 | break; |
ec9f1c41 |
569 | case 452: $message = 'Requested action not taken: insufficient system storage'; |
3021b626 |
570 | $status = 0; |
571 | break; |
ec9f1c41 |
572 | case 552: $message = 'Requested mail action aborted: exceeding storage allocation'; |
3021b626 |
573 | $status = 0; |
574 | break; |
ec9f1c41 |
575 | case 553: $message = 'Requested action not taken: mailbox name not allowed'; |
3021b626 |
576 | $status = 0; |
577 | break; |
ec9f1c41 |
578 | case 354: $message = 'Start mail input; end with .'; |
3021b626 |
579 | $status = 5; |
580 | break; |
ec9f1c41 |
581 | case 554: $message = 'Transaction failed'; |
3021b626 |
582 | $status = 0; |
583 | break; |
434666c7 |
584 | default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines)); |
3021b626 |
585 | $status = 0; |
ec9f1c41 |
586 | $error_num = '001'; |
3021b626 |
587 | break; |
588 | } |
589 | |
590 | if ($status == 0) { |
0fc2aca0 |
591 | include_once('../functions/page_header.php'); |
ec9f1c41 |
592 | displayPageHeader($color, 'None'); |
0fc2aca0 |
593 | include_once('../functions/display_messages.php'); |
f8a9394d |
594 | $lines = nl2br(htmlspecialchars($lines)); |
d5f34863 |
595 | $msg = $message . "<br>\nServer replied: $lines"; |
596 | plain_error_message($msg, $color); |
3021b626 |
597 | } |
354eb441 |
598 | if (! $verbose) return $status; |
3021b626 |
599 | return $err_num; |
600 | } |
df15de21 |
601 | |
020abcf3 |
602 | function sendMessage($t, $c, $b, $subject, $body, $reply_id, $prio = 3) { |
d2f4c914 |
603 | global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad; |
6441f7c6 |
604 | global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort; |
020abcf3 |
605 | global $default_use_priority; |
8fa48477 |
606 | global $more_headers; |
60994e13 |
607 | $more_headers = Array(); |
d1b8b679 |
608 | |
8fa48477 |
609 | do_hook("smtp_send"); |
610 | |
966286ae |
611 | $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1); |
60994e13 |
612 | |
214e1343 |
613 | if (isset($reply_id) && $reply_id) { |
966286ae |
614 | sqimap_mailbox_select ($imap_stream, $mailbox); |
ec9f1c41 |
615 | sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, 'Answered'); |
60994e13 |
616 | |
c95df380 |
617 | // Insert In-Reply-To and References headers if the |
618 | // message-id of the message we reply to is set (longer than "<>") |
619 | // The References header should really be the old Referenced header |
620 | // with the message ID appended, but it can be only the message ID too. |
621 | $hdr = sqimap_get_small_header ($imap_stream, $reply_id, false); |
622 | if(strlen($hdr->message_id) > 2) { |
ec9f1c41 |
623 | $more_headers['In-Reply-To'] = $hdr->message_id; |
624 | $more_headers['References'] = $hdr->message_id; |
c95df380 |
625 | } |
966286ae |
626 | } |
020abcf3 |
627 | if ($default_use_priority) { |
628 | $more_headers = array_merge($more_headers, createPriorityHeaders($prio)); |
629 | } |
8ae331c2 |
630 | |
af8eba43 |
631 | // In order to remove the problem of users not able to create |
eedcdd97 |
632 | // messages with "." on a blank line, RFC821 has made provision |
633 | // in section 4.5.2 (Transparency). |
634 | $body = ereg_replace("\n\\.", "\n..", $body); |
635 | $body = ereg_replace("^\\.", "..", $body); |
af8eba43 |
636 | |
8ae331c2 |
637 | // this is to catch all plain \n instances and |
eedcdd97 |
638 | // replace them with \r\n. All newlines were converted |
639 | // into just \n inside the compose.php file. |
8ae331c2 |
640 | $body = ereg_replace("\n", "\r\n", $body); |
3a8c414d |
641 | |
8ae331c2 |
642 | if ($useSendmail) { |
60994e13 |
643 | $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers); |
df15de21 |
644 | } else { |
60994e13 |
645 | $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers); |
df15de21 |
646 | } |
d1b8b679 |
647 | |
2966f172 |
648 | if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) { |
649 | sqimap_append ($imap_stream, $sent_folder, $length); |
60994e13 |
650 | write822Header ($imap_stream, $t, $c, $b, $subject, $more_headers); |
8ae331c2 |
651 | writeBody ($imap_stream, $body); |
2966f172 |
652 | sqimap_append_done ($imap_stream); |
8ae331c2 |
653 | } |
654 | sqimap_logout($imap_stream); |
a7d75834 |
655 | // Delete the files uploaded for attaching (if any). |
f95d9e7b |
656 | // only if $length != 0 (if there was no error) |
657 | if ($length) |
658 | ClearAttachments(); |
659 | |
660 | return $length; |
df15de21 |
661 | } |
020abcf3 |
662 | |
663 | function createPriorityHeaders($prio) { |
664 | $prio_headers = Array(); |
665 | $prio_headers["X-Priority"] = $prio; |
666 | |
667 | switch($prio) { |
668 | case 1: $prio_headers["Importance"] = "High"; |
669 | $prio_headers["X-MSMail-Priority"] = "High"; |
670 | break; |
671 | |
672 | case 3: $prio_headers["Importance"] = "Normal"; |
673 | $prio_headers["X-MSMail-Priority"] = "Normal"; |
674 | break; |
675 | |
676 | case 5: |
677 | $prio_headers["Importance"] = "Low"; |
678 | $prio_headers["X-MSMail-Priority"] = "Low"; |
679 | break; |
680 | } |
681 | return $prio_headers; |
682 | } |
683 | ?> |