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