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