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