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