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