6 * This code sends a mail.
8 * There are 4 modes of operation:
14 * @copyright © 1999-2005 The SquirrelMail Project Team
15 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
17 * @package squirrelmail
21 * Path for SquirrelMail required files.
24 define('SM_PATH','../');
26 /* SquirrelMail required files. */
27 require_once(SM_PATH
. 'include/validate.php');
28 require_once(SM_PATH
. 'functions/global.php');
29 require_once(SM_PATH
. 'functions/imap.php');
30 require_once(SM_PATH
. 'functions/date.php');
31 require_once(SM_PATH
. 'functions/mime.php');
32 require_once(SM_PATH
. 'functions/plugin.php');
33 require_once(SM_PATH
. 'functions/display_messages.php');
34 require_once(SM_PATH
. 'class/deliver/Deliver.class.php');
35 require_once(SM_PATH
. 'functions/addressbook.php');
36 require_once(SM_PATH
. 'functions/forms.php');
37 require_once(SM_PATH
. 'functions/identity.php');
39 /* --------------------- Get globals ------------------------------------- */
41 sqgetGlobalVar('key', $key, SQ_COOKIE
);
44 sqgetGlobalVar('username', $username, SQ_SESSION
);
45 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
46 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
47 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
49 sqgetGlobalVar('composesession', $composesession, SQ_SESSION
);
50 sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION
);
52 /** SESSION/POST/GET VARS */
53 sqgetGlobalVar('session',$session);
54 sqgetGlobalVar('mailbox',$mailbox);
55 if(!sqgetGlobalVar('identity',$identity)) {
58 sqgetGlobalVar('send_to',$send_to);
59 sqgetGlobalVar('send_to_cc',$send_to_cc);
60 sqgetGlobalVar('send_to_bcc',$send_to_bcc);
61 sqgetGlobalVar('subject',$subject);
62 sqgetGlobalVar('body',$body);
63 sqgetGlobalVar('mailprio',$mailprio);
64 sqgetGlobalVar('request_mdn',$request_mdn);
65 sqgetGlobalVar('request_dr',$request_dr);
66 sqgetGlobalVar('html_addr_search',$html_addr_search);
67 sqgetGlobalVar('mail_sent',$mail_sent);
68 sqgetGlobalVar('passed_id',$passed_id);
69 sqgetGlobalVar('passed_ent_id',$passed_ent_id);
70 sqgetGlobalVar('send',$send);
72 sqgetGlobalVar('attach',$attach);
74 sqgetGlobalVar('draft',$draft);
75 sqgetGlobalVar('draft_id',$draft_id);
76 sqgetGlobalVar('ent_num',$ent_num);
77 sqgetGlobalVar('saved_draft',$saved_draft);
78 sqgetGlobalVar('delete_draft',$delete_draft);
79 if ( sqgetGlobalVar('startMessage',$startMessage) ) {
80 $startMessage = (int)$startMessage;
86 sqgetGlobalVar('sigappend', $sigappend, SQ_POST
);
87 sqgetGlobalVar('from_htmladdr_search', $from_htmladdr_search, SQ_POST
);
88 sqgetGlobalVar('addr_search_done', $html_addr_search_done, SQ_POST
);
89 sqgetGlobalVar('send_to_search', $send_to_search, SQ_POST
);
90 sqgetGlobalVar('do_delete', $do_delete, SQ_POST
);
91 sqgetGlobalVar('delete', $delete, SQ_POST
);
92 sqgetGlobalVar('restoremessages', $restoremessages, SQ_POST
);
93 if ( sqgetGlobalVar('return', $temp, SQ_POST
) ) {
94 $html_addr_search_done = 'Use Addresses';
98 sqgetGlobalVar('attachedmessages', $attachedmessages, SQ_GET
);
101 if ( !sqgetGlobalVar('smaction',$action) )
103 if ( sqgetGlobalVar('smaction_reply',$tmp) ) $action = 'reply';
104 if ( sqgetGlobalVar('smaction_reply_all',$tmp) ) $action = 'reply_all';
105 if ( sqgetGlobalVar('smaction_forward',$tmp) ) $action = 'forward';
106 if ( sqgetGlobalVar('smaction_attache',$tmp) ) $action = 'forward_as_attachment';
107 if ( sqgetGlobalVar('smaction_draft',$tmp) ) $action = 'draft';
108 if ( sqgetGlobalVar('smaction_edit_new',$tmp) ) $action = 'edit_as_new';
111 /* Location (For HTTP 1.1 Header("Location: ...") redirects) */
112 $location = get_location();
113 /* Identities (fetch only once) */
114 $idents = get_identities();
116 /* --------------------- Specific Functions ------------------------------ */
118 function replyAllString($header) {
119 global $include_self_reply_all, $idents;
122 * 1) Remove the addresses we'll be sending the message 'to'
124 if (isset($header->replyto
)) {
125 $excl_ar = $header->getAddr_a('replyto');
128 * 2) Remove our identities from the CC list (they still can be in the
129 * TO list) only if $include_self_reply_all is turned off
131 if (!$include_self_reply_all) {
132 foreach($idents as $id) {
133 $excl_ar[strtolower(trim($id['email_address']))] = '';
138 * 3) get the addresses.
140 $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar);
143 * 4) generate the string.
145 $url_replytoallcc = '';
146 foreach( $url_replytoall_ar as $email => $personal) {
148 // if personal name contains address separator then surround
149 // the personal name with double quotes.
150 if (strpos($personal,',') !== false) {
151 $personal = '"'.$personal.'"';
153 $url_replytoallcc .= ", $personal <$email>";
155 $url_replytoallcc .= ', '. $email;
158 $url_replytoallcc = substr($url_replytoallcc,2);
160 return $url_replytoallcc;
164 * creates top line in reply citations
166 * Line style depends on user preferences.
167 * $orig_date argument is available only from 1.4.3 and 1.5.1 version.
168 * @param object $orig_from From: header object.
169 * @param integer $orig_date email's timestamp
170 * @return string reply citation
172 function getReplyCitation($orig_from, $orig_date) {
173 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
175 if (!is_object($orig_from)) {
178 $sOrig_from = decodeHeader($orig_from->getAddress(false),false,false,true);
181 // $from = decodeHeader($orig_header->getAddr_s('from',"\n$indent"),false,false);
183 /* First, return an empty string when no citation style selected. */
184 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
188 /* Make sure our final value isn't an empty string. */
189 if ($sOrig_from == '') {
193 /* Otherwise, try to select the desired citation style. */
194 switch ($reply_citation_style) {
197 * To translators: %s is for author's name
199 $full_reply_citation = sprintf(_("%s wrote:"),$sOrig_from);
202 // FIXME: do we have to translate xml formating?
203 $start = '<' . _("quote") . ' ' . _("who") . '="';
205 $full_reply_citation = $start . $sOrig_from . $end;
207 case 'date_time_author':
210 * first %s is for date string, second %s is for author's name. Date uses
211 * formating from "D, F j, Y g:i a" and "D, F j, Y H:i" translations.
213 * "On Sat, December 24, 2004 23:59, Santa wrote:"
214 * If you have to put author's name in front of date string, check comments about
215 * argument swapping at http://www.php.net/sprintf
217 $full_reply_citation = sprintf(_("On %s, %s wrote:"), getLongDateString($orig_date), $sOrig_from);
220 $start = $reply_citation_start .
221 ($reply_citation_start == '' ?
'' : ' ');
222 $end = $reply_citation_end;
223 $full_reply_citation = $start . $sOrig_from . $end;
229 /* Add line feed and return the citation string. */
230 return ($full_reply_citation . "\n");
234 * Creates header fields in forwarded email body
236 * $default_charset global must be set correctly before you call this function.
237 * @param object $orig_header
240 function getforwardHeader($orig_header) {
241 global $editor_size, $default_charset;
243 // using own strlen function in order to detect correct string length
244 $display = array( _("Subject") => sq_strlen(_("Subject"),$default_charset),
245 _("From") => sq_strlen(_("From"),$default_charset),
246 _("Date") => sq_strlen(_("Date"),$default_charset),
247 _("To") => sq_strlen(_("To"),$default_charset),
248 _("Cc") => sq_strlen(_("Cc"),$default_charset) );
249 $maxsize = max($display);
250 $indent = str_pad('',$maxsize+
2);
251 foreach($display as $key => $val) {
252 $display[$key] = $key .': '. str_pad('', $maxsize - $val);
254 $from = decodeHeader($orig_header->getAddr_s('from',"\n$indent"),false,false,true);
255 $from = str_replace(' ',' ',$from);
256 $to = decodeHeader($orig_header->getAddr_s('to',"\n$indent"),false,false,true);
257 $to = str_replace(' ',' ',$to);
258 $subject = decodeHeader($orig_header->subject
,false,false,true);
259 $subject = str_replace(' ',' ',$subject);
261 // using own str_pad function in order to create correct string pad
262 $bodyTop = sq_str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH
,$default_charset) .
263 "\n". $display[_("Subject")] . $subject . "\n" .
264 $display[_("From")] . $from . "\n" .
265 $display[_("Date")] . getLongDateString( $orig_header->date
). "\n" .
266 $display[_("To")] . $to . "\n";
267 if ($orig_header->cc
!= array() && $orig_header->cc
!='') {
268 $cc = decodeHeader($orig_header->getAddr_s('cc',"\n$indent"),false,false,true);
269 $cc = str_replace(' ',' ',$cc);
270 $bodyTop .= $display[_("Cc")] .$cc . "\n";
272 $bodyTop .= str_pad('', $editor_size -2 , '-') .
276 /* ----------------------------------------------------------------------- */
279 * If the session is expired during a post this restores the compose session
282 if (sqsession_is_registered('session_expired_post')) {
283 sqgetGlobalVar('session_expired_post', $session_expired_post, SQ_SESSION
);
285 * extra check for username so we don't display previous post data from
286 * another user during this session.
288 if ($session_expired_post['username'] != $username) {
289 unset($session_expired_post);
290 sqsession_unregister('session_expired_post');
291 session_write_close();
293 foreach ($session_expired_post as $postvar => $val) {
300 $compose_messages = unserialize(urldecode($restoremessages));
301 sqsession_register($compose_messages,'compose_messages');
302 sqsession_register($composesession,'composesession');
306 $session_expired = true;
308 unset($session_expired_post);
309 sqsession_unregister('session_expired_post');
310 session_write_close();
311 if (!isset($mailbox)) {
314 if ($compose_new_win == '1') {
315 compose_Header($color, $mailbox);
317 $sHeaderJs = (isset($sHeaderJs)) ?
$sHeaderJs : '';
318 if (strpos($action, 'reply') !== false && $reply_focus) {
319 $sBodyTagJs = 'onload="checkForm(\''.$replyfocus.'\');"';
321 $sBodyTagJs = 'onload="checkForm();"';
323 displayPageHeader($color, $mailbox,$sHeaderJs,$sBodyTagJs);
325 showInputForm($session, false);
328 if (!isset($composesession)) {
330 sqsession_register(0,'composesession');
333 if (!isset($session) ||
(isset($newmessage) && $newmessage)) {
334 sqsession_unregister('composesession');
335 $session = "$composesession" +
1;
336 $composesession = $session;
337 sqsession_register($composesession,'composesession');
339 if (!isset($compose_messages)) {
340 $compose_messages = array();
343 if (!isset($compose_messages[$session]) ||
($compose_messages[$session] == NULL)) {
344 /* if (!array_key_exists($session, $compose_messages)) { /* We can only do this in PHP >= 4.1 */
345 $composeMessage = new Message();
346 $rfc822_header = new Rfc822Header();
347 $composeMessage->rfc822_header
= $rfc822_header;
348 $composeMessage->reply_rfc822_header
= '';
349 $compose_messages[$session] = $composeMessage;
351 sqsession_register($compose_messages,'compose_messages');
353 $composeMessage=$compose_messages[$session];
356 if (!isset($mailbox) ||
$mailbox == '' ||
($mailbox == 'None')) {
362 * Set $default_charset to correspond with the user's selection
363 * of language interface.
366 $composeMessage=$compose_messages[$session];
367 if (! deliverMessage($composeMessage, true)) {
368 showInputForm($session);
371 unset($compose_messages[$session]);
372 $draft_message = _("Draft Email Saved");
373 /* If this is a resumed draft, then delete the original */
374 if(isset($delete_draft)) {
375 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, false);
376 sqimap_mailbox_select($imap_stream, $draft_folder);
377 // force bypass_trash=true because message should be saved when deliverMessage() returns true.
378 // in current implementation of sqimap_msgs_list_flag() single message id can
379 // be submitted as string. docs state that it should be array.
380 sqimap_msgs_list_delete($imap_stream, $draft_folder, $delete_draft, true);
382 sqimap_mailbox_expunge($imap_stream, $draft_folder, true);
384 sqimap_logout($imap_stream);
386 if ($compose_new_win == '1') {
387 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
388 Header("Location: $location/compose.php?saved_draft=yes&session=$composesession");
390 echo ' <br><br><center><a href="' . $location
391 . '/compose.php?saved_sent=yes&session=' . $composesession . '">'
392 . _("Return") . '</a></center>';
396 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
397 Header("Location: $location/right_main.php?mailbox=" . urlencode($draft_folder) .
398 "&startMessage=1¬e=".urlencode($draft_message));
400 echo ' <br><br><center><a href="' . $location
401 . '/right_main.php?mailbox=' . urlencode($draft_folder)
402 . '&startMessage=1&note=' . urlencode($draft_message) .'">'
403 . _("Return") . '</a></center>';
411 if (isset($_FILES['attachfile']) &&
412 $_FILES['attachfile']['tmp_name'] &&
413 $_FILES['attachfile']['tmp_name'] != 'none') {
414 $AttachFailure = saveAttachedFiles($session);
416 if (checkInput(false) && !isset($AttachFailure)) {
417 if ($mailbox == "All Folders") {
418 /* We entered compose via the search results page */
419 $mailbox="INBOX"; /* Send 'em to INBOX, that's safe enough */
421 $urlMailbox = urlencode (trim($mailbox));
422 if (! isset($passed_id)) {
426 * Set $default_charset to correspond with the user's selection
427 * of language interface.
431 * This is to change all newlines to \n
432 * We'll change them to \r\n later (in the sendMessage function)
434 $body = str_replace("\r\n", "\n", $body);
435 $body = str_replace("\r", "\n", $body);
438 * Rewrap $body so that no line is bigger than $editor_size
440 $body = explode("\n", $body);
442 foreach ($body as $line) {
443 if( $line <> '-- ' ) {
444 $line = rtrim($line);
446 if (sq_strlen($line,$default_charset) <= $editor_size +
1) {
447 $newBody .= $line . "\n";
449 sqWordWrap($line, $editor_size,$default_charset);
450 $newBody .= $line . "\n";
457 $composeMessage=$compose_messages[$session];
459 $Result = deliverMessage($composeMessage);
461 showInputForm($session);
464 unset($compose_messages[$session]);
465 /* if it is resumed draft, delete draft message */
466 if ( isset($delete_draft)) {
467 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, false);
468 sqimap_mailbox_select($imap_stream, $draft_folder);
469 // bypass_trash=true because message should be saved when deliverMessage() returns true.
470 // in current implementation of sqimap_msgs_list_flag() single message id can
471 // be submitted as string. docs state that it should be array.
472 sqimap_msgs_list_delete($imap_stream, $draft_folder, $delete_draft, true);
474 sqimap_mailbox_expunge($imap_stream, $draft_folder, true);
476 sqimap_logout($imap_stream);
478 if ($compose_new_win == '1') {
479 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
480 Header("Location: $location/compose.php?mail_sent=yes");
482 echo ' <br><br><center><a href="' . $location
483 . '/compose.php?mail_sent=yes">'
484 . _("Return") . '</a></center>';
488 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
489 Header("Location: $location/right_main.php?mailbox=$urlMailbox".
490 "&startMessage=$startMessage&mail_sent=yes");
492 echo ' <br><br><center><a href="' . $location
493 . "/right_main.php?mailbox=$urlMailbox"
494 . "&startMessage=$startMessage&mail_sent=yes\">"
495 . _("Return") . '</a></center>';
500 if ($compose_new_win == '1') {
501 compose_Header($color, $mailbox);
504 displayPageHeader($color, $mailbox);
506 if (isset($AttachFailure)) {
507 plain_error_message(_("Could not move/copy file. File not attached"),
511 showInputForm($session);
512 /* sqimap_logout($imapConnection); */
514 } elseif (isset($html_addr_search_done)) {
515 if ($compose_new_win == '1') {
516 compose_Header($color, $mailbox);
519 displayPageHeader($color, $mailbox);
522 if (isset($send_to_search) && is_array($send_to_search)) {
523 foreach ($send_to_search as $k => $v) {
524 if (substr($k, 0, 1) == 'T') {
530 elseif (substr($k, 0, 1) == 'C') {
536 elseif (substr($k, 0, 1) == 'B') {
538 $send_to_bcc .= ', ';
544 showInputForm($session);
545 } elseif (isset($html_addr_search)) {
546 if (isset($_FILES['attachfile']) &&
547 $_FILES['attachfile']['tmp_name'] &&
548 $_FILES['attachfile']['tmp_name'] != 'none') {
549 if(saveAttachedFiles($session)) {
550 plain_error_message(_("Could not move/copy file. File not attached"), $color);
554 * I am using an include so as to elminiate an extra unnecessary
555 * click. If you can think of a better way, please implement it.
557 include_once('./addrbook_search_html.php');
558 } elseif (isset($attach)) {
559 if (saveAttachedFiles($session)) {
560 plain_error_message(_("Could not move/copy file. File not attached"), $color);
562 if ($compose_new_win == '1') {
563 compose_Header($color, $mailbox);
565 displayPageHeader($color, $mailbox);
567 showInputForm($session);
569 elseif (isset($sigappend)) {
570 $signature = $idents[$identity]['signature'];
572 $body .= "\n\n".($prefix_sig==true?
"-- \n":'').$signature;
573 if ($compose_new_win == '1') {
574 compose_Header($color, $mailbox);
576 displayPageHeader($color, $mailbox);
578 showInputForm($session);
579 } elseif (isset($do_delete)) {
580 if ($compose_new_win == '1') {
581 compose_Header($color, $mailbox);
583 displayPageHeader($color, $mailbox);
586 if (isset($delete) && is_array($delete)) {
587 $composeMessage = $compose_messages[$session];
588 foreach($delete as $index) {
589 $attached_file = $composeMessage->entities
[$index]->att_local_name
;
590 unlink ($attached_file);
591 unset ($composeMessage->entities
[$index]);
593 $new_entities = array();
594 foreach ($composeMessage->entities
as $entity) {
595 $new_entities[] = $entity;
597 $composeMessage->entities
= $new_entities;
598 $compose_messages[$session] = $composeMessage;
599 sqsession_register($compose_messages, 'compose_messages');
601 showInputForm($session);
604 * This handles the default case as well as the error case
605 * (they had the same code) --> if (isset($smtpErrors))
608 if ($compose_new_win == '1') {
609 compose_Header($color, $mailbox);
611 displayPageHeader($color, $mailbox);
616 if (!isset($passed_ent_id)) {
619 if (!isset($passed_id)) {
622 if (!isset($mailbox)) {
625 if (!isset($action)) {
629 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
631 /* in case the origin is not read_body.php */
632 if (isset($send_to)) {
633 $values['send_to'] = $send_to;
635 if (isset($send_to_cc)) {
636 $values['send_to_cc'] = $send_to_cc;
638 if (isset($send_to_bcc)) {
639 $values['send_to_bcc'] = $send_to_bcc;
641 if (isset($subject)) {
642 $values['subject'] = $subject;
644 showInputForm($session, $values);
649 /**************** Only function definitions go below *************/
651 function getforwardSubject($subject)
653 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
654 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
655 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
656 $subject = '[Fwd: ' . $subject . ']';
661 /* This function is used when not sending or adding attachments */
662 function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
663 global $editor_size, $default_use_priority, $body, $idents,
664 $use_signature, $data_dir, $username,
665 $username, $key, $imapServerAddress, $imapPort, $compose_messages,
666 $composeMessage, $body_quote;
667 global $languages, $squirrelmail_language, $default_charset;
670 * Set $default_charset to correspond with the user's selection
671 * of language interface. $default_charset global is not correct,
672 * if message is composed in new window.
676 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
680 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
683 sqimap_mailbox_select($imapConnection, $mailbox);
684 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
687 if ($passed_ent_id) {
688 /* redefine the messsage in case of message/rfc822 */
689 $message = $message->getEntity($passed_ent_id);
690 /* message is an entity which contains the envelope and type0=message
691 * and type1=rfc822. The actual entities are childs from
692 * $message->entities[0]. That's where the encoding and is located
695 $entities = $message->entities
[0]->findDisplayEntity
696 (array(), $alt_order = array('text/plain'));
697 if (!count($entities)) {
698 $entities = $message->entities
[0]->findDisplayEntity
699 (array(), $alt_order = array('text/plain','html/plain'));
701 $orig_header = $message->rfc822_header
; /* here is the envelope located */
702 /* redefine the message for picking up the attachments */
703 $message = $message->entities
[0];
706 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
707 if (!count($entities)) {
708 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
710 $orig_header = $message->rfc822_header
;
713 $type0 = $message->type0
;
714 $type1 = $message->type1
;
715 foreach ($entities as $ent) {
716 $msg = $message->getEntity($ent);
717 $type0 = $msg->type0
;
718 $type1 = $msg->type1
;
719 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
720 $body_part_entity = $message->getEntity($ent);
721 $bodypart = decodeBody($unencoded_bodypart,
722 $body_part_entity->header
->encoding
);
723 if ($type1 == 'html') {
724 $bodypart = str_replace("\n", ' ', $bodypart);
725 $bodypart = preg_replace(array('/<\/?p>/i','/<div><\/div>/i','/<br\s*(\/)*>/i','/<\/?div>/i'), "\n", $bodypart);
726 $bodypart = str_replace(array(' ','>','<'),array(' ','>','<'),$bodypart);
727 $bodypart = strip_tags($bodypart);
729 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
730 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
731 if (mb_detect_encoding($bodypart) != 'ASCII') {
732 $bodypart = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $bodypart);
736 // charset encoding in compose form stuff
737 if (isset($body_part_entity->header
->parameters
['charset'])) {
738 $actual = $body_part_entity->header
->parameters
['charset'];
740 $actual = 'us-ascii';
743 if ( $actual && is_conversion_safe($actual) && $actual != $default_charset){
744 $bodypart = charset_convert($actual,$bodypart,$default_charset,false);
746 // end of charset encoding in compose
750 if ($default_use_priority) {
751 $mailprio = substr($orig_header->priority
,0,1);
758 //ClearAttachments($session);
761 $from_o = $orig_header->from
;
762 if (is_array($from_o)) {
763 if (isset($from_o[0])) {
764 $from_o = $from_o[0];
767 if (is_object($from_o)) {
768 $orig_from = $from_o->getAddress();
773 $identities = array();
774 if (count($idents) > 1) {
775 foreach($idents as $nr=>$data) {
776 $enc_from_name = '"'.$data['full_name'].'" <'. $data['email_address'].'>';
777 if($enc_from_name == $orig_from) {
781 $identities[] = $enc_from_name;
784 $identity_match = $orig_header->findAddress($identities);
785 if ($identity_match) {
786 $identity = $identity_match;
792 $use_signature = FALSE;
793 $composeMessage->rfc822_header
= $orig_header;
794 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
795 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
796 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
797 $send_from = $orig_header->getAddr_s('from');
798 $send_from_parts = new AddressStructure();
799 $send_from_parts = $orig_header->parseAddress($send_from);
800 $send_from_add = $send_from_parts->mailbox
. '@' . $send_from_parts->host
;
801 $identities = get_identities();
802 if (count($identities) > 0) {
803 foreach($identities as $iddata) {
804 if ($send_from_add == $iddata['email_address']) {
805 $identity = $iddata['index'];
810 $subject = decodeHeader($orig_header->subject
,false,false,true);
811 /* remember the references and in-reply-to headers in case of an reply */
812 $composeMessage->rfc822_header
->more_headers
['References'] = $orig_header->references
;
813 $composeMessage->rfc822_header
->more_headers
['In-Reply-To'] = $orig_header->in_reply_to
;
814 // rewrap the body to clean up quotations and line lengths
815 sqBodyWrap($body, $editor_size);
816 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
818 case ('edit_as_new'):
819 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
820 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
821 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
822 $subject = decodeHeader($orig_header->subject
,false,false,true);
823 $mailprio = $orig_header->priority
;
825 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
826 // rewrap the body to clean up quotations and line lengths
827 sqBodyWrap($body, $editor_size);
831 $subject = getforwardSubject(decodeHeader($orig_header->subject
,false,false,true));
832 $body = getforwardHeader($orig_header) . $body;
833 // the logic for calling sqUnWordWrap here would be to allow the browser to wrap the lines
834 // forwarded message text should be as undisturbed as possible, so commenting out this call
835 // sqUnWordWrap($body);
836 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
838 //add a blank line after the forward headers
839 $body = "\n" . $body;
841 case ('forward_as_attachment'):
842 $subject = getforwardSubject(decodeHeader($orig_header->subject
,false,false,true));
843 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
847 if(isset($orig_header->mail_followup_to
) && $orig_header->mail_followup_to
) {
848 $send_to = $orig_header->getAddr_s('mail_followup_to');
850 $send_to_cc = replyAllString($orig_header);
851 $send_to_cc = decodeHeader($send_to_cc,false,false,true);
854 // skip this if send_to was already set right above here
856 $send_to = $orig_header->reply_to
;
857 if (is_array($send_to) && count($send_to)) {
858 $send_to = $orig_header->getAddr_s('reply_to');
859 } else if (is_object($send_to)) { /* unneccesarry, just for failsafe purpose */
860 $send_to = $orig_header->getAddr_s('reply_to');
862 $send_to = $orig_header->getAddr_s('from');
865 $send_to = decodeHeader($send_to,false,false,true);
866 $subject = decodeHeader($orig_header->subject
,false,false,true);
867 $subject = str_replace('"', "'", $subject);
868 $subject = trim($subject);
869 if (substr(strtolower($subject), 0, 3) != 're:') {
870 $subject = 'Re: ' . $subject;
872 /* this corrects some wrapping/quoting problems on replies */
873 $rewrap_body = explode("\n", $body);
874 $from = (is_array($orig_header->from
)) ?
$orig_header->from
[0] : $orig_header->from
;
876 $strip_sigs = getPref($data_dir, $username, 'strip_sigs');
877 foreach ($rewrap_body as $line) {
878 if ($strip_sigs && substr($line,0,3) == '-- ') {
881 if (preg_match("/^(>+)/", $line, $matches)) {
883 $body .= $body_quote . str_replace("\n", "\n$body_quote$gt ", rtrim($line)) ."\n";
885 $body .= $body_quote . (!empty($body_quote) ?
' ' : '') . str_replace("\n", "\n$body_quote" . (!empty($body_quote) ?
' ' : ''), rtrim($line)) . "\n";
889 //rewrap the body to clean up quotations and line lengths
890 $body = sqBodyWrap ($body, $editor_size);
892 $body = getReplyCitation($from , $orig_header->date
) . $body;
893 $composeMessage->reply_rfc822_header
= $orig_header;
899 $compose_messages[$session] = $composeMessage;
900 sqsession_register($compose_messages, 'compose_messages');
901 session_write_close();
902 sqimap_logout($imapConnection);
904 $ret = array( 'send_to' => $send_to,
905 'send_to_cc' => $send_to_cc,
906 'send_to_bcc' => $send_to_bcc,
907 'subject' => $subject,
908 'mailprio' => $mailprio,
910 'identity' => $identity );
913 } /* function newMail() */
916 * downloads attachments from original message, stores them in attachment directory and adds
917 * them to composed message.
918 * @param object $message
919 * @param object $composeMessage
920 * @param integer $passed_id
921 * @param mixed $entities
922 * @param mixed $imapConnection
925 function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
926 global $attachment_dir, $username, $data_dir, $squirrelmail_language, $languages;
927 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
928 if (!count($message->entities
) ||
929 ($message->type0
== 'message' && $message->type1
== 'rfc822')) {
930 if ( !in_array($message->entity_id
, $entities) && $message->entity_id
) {
931 switch ($message->type0
) {
933 if ($message->type1
== 'rfc822') {
934 $filename = $message->rfc822_header
->subject
;
935 if ($filename == "") {
936 $filename = "untitled-".$message->entity_id
;
940 $filename = $message->getFilename();
944 if (!$message->mime_header
) { /* temporary hack */
945 $message->mime_header
= $message->header
;
947 $filename = $message->getFilename();
950 $filename = str_replace(' ', ' ', decodeHeader($filename));
951 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
952 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode')) {
953 $filename = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode', $filename);
955 $localfilename = GenerateRandomString(32, '', 7);
956 $full_localfilename = "$hashed_attachment_dir/$localfilename";
957 while (file_exists($full_localfilename)) {
958 $localfilename = GenerateRandomString(32, '', 7);
959 $full_localfilename = "$hashed_attachment_dir/$localfilename";
961 $message->att_local_name
= $full_localfilename;
963 $composeMessage->initAttachment($message->type0
.'/'.$message->type1
,$filename,
964 $full_localfilename);
966 /* Write Attachment to file */
967 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
968 mime_print_body_lines ($imapConnection, $passed_id, $message->entity_id
, $message->header
->encoding
, $fp);
972 for ($i=0, $entCount=count($message->entities
); $i<$entCount;$i++
) {
973 $composeMessage=getAttachments($message->entities
[$i], $composeMessage, $passed_id, $entities, $imapConnection);
976 return $composeMessage;
979 function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
980 $passed_ent_id='', $imapConnection) {
981 global $attachment_dir, $username, $data_dir;
982 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
983 if (!$passed_ent_id) {
984 $body_a = sqimap_run_command($imapConnection,
985 'FETCH '.$passed_id.' RFC822',
986 TRUE, $response, $readmessage,
989 $body_a = sqimap_run_command($imapConnection,
990 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
991 TRUE, $response, $readmessage, TRUE);
992 $message = $message->parent
;
994 if ($response == 'OK') {
995 $subject = encodeHeader($message->rfc822_header
->subject
);
996 array_shift($body_a);
998 $body = implode('', $body_a) . "\r\n";
1000 $localfilename = GenerateRandomString(32, 'FILE', 7);
1001 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1003 $fp = fopen($full_localfilename, 'w');
1004 fwrite ($fp, $body);
1006 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
1007 $full_localfilename);
1009 return $composeMessage;
1012 function showInputForm ($session, $values=false) {
1013 global $send_to, $send_to_cc, $body, $startMessage, $action,
1014 $color, $use_signature, $signature, $prefix_sig,
1015 $editor_size, $editor_height, $subject, $newmail,
1016 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
1017 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
1018 $username, $data_dir, $identity, $idents, $delete_draft,
1019 $mailprio, $compose_new_win, $saved_draft, $mail_sent, $sig_first,
1020 $username, $compose_messages, $composesession, $default_charset;
1022 if (checkForJavascript()) {
1023 $onfocus = ' onfocus="alreadyFocused=true;"';
1024 $onfocus_array = array('onfocus' => 'alreadyFocused=true;');
1028 $onfocus_array = array();
1031 $composeMessage = $compose_messages[$session];
1033 $send_to = $values['send_to'];
1034 $send_to_cc = $values['send_to_cc'];
1035 $send_to_bcc = $values['send_to_bcc'];
1036 $subject = $values['subject'];
1037 $mailprio = $values['mailprio'];
1038 $body = $values['body'];
1039 $identity = (int) $values['identity'];
1041 $send_to = decodeHeader($send_to, true, false);
1042 $send_to_cc = decodeHeader($send_to_cc, true, false);
1043 $send_to_bcc = decodeHeader($send_to_bcc, true, false);
1046 if ($use_javascript_addr_book) {
1047 echo "\n". '<script language="JavaScript">'."\n<!--\n" .
1048 'function open_abook() { ' . "\n" .
1049 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
1050 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
1051 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
1052 ' nwin.opener = document.windows;' . "\n" .
1054 "// -->\n</script>\n\n";
1057 echo "\n" . '<form name="compose" action="compose.php" method="post" ' .
1058 'enctype="multipart/form-data"';
1059 do_hook('compose_form');
1063 echo addHidden('startMessage', $startMessage);
1065 if ($action == 'draft') {
1066 echo addHidden('delete_draft', $passed_id);
1068 if (isset($delete_draft)) {
1069 echo addHidden('delete_draft', $delete_draft);
1071 if (isset($session)) {
1072 echo addHidden('session', $session);
1075 if (isset($passed_id)) {
1076 echo addHidden('passed_id', $passed_id);
1079 if ($saved_draft == 'yes') {
1080 echo '<br /><center><b>'. _("Draft Saved").'</center></b>';
1082 if ($mail_sent == 'yes') {
1083 echo '<br /><center><b>'. _("Your Message has been sent.").'</center></b>';
1085 if ($compose_new_win == '1') {
1086 echo '<table align="center" bgcolor="'.$color[0].'" width="100%" border="0">'."\n" .
1087 ' <tr><td></td>'.html_tag( 'td', '', 'right' ).
1088 '<input type="button" name="Close" onclick="return self.close()" value="'.
1089 _("Close").'" /></td></tr>'."\n";
1091 echo '<table align="center" cellspacing="0" border="0">' . "\n";
1093 if ($location_of_buttons == 'top') {
1094 showComposeButtonRow();
1097 /* display select list for identities */
1098 if (count($idents) > 1) {
1099 $ident_list = array();
1100 foreach($idents as $id => $data) {
1102 $data['full_name'].' <'.$data['email_address'].'>';
1104 echo ' <tr>' . "\n" .
1105 html_tag( 'td', '', 'right', $color[4], 'width="10%"' ) .
1106 _("From:") . '</td>' . "\n" .
1107 html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
1109 addSelect('identity', $ident_list, $identity, TRUE);
1111 echo ' </td>' . "\n" .
1115 echo ' <tr>' . "\n" .
1116 html_tag( 'td', '', 'right', $color[4], 'width="10%"' ) .
1117 _("To") . ':</td>' . "\n" .
1118 html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
1119 addInput('send_to', $send_to, 60, 0, $onfocus_array). '<br />' . "\n" .
1123 html_tag( 'td', '', 'right', $color[4] ) .
1124 _("Cc") . ':</td>' . "\n" .
1125 html_tag( 'td', '', 'left', $color[4] ) .
1126 addInput('send_to_cc', $send_to_cc, 60, 0, $onfocus_array). '<br />' . "\n" .
1130 html_tag( 'td', '', 'right', $color[4] ) .
1131 _("Bcc") . ':</td>' . "\n" .
1132 html_tag( 'td', '', 'left', $color[4] ) .
1133 addInput('send_to_bcc', $send_to_bcc, 60, 0, $onfocus_array).'<br />' . "\n" .
1137 html_tag( 'td', '', 'right', $color[4] ) .
1138 _("Subject") . ':</td>' . "\n" .
1139 html_tag( 'td', '', 'left', $color[4] ) . "\n";
1140 echo ' '.addInput('subject', $subject, 60, 0, $onfocus_array).
1144 if ($location_of_buttons == 'between') {
1145 showComposeButtonRow();
1148 /* why this distinction? */
1149 if ($compose_new_win == '1') {
1150 echo ' <tr>' . "\n" .
1151 ' <td bgcolor="' . $color[0] . '" colspan="2" align="center">' . "\n" .
1152 ' <textarea name="body" id="body" rows="' . (int)$editor_height .
1153 '" cols="' . (int)$editor_size . '" wrap="virtual"' . $onfocus . '>';
1156 echo ' <tr>' . "\n" .
1157 ' <td bgcolor="' . $color[4] . '" colspan="2">' . "\n" .
1158 ' <textarea name="body" id="body" rows="' . (int)$editor_height .
1159 '" cols="' . (int)$editor_size . '" wrap="virtual"' . $onfocus . '>';
1162 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
1163 $signature = $idents[$identity]['signature'];
1165 if ($sig_first == '1') {
1167 * FIXME: test is specific to ja_JP translation implementation.
1168 * This test might apply incorrect conversion to other translations, but
1169 * use of 7bit iso-2022-jp charset in other translations might have other
1172 if ($default_charset == 'iso-2022-jp') {
1173 echo "\n\n".($prefix_sig==true?
"-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1175 echo "\n\n".($prefix_sig==true?
"-- \n":'').decodeHeader($signature,false,false);
1177 echo "\n\n".htmlspecialchars(decodeHeader($body,false,false));
1180 echo "\n\n".htmlspecialchars(decodeHeader($body,false,false));
1181 // FIXME: test is specific to ja_JP translation implementation. See above comments.
1182 if ($default_charset == 'iso-2022-jp') {
1183 echo "\n\n".($prefix_sig==true?
"-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1185 echo "\n\n".($prefix_sig==true?
"-- \n":'').decodeHeader($signature,false,false);
1189 echo htmlspecialchars(decodeHeader($body,false,false));
1191 echo '</textarea><br />' . "\n" .
1196 if ($location_of_buttons == 'bottom') {
1197 showComposeButtonRow();
1199 echo ' <tr>' . "\n" .
1200 html_tag( 'td', '', 'right', '', 'colspan="2"' ) . "\n" .
1201 ' ' . addSubmit(_("Send"), 'send').
1202 ' <br /><br />' . "\n" .
1207 /* This code is for attachments */
1208 if ((bool) ini_get('file_uploads')) {
1210 /* Calculate the max size for an uploaded file.
1211 * This is advisory for the user because we can't actually prevent
1212 * people to upload too large files. */
1214 /* php.ini vars which influence the max for uploads */
1215 $configvars = array('post_max_size', 'memory_limit', 'upload_max_filesize');
1216 foreach($configvars as $var) {
1217 /* skip 0 or empty values, and -1 which means 'unlimited' */
1218 if( $size = getByteSize(ini_get($var)) ) {
1219 if ( $size != '-1' ) {
1225 if(count($sizes) > 0) {
1226 $maxsize = '(max. ' . show_readable_size( min( $sizes ) ) . ')';
1227 echo addHidden('MAX_FILE_SIZE', min( $sizes ));
1231 echo ' <tr>' . "\n" .
1232 ' <td colspan="2">' . "\n" .
1233 ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.
1234 ' border="0" bgcolor="'.$color[9].'">' . "\n" .
1237 ' <table width="100%" cellpadding="3" cellspacing="0" align="center"'.
1238 ' border="0">' . "\n" .
1240 html_tag( 'td', '', 'right', '', 'valign="middle"' ) .
1241 _("Attach:") . '</td>' . "\n" .
1242 html_tag( 'td', '', 'left', '', 'valign="middle"' ) .
1243 ' <input name="attachfile" size="48" type="file" />' . "\n" .
1244 ' <input type="submit" name="attach"' .
1245 ' value="' . _("Add") .'" />' . "\n" .
1251 if ($composeMessage->entities
) {
1252 foreach ($composeMessage->entities
as $key => $attachment) {
1253 $attached_file = $attachment->att_local_name
;
1254 if ($attachment->att_local_name ||
$attachment->body_part
) {
1255 $attached_filename = decodeHeader($attachment->mime_header
->getParameter('name'));
1256 $type = $attachment->mime_header
->type0
.'/'.
1257 $attachment->mime_header
->type1
;
1259 $s_a[] = '<table bgcolor="'.$color[0].
1260 '" border="0"><tr><td>'.
1261 addCheckBox('delete[]', FALSE, $key).
1262 "</td><td>\n" . $attached_filename .
1263 '</td><td>-</td><td> ' . $type . '</td><td>('.
1264 show_readable_size( filesize( $attached_file ) ) . ')</td></tr></table>'."\n";
1269 foreach ($s_a as $s) {
1270 echo '<tr>' . html_tag( 'td', '', 'left', $color[0], 'colspan="2"' ) . $s .'</td></tr>';
1272 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
1273 _("Delete selected attachments") . "\" />\n" .
1276 echo ' </table>' . "\n" .
1279 ' </table>' . "\n" .
1282 } // End of file_uploads if-block
1283 /* End of attachment code */
1284 echo '</table>' . "\n" .
1285 addHidden('username', $username).
1286 addHidden('smaction', $action).
1287 addHidden('mailbox', $mailbox);
1289 store the complete ComposeMessages array in a hidden input value
1290 so we can restore them in case of a session timeout.
1292 sqgetGlobalVar('QUERY_STRING', $queryString, SQ_SERVER
);
1293 echo addHidden('restoremessages', serialize($compose_messages)).
1294 addHidden('composesession', $composesession).
1295 addHidden('querystring', $queryString).
1297 if (!(bool) ini_get('file_uploads')) {
1298 /* File uploads are off, so we didn't show that part of the form.
1299 To avoid bogus bug reports, tell the user why. */
1300 echo '<p style="text-align:center">'
1301 . _("Because PHP file uploads are turned off, you can not attach files to this message. Please see your system administrator for details.")
1305 do_hook('compose_bottom');
1306 echo '</body></html>' . "\n";
1310 function showComposeButtonRow() {
1311 global $use_javascript_addr_book, $save_as_draft,
1312 $default_use_priority, $mailprio, $default_use_mdn,
1313 $request_mdn, $request_dr,
1314 $data_dir, $username;
1316 echo ' <tr>' . "\n" .
1317 ' <td></td>' . "\n" .
1319 if ($default_use_priority) {
1320 if(!isset($mailprio)) {
1323 echo ' ' . _("Priority") .
1324 addSelect('mailprio', array(
1327 '5' => _("Low") ), $mailprio, TRUE);
1329 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1330 if ($default_use_mdn) {
1331 if ($mdn_user_support) {
1332 echo ' ' . _("Receipt") .': '.
1333 addCheckBox('request_mdn', $request_mdn == '1', '1'). _("On Read").
1334 addCheckBox('request_dr', $request_dr == '1', '1'). _("On Delivery");
1338 echo ' </td>' . "\n" .
1341 ' <td></td>' . "\n" .
1343 ' <input type="submit" name="sigappend" value="' . _("Signature") . '" />' . "\n";
1344 if ($use_javascript_addr_book) {
1345 echo " <script language=\"JavaScript\"><!--\n document.write(\"".
1346 " <input type=button value=\\\""._("Addresses").
1347 "\\\" onclick=\\\"javascript:open_abook();\\\" />\");".
1348 " // --></script><noscript>\n".
1349 ' <input type="submit" name="html_addr_search" value="'.
1350 _("Addresses").'" />'.
1353 echo ' <input type="submit" name="html_addr_search" value="'.
1354 _("Addresses").'" />' . "\n";
1357 if ($save_as_draft) {
1358 echo ' <input type="submit" name ="draft" value="' . _("Save Draft") . "\" />\n";
1361 echo ' <input type="submit" name="send" value="'. _("Send") . '" />' . "\n";
1362 do_hook('compose_button_row');
1364 echo ' </td>' . "\n" .
1368 function checkInput ($show) {
1370 * I implemented the $show variable because the error messages
1371 * were getting sent before the page header. So, I check once
1372 * using $show=false, and then when i'm ready to display the error
1373 * message, show=true
1375 global $body, $send_to, $send_to_bcc, $subject, $color;
1377 if ($send_to == '' && $send_to_bcc == '') {
1379 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
1384 } /* function checkInput() */
1387 /* True if FAILURE */
1388 function saveAttachedFiles($session) {
1389 global $_FILES, $attachment_dir, $attachments, $username,
1390 $data_dir, $compose_messages;
1392 /* get out of here if no file was attached at all */
1393 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1397 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1398 $localfilename = GenerateRandomString(32, '', 7);
1399 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1400 while (file_exists($full_localfilename)) {
1401 $localfilename = GenerateRandomString(32, '', 7);
1402 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1405 // FIXME: we SHOULD prefer move_uploaded_file over rename because
1406 // m_u_f works better with restricted PHP installs (safe_mode, open_basedir)
1407 if (!@rename
($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1408 if (!@move_uploaded_file
($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
1412 $message = $compose_messages[$session];
1413 $type = strtolower($_FILES['attachfile']['type']);
1414 $name = $_FILES['attachfile']['name'];
1415 $message->initAttachment($type, $name, $full_localfilename);
1416 $compose_messages[$session] = $message;
1417 sqsession_register($compose_messages , 'compose_messages');
1420 function ClearAttachments($composeMessage) {
1421 if ($composeMessage->att_local_name
) {
1422 $attached_file = $composeMessage->att_local_name
;
1423 if (file_exists($attached_file)) {
1424 unlink($attached_file);
1427 for ($i=0, $entCount=count($composeMessage->entities
);$i< $entCount; ++
$i) {
1428 ClearAttachments($composeMessage->entities
[$i]);
1432 /* parse values like 8M and 2k into bytes */
1433 function getByteSize($ini_size) {
1439 $ini_size = trim($ini_size);
1441 // if there's some kind of letter at the end of the string we need to multiply.
1442 if(!is_numeric(substr($ini_size, -1))) {
1444 switch(strtoupper(substr($ini_size, -1))) {
1446 $bytesize = 1073741824;
1449 $bytesize = 1048576;
1456 return ($bytesize * (int)substr($ini_size, 0, -1));
1464 * temporary function to make use of the deliver class.
1465 * In the future the responsable backend should be automaticly loaded
1466 * and conf.pl should show a list of available backends.
1467 * The message also should be constructed by the message class.
1469 function deliverMessage($composeMessage, $draft=false) {
1470 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
1471 $username, $popuser, $usernamedata, $identity, $idents, $data_dir,
1472 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
1473 $domain, $action, $default_move_to_sent, $move_to_sent;
1474 global $imapServerAddress, $imapPort, $sent_folder, $key;
1476 $rfc822_header = $composeMessage->rfc822_header
;
1478 $abook = addressbook_init(false, true);
1479 $rfc822_header->to
= $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
1480 $rfc822_header->cc
= $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
1481 $rfc822_header->bcc
= $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
1482 $rfc822_header->priority
= $mailprio;
1483 $rfc822_header->subject
= $subject;
1485 $special_encoding='';
1486 if (strtolower($default_charset) == 'iso-2022-jp') {
1487 if (mb_detect_encoding($body) == 'ASCII') {
1488 $special_encoding = '8bit';
1490 $body = mb_convert_encoding($body, 'JIS');
1491 $special_encoding = '7bit';
1494 $composeMessage->setBody($body);
1496 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
1497 $popuser = $usernamedata[1];
1498 $domain = $usernamedata[2];
1499 unset($usernamedata);
1501 $popuser = $username;
1504 $from_mail = $idents[$identity]['email_address'];
1505 $full_name = $idents[$identity]['full_name'];
1506 $reply_to = $idents[$identity]['reply_to'];
1508 $from_mail = "$popuser@$domain";
1510 $rfc822_header->from
= $rfc822_header->parseAddress($from_mail,true);
1512 $from = $rfc822_header->from
[0];
1513 if (!$from->host
) $from->host
= $domain;
1514 $full_name_encoded = encodeHeader($full_name);
1515 if ($full_name_encoded != $full_name) {
1516 $from_addr = $full_name_encoded .' <'.$from->mailbox
.'@'.$from->host
.'>';
1518 $from_addr = '"'.$full_name .'" <'.$from->mailbox
.'@'.$from->host
.'>';
1520 $rfc822_header->from
= $rfc822_header->parseAddress($from_addr,true);
1523 $rfc822_header->reply_to
= $rfc822_header->parseAddress($reply_to,true);
1525 /* Receipt: On Read */
1526 if (isset($request_mdn) && $request_mdn) {
1527 $rfc822_header->dnt
= $rfc822_header->parseAddress($from_mail,true);
1529 /* Receipt: On Delivery */
1530 if (isset($request_dr) && $request_dr) {
1531 $rfc822_header->more_headers
['Return-Receipt-To'] = $from_mail;
1533 /* multipart messages */
1534 if (count($composeMessage->entities
)) {
1535 $message_body = new Message();
1536 $message_body->body_part
= $composeMessage->body_part
;
1537 $composeMessage->body_part
= '';
1538 $mime_header = new MessageHeader
;
1539 $mime_header->type0
= 'text';
1540 $mime_header->type1
= 'plain';
1541 if ($special_encoding) {
1542 $mime_header->encoding
= $special_encoding;
1544 $mime_header->encoding
= '8bit';
1546 if ($default_charset) {
1547 $mime_header->parameters
['charset'] = $default_charset;
1549 $message_body->mime_header
= $mime_header;
1550 array_unshift($composeMessage->entities
, $message_body);
1551 $content_type = new ContentType('multipart/mixed');
1553 $content_type = new ContentType('text/plain');
1554 if ($special_encoding) {
1555 $rfc822_header->encoding
= $special_encoding;
1557 $rfc822_header->encoding
= '8bit';
1559 if ($default_charset) {
1560 $content_type->properties
['charset']=$default_charset;
1564 $rfc822_header->content_type
= $content_type;
1565 $composeMessage->rfc822_header
= $rfc822_header;
1567 /* Here you can modify the message structure just before we hand
1568 it over to deliver */
1569 $hookReturn = do_hook('compose_send', $composeMessage);
1570 /* Get any changes made by plugins to $composeMessage. */
1571 if ( is_object($hookReturn[1]) ) {
1572 $composeMessage = $hookReturn[1];
1575 if (!$useSendmail && !$draft) {
1576 require_once(SM_PATH
. 'class/deliver/Deliver_SMTP.class.php');
1577 $deliver = new Deliver_SMTP();
1578 global $smtpServerAddress, $smtpPort, $pop_before_smtp;
1580 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ?
true : false;
1581 get_smtp_user($user, $pass);
1582 $stream = $deliver->initStream($composeMessage,$domain,0,
1583 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
1584 } elseif (!$draft) {
1585 require_once(SM_PATH
. 'class/deliver/Deliver_SendMail.class.php');
1586 global $sendmail_path;
1587 $deliver = new Deliver_SendMail();
1588 $stream = $deliver->initStream($composeMessage,$sendmail_path);
1590 global $draft_folder;
1591 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1592 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1594 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
1595 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1596 $imap_deliver = new Deliver_IMAP();
1597 $length = $imap_deliver->mail($composeMessage);
1598 sqimap_append ($imap_stream, $draft_folder, $length);
1599 $imap_deliver->mail($composeMessage, $imap_stream);
1600 sqimap_append_done ($imap_stream, $draft_folder);
1601 sqimap_logout($imap_stream);
1602 unset ($imap_deliver);
1605 $msg = '<br />'.sprintf(_("Error: Draft folder %s does not exist."), $draft_folder);
1606 plain_error_message($msg, $color);
1612 $length = $deliver->mail($composeMessage, $stream);
1613 $succes = $deliver->finalizeStream($stream);
1616 $msg = $deliver->dlv_msg
. '<br />' .
1617 _("Server replied:") . ' ' . $deliver->dlv_ret_nr
. ' ' .
1618 $deliver->dlv_server_msg
;
1619 plain_error_message($msg, $color);
1622 $move_to_sent = getPref($data_dir,$username,'move_to_sent');
1623 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
1625 /* Move to sent code */
1626 if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
1627 $svr_allow_sent = true;
1629 $svr_allow_sent = false;
1632 if (isset($sent_folder) && (($sent_folder != '') ||
($sent_folder != 'none'))
1633 && sqimap_mailbox_exists( $imap_stream, $sent_folder)) {
1639 if ((isset($move_to_sent) && ($move_to_sent != 0)) ||
(!isset($move_to_sent))) {
1640 $lcl_allow_sent = true;
1642 $lcl_allow_sent = false;
1645 if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) ||
($fld_sent && $lcl_allow_sent)) {
1646 global $passed_id, $mailbox, $action;
1647 if ($action == 'reply' ||
$action == 'reply_all') {
1648 $save_reply_with_orig=getPref($data_dir,$username,'save_reply_with_orig');
1649 if ($save_reply_with_orig) {
1650 $sent_folder = $mailbox;
1653 sqimap_append ($imap_stream, $sent_folder, $length);
1654 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1655 $imap_deliver = new Deliver_IMAP();
1656 $imap_deliver->mail($composeMessage, $imap_stream);
1657 sqimap_append_done ($imap_stream, $sent_folder);
1658 unset ($imap_deliver);
1660 global $passed_id, $mailbox, $action;
1661 ClearAttachments($composeMessage);
1662 if ($action == 'reply' ||
$action == 'reply_all') {
1663 sqimap_mailbox_select ($imap_stream, $mailbox);
1664 sqimap_toggle_flag($imap_stream, array($passed_id), '\\Answered', true, false);
1666 sqimap_logout($imap_stream);