6 * This code sends a mail.
8 * There are 4 modes of operation:
14 * @copyright © 1999-2006 The SquirrelMail Project Team
15 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
17 * @package squirrelmail
21 * Include the SquirrelMail initialization file.
23 require('../include/init.php');
25 /* SquirrelMail required files. */
26 require_once(SM_PATH
. 'functions/imap_general.php');
27 require_once(SM_PATH
. 'functions/imap_messages.php');
28 require_once(SM_PATH
. 'functions/date.php');
29 require_once(SM_PATH
. 'functions/mime.php');
30 require_once(SM_PATH
. 'class/deliver/Deliver.class.php');
31 require_once(SM_PATH
. 'functions/addressbook.php');
32 require_once(SM_PATH
. 'functions/forms.php');
33 require_once(SM_PATH
. 'functions/identity.php');
35 /* --------------------- Get globals ------------------------------------- */
37 sqgetGlobalVar('key', $key, SQ_COOKIE
);
40 sqgetGlobalVar('username', $username, SQ_SESSION
);
41 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
42 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
43 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
45 sqgetGlobalVar('composesession', $composesession, SQ_SESSION
);
46 sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION
);
47 sqgetGlobalVar('delayed_errors', $delayed_errors, SQ_SESSION
);
48 if (is_array($delayed_errors)) {
49 $oErrorHandler->AssignDelayedErrors($delayed_errors);
50 sqsession_unregister("delayed_errors");
53 /** SESSION/POST/GET VARS */
54 sqgetGlobalVar('session',$session);
55 sqgetGlobalVar('mailbox',$mailbox);
56 if(!sqgetGlobalVar('identity',$identity)) {
59 sqgetGlobalVar('send_to',$send_to);
60 sqgetGlobalVar('send_to_cc',$send_to_cc);
61 sqgetGlobalVar('send_to_bcc',$send_to_bcc);
62 sqgetGlobalVar('subject',$subject);
63 sqgetGlobalVar('body',$body);
64 sqgetGlobalVar('mailprio',$mailprio);
65 sqgetGlobalVar('request_mdn',$request_mdn);
66 sqgetGlobalVar('request_dr',$request_dr);
67 sqgetGlobalVar('html_addr_search',$html_addr_search);
68 sqgetGlobalVar('mail_sent',$mail_sent);
69 sqgetGlobalVar('passed_id',$passed_id);
70 sqgetGlobalVar('passed_ent_id',$passed_ent_id);
71 sqgetGlobalVar('send',$send);
73 sqgetGlobalVar('attach',$attach);
75 sqgetGlobalVar('draft',$draft);
76 sqgetGlobalVar('draft_id',$draft_id);
77 sqgetGlobalVar('ent_num',$ent_num);
78 sqgetGlobalVar('saved_draft',$saved_draft);
79 sqgetGlobalVar('delete_draft',$delete_draft);
80 if ( sqgetGlobalVar('startMessage',$startMessage) ) {
81 $startMessage = (int)$startMessage;
88 sqgetGlobalVar('sigappend', $sigappend, SQ_POST
);
89 sqgetGlobalVar('from_htmladdr_search', $from_htmladdr_search, SQ_POST
);
90 sqgetGlobalVar('addr_search_done', $html_addr_search_done, SQ_POST
);
91 sqgetGlobalVar('send_to_search', $send_to_search, SQ_POST
);
92 sqgetGlobalVar('do_delete', $do_delete, SQ_POST
);
93 sqgetGlobalVar('delete', $delete, SQ_POST
);
94 sqgetGlobalVar('restoremessages', $restoremessages, SQ_POST
);
95 if ( sqgetGlobalVar('return', $temp, SQ_POST
) ) {
96 $html_addr_search_done = 'Use Addresses';
100 sqgetGlobalVar('attachedmessages', $attachedmessages, SQ_GET
);
101 if ( sqgetGlobalVar('account', $temp, SQ_GET
) ) {
102 $iAccount = (int) $temp;
109 if ( !sqgetGlobalVar('smaction',$action) )
111 if ( sqgetGlobalVar('smaction_reply',$tmp) ) $action = 'reply';
112 if ( sqgetGlobalVar('smaction_reply_all',$tmp) ) $action = 'reply_all';
113 if ( sqgetGlobalVar('smaction_forward',$tmp) ) $action = 'forward';
114 if ( sqgetGlobalVar('smaction_attache',$tmp) ) $action = 'forward_as_attachment';
115 if ( sqgetGlobalVar('smaction_draft',$tmp) ) $action = 'draft';
116 if ( sqgetGlobalVar('smaction_edit_new',$tmp) ) $action = 'edit_as_new';
119 /* Location (For HTTP 1.1 Header("Location: ...") redirects) */
120 $location = get_location();
121 /* Identities (fetch only once) */
122 $idents = get_identities();
124 /* --------------------- Specific Functions ------------------------------ */
126 function replyAllString($header) {
127 global $include_self_reply_all, $idents;
130 * 1) Remove the addresses we'll be sending the message 'to'
132 if (isset($header->replyto
)) {
133 $excl_ar = $header->getAddr_a('replyto');
136 * 2) Remove our identities from the CC list (they still can be in the
137 * TO list) only if $include_self_reply_all is turned off
139 if (!$include_self_reply_all) {
140 foreach($idents as $id) {
141 $excl_ar[strtolower(trim($id['email_address']))] = '';
146 * 3) get the addresses.
148 $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar);
151 * 4) generate the string.
153 $url_replytoallcc = '';
154 foreach( $url_replytoall_ar as $email => $personal) {
156 // if personal name contains address separator then surround
157 // the personal name with double quotes.
158 if (strpos($personal,',') !== false) {
159 $personal = '"'.$personal.'"';
161 $url_replytoallcc .= ", $personal <$email>";
163 $url_replytoallcc .= ', '. $email;
166 $url_replytoallcc = substr($url_replytoallcc,2);
168 return $url_replytoallcc;
172 * creates top line in reply citations
174 * Line style depends on user preferences.
175 * $orig_date argument is available only from 1.4.3 and 1.5.1 version.
176 * @param object $orig_from From: header object.
177 * @param integer $orig_date email's timestamp
178 * @return string reply citation
180 function getReplyCitation($orig_from, $orig_date) {
181 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
183 if (!is_object($orig_from)) {
186 $sOrig_from = decodeHeader($orig_from->getAddress(false),false,false,true);
189 /* First, return an empty string when no citation style selected. */
190 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
194 /* Make sure our final value isn't an empty string. */
195 if ($sOrig_from == '') {
199 /* Otherwise, try to select the desired citation style. */
200 switch ($reply_citation_style) {
203 * To translators: %s is for author's name
205 $full_reply_citation = sprintf(_("%s wrote:"),$sOrig_from);
208 $start = '<quote who="';
210 $full_reply_citation = $start . $sOrig_from . $end;
212 case 'date_time_author':
215 * first %s is for date string, second %s is for author's name. Date uses
216 * formating from "D, F j, Y g:i a" and "D, F j, Y H:i" translations.
218 * "On Sat, December 24, 2004 23:59, Santa wrote:"
219 * If you have to put author's name in front of date string, check comments about
220 * argument swapping at http://www.php.net/sprintf
222 $full_reply_citation = sprintf(_("On %s, %s wrote:"), getLongDateString($orig_date), $sOrig_from);
225 $start = $reply_citation_start .
226 ($reply_citation_start == '' ?
'' : ' ');
227 $end = $reply_citation_end;
228 $full_reply_citation = $start . $sOrig_from . $end;
234 /* Add line feed and return the citation string. */
235 return ($full_reply_citation . "\n");
239 * Creates header fields in forwarded email body
241 * $default_charset global must be set correctly before you call this function.
242 * @param object $orig_header
245 function getforwardHeader($orig_header) {
246 global $editor_size, $default_charset;
248 // using own strlen function in order to detect correct string length
249 $display = array( _("Subject") => sq_strlen(_("Subject"),$default_charset),
250 _("From") => sq_strlen(_("From"),$default_charset),
251 _("Date") => sq_strlen(_("Date"),$default_charset),
252 _("To") => sq_strlen(_("To"),$default_charset),
253 _("Cc") => sq_strlen(_("Cc"),$default_charset) );
254 $maxsize = max($display);
255 $indent = str_pad('',$maxsize+
2);
256 foreach($display as $key => $val) {
257 $display[$key] = $key .': '. str_pad('', $maxsize - $val);
259 $from = decodeHeader($orig_header->getAddr_s('from',"\n$indent"),false,false,true);
260 $from = str_replace(' ',' ',$from);
261 $to = decodeHeader($orig_header->getAddr_s('to',"\n$indent"),false,false,true);
262 $to = str_replace(' ',' ',$to);
263 $subject = decodeHeader($orig_header->subject
,false,false,true);
264 $subject = str_replace(' ',' ',$subject);
266 // using own str_pad function in order to create correct string pad
267 $bodyTop = sq_str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH
,$default_charset) .
268 "\n". $display[_("Subject")] . $subject . "\n" .
269 $display[_("From")] . $from . "\n" .
270 $display[_("Date")] . getLongDateString( $orig_header->date
). "\n" .
271 $display[_("To")] . $to . "\n";
272 if ($orig_header->cc
!= array() && $orig_header->cc
!='') {
273 $cc = decodeHeader($orig_header->getAddr_s('cc',"\n$indent"),false,false,true);
274 $cc = str_replace(' ',' ',$cc);
275 $bodyTop .= $display[_("Cc")] .$cc . "\n";
277 $bodyTop .= str_pad('', $editor_size -2 , '-') .
281 /* ----------------------------------------------------------------------- */
284 * If the session is expired during a post this restores the compose session
287 if (sqsession_is_registered('session_expired_post')) {
288 sqgetGlobalVar('session_expired_post', $session_expired_post, SQ_SESSION
);
290 * extra check for username so we don't display previous post data from
291 * another user during this session.
293 if ($session_expired_post['username'] != $username) {
294 unset($session_expired_post);
295 sqsession_unregister('session_expired_post');
296 session_write_close();
298 foreach ($session_expired_post as $postvar => $val) {
305 $compose_messages = unserialize(urldecode($restoremessages));
306 sqsession_register($compose_messages,'compose_messages');
307 sqsession_register($composesession,'composesession');
311 $session_expired = true;
313 unset($session_expired_post);
314 sqsession_unregister('session_expired_post');
315 session_write_close();
316 if (!isset($mailbox)) {
319 if ($compose_new_win == '1') {
320 compose_Header($color, $mailbox);
322 $sHeaderJs = (isset($sHeaderJs)) ?
$sHeaderJs : '';
323 if (strpos($action, 'reply') !== false && $reply_focus) {
324 $sBodyTagJs = 'onload="checkForm(\''.$replyfocus.'\');"';
326 $sBodyTagJs = 'onload="checkForm();"';
328 displayPageHeader($color, $mailbox,$sHeaderJs,$sBodyTagJs);
330 showInputForm($session, false);
333 if (!isset($composesession)) {
335 sqsession_register(0,'composesession');
338 if (!isset($session) ||
(isset($newmessage) && $newmessage)) {
339 sqsession_unregister('composesession');
340 $session = "$composesession" +
1;
341 $composesession = $session;
342 sqsession_register($composesession,'composesession');
344 if (!isset($compose_messages)) {
345 $compose_messages = array();
348 if (!isset($compose_messages[$session]) ||
($compose_messages[$session] == NULL)) {
349 $composeMessage = new Message();
350 $rfc822_header = new Rfc822Header();
351 $composeMessage->rfc822_header
= $rfc822_header;
352 $composeMessage->reply_rfc822_header
= '';
353 $compose_messages[$session] = $composeMessage;
355 sqsession_register($compose_messages,'compose_messages');
357 $composeMessage=$compose_messages[$session];
360 if (!isset($mailbox) ||
$mailbox == '' ||
($mailbox == 'None')) {
366 * Set $default_charset to correspond with the user's selection
367 * of language interface.
370 $composeMessage=$compose_messages[$session];
371 if (! deliverMessage($composeMessage, true)) {
372 showInputForm($session);
375 unset($compose_messages[$session]);
376 $draft_message = _("Draft Email Saved");
377 /* If this is a resumed draft, then delete the original */
378 if(isset($delete_draft)) {
379 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, false);
380 sqimap_mailbox_select($imap_stream, $draft_folder);
381 // force bypass_trash=true because message should be saved when deliverMessage() returns true.
382 // in current implementation of sqimap_msgs_list_flag() single message id can
383 // be submitted as string. docs state that it should be array.
384 sqimap_msgs_list_delete($imap_stream, $draft_folder, $delete_draft, true);
386 sqimap_mailbox_expunge($imap_stream, $draft_folder, true);
388 sqimap_logout($imap_stream);
390 if (count($oErrorHandler->aErrors
)) {
391 sqsession_register($oErrorHandler->aErrors
,"delayed_errors");
393 session_write_close();
394 if ($compose_new_win == '1') {
395 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
396 Header("Location: $location/compose.php?saved_draft=yes&session=$composesession");
398 echo ' <br><br><div style="text-align: center;"><a href="' . $location
399 . '/compose.php?saved_sent=yes&session=' . $composesession . '">'
400 . _("Return") . '</a></div>';
404 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
405 Header("Location: $location/right_main.php?mailbox=" . urlencode($draft_folder) .
406 "&startMessage=1¬e=".urlencode($draft_message));
408 echo ' <br><br><div style="text-align: center;"><a href="' . $location
409 . '/right_main.php?mailbox=' . urlencode($draft_folder)
410 . '&startMessage=1&note=' . urlencode($draft_message) .'">'
411 . _("Return") . '</a></div>';
419 if (isset($_FILES['attachfile']) &&
420 $_FILES['attachfile']['tmp_name'] &&
421 $_FILES['attachfile']['tmp_name'] != 'none') {
422 $AttachFailure = saveAttachedFiles($session);
424 if (checkInput(false) && !isset($AttachFailure)) {
425 if ($mailbox == "All Folders") {
426 /* We entered compose via the search results page */
427 $mailbox = 'INBOX'; /* Send 'em to INBOX, that's safe enough */
429 $urlMailbox = urlencode (trim($mailbox));
430 if (! isset($passed_id)) {
434 * Set $default_charset to correspond with the user's selection
435 * of language interface.
439 * This is to change all newlines to \n
440 * We'll change them to \r\n later (in the sendMessage function)
442 $body = str_replace("\r\n", "\n", $body);
443 $body = str_replace("\r", "\n", $body);
446 * Rewrap $body so that no line is bigger than $editor_size
448 $body = explode("\n", $body);
450 foreach ($body as $line) {
451 if( $line <> '-- ' ) {
452 $line = rtrim($line);
454 if (sq_strlen($line,$default_charset) <= $editor_size +
1) {
455 $newBody .= $line . "\n";
457 sqWordWrap($line, $editor_size,$default_charset);
458 $newBody .= $line . "\n";
465 $composeMessage=$compose_messages[$session];
467 $Result = deliverMessage($composeMessage);
469 do_hook('compose_send_after', $Result, $composeMessage);
471 showInputForm($session);
474 unset($compose_messages[$session]);
476 /* if it is resumed draft, delete draft message */
477 if ( isset($delete_draft)) {
478 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, false);
479 sqimap_mailbox_select($imap_stream, $draft_folder);
480 // bypass_trash=true because message should be saved when deliverMessage() returns true.
481 // in current implementation of sqimap_msgs_list_flag() single message id can
482 // be submitted as string. docs state that it should be array.
483 sqimap_msgs_list_delete($imap_stream, $draft_folder, $delete_draft, true);
485 sqimap_mailbox_expunge($imap_stream, $draft_folder, true);
487 sqimap_logout($imap_stream);
490 * Store the error array in the session because they will be lost on a redirect
492 if (count($oErrorHandler->aErrors
)) {
493 sqsession_register($oErrorHandler->aErrors
,"delayed_errors");
495 session_write_close();
496 if ($compose_new_win == '1') {
497 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
498 Header("Location: $location/compose.php?mail_sent=yes");
500 echo ' <br><br><div style="text-align: center;"><a href="' . $location
501 . '/compose.php?mail_sent=yes">'
502 . _("Return") . '</a></div>';
506 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
507 Header("Location: $location/right_main.php?mailbox=$urlMailbox".
508 "&startMessage=$startMessage&mail_sent=yes");
510 echo ' <br><br><div style="text-align: center;"><a href="' . $location
511 . "/right_main.php?mailbox=$urlMailbox"
512 . "&startMessage=$startMessage&mail_sent=yes\">"
513 . _("Return") . '</a></div>';
518 if ($compose_new_win == '1') {
519 compose_Header($color, $mailbox);
522 displayPageHeader($color, $mailbox);
524 if (isset($AttachFailure)) {
525 plain_error_message(_("Could not move/copy file. File not attached"),
529 showInputForm($session);
530 /* sqimap_logout($imapConnection); */
532 } elseif (isset($html_addr_search_done)) {
533 if ($compose_new_win == '1') {
534 compose_Header($color, $mailbox);
537 displayPageHeader($color, $mailbox);
540 if (isset($send_to_search) && is_array($send_to_search)) {
541 foreach ($send_to_search as $k => $v) {
542 if (substr($k, 0, 1) == 'T') {
548 elseif (substr($k, 0, 1) == 'C') {
554 elseif (substr($k, 0, 1) == 'B') {
556 $send_to_bcc .= ', ';
562 showInputForm($session);
563 } elseif (isset($html_addr_search)) {
564 if (isset($_FILES['attachfile']) &&
565 $_FILES['attachfile']['tmp_name'] &&
566 $_FILES['attachfile']['tmp_name'] != 'none') {
567 if(saveAttachedFiles($session)) {
568 plain_error_message(_("Could not move/copy file. File not attached"), $color);
572 * I am using an include so as to elminiate an extra unnecessary
573 * click. If you can think of a better way, please implement it.
575 include_once('./addrbook_search_html.php');
576 } elseif (isset($attach)) {
577 if ($compose_new_win == '1') {
578 compose_Header($color, $mailbox);
580 displayPageHeader($color, $mailbox);
582 if (saveAttachedFiles($session)) {
583 plain_error_message(_("Could not move/copy file. File not attached"), $color);
585 showInputForm($session);
587 elseif (isset($sigappend)) {
588 $signature = $idents[$identity]['signature'];
590 $body .= "\n\n".($prefix_sig==true?
"-- \n":'').$signature;
591 if ($compose_new_win == '1') {
592 compose_Header($color, $mailbox);
594 displayPageHeader($color, $mailbox);
596 showInputForm($session);
597 } elseif (isset($do_delete)) {
598 if ($compose_new_win == '1') {
599 compose_Header($color, $mailbox);
601 displayPageHeader($color, $mailbox);
604 if (isset($delete) && is_array($delete)) {
605 $composeMessage = $compose_messages[$session];
606 foreach($delete as $index) {
607 if (!empty($composeMessage->entities
) && isset($composeMessage->entities
[$index])) {
608 $composeMessage->entities
[$index]->purgeAttachments();
609 unset ($composeMessage->entities
[$index]);
612 $new_entities = array();
613 foreach ($composeMessage->entities
as $entity) {
614 $new_entities[] = $entity;
616 $composeMessage->entities
= $new_entities;
617 $compose_messages[$session] = $composeMessage;
618 sqsession_register($compose_messages, 'compose_messages');
620 showInputForm($session);
623 * This handles the default case as well as the error case
624 * (they had the same code) --> if (isset($smtpErrors))
627 if ($compose_new_win == '1') {
628 compose_Header($color, $mailbox);
630 displayPageHeader($color, $mailbox);
635 if (!isset($passed_ent_id)) {
638 if (!isset($passed_id)) {
641 if (!isset($mailbox)) {
644 if (!isset($action)) {
648 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
650 /* in case the origin is not read_body.php */
651 if (isset($send_to)) {
652 $values['send_to'] = $send_to;
654 if (isset($send_to_cc)) {
655 $values['send_to_cc'] = $send_to_cc;
657 if (isset($send_to_bcc)) {
658 $values['send_to_bcc'] = $send_to_bcc;
660 if (isset($subject)) {
661 $values['subject'] = $subject;
663 showInputForm($session, $values);
668 /**************** Only function definitions go below *************/
670 function getforwardSubject($subject)
672 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
673 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
674 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
675 $subject = '[Fwd: ' . $subject . ']';
680 /* This function is used when not sending or adding attachments */
681 function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
682 global $editor_size, $default_use_priority, $body, $idents,
683 $use_signature, $data_dir, $username,
684 $username, $key, $imapServerAddress, $imapPort, $compose_messages,
685 $composeMessage, $body_quote;
686 global $languages, $squirrelmail_language, $default_charset;
689 * Set $default_charset to correspond with the user's selection
690 * of language interface. $default_charset global is not correct,
691 * if message is composed in new window.
695 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
699 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
702 sqimap_mailbox_select($imapConnection, $mailbox);
703 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
706 if ($passed_ent_id) {
707 /* redefine the messsage in case of message/rfc822 */
708 $message = $message->getEntity($passed_ent_id);
709 /* message is an entity which contains the envelope and type0=message
710 * and type1=rfc822. The actual entities are childs from
711 * $message->entities[0]. That's where the encoding and is located
714 $entities = $message->entities
[0]->findDisplayEntity
715 (array(), $alt_order = array('text/plain'));
716 if (!count($entities)) {
717 $entities = $message->entities
[0]->findDisplayEntity
718 (array(), $alt_order = array('text/plain','html/plain'));
720 $orig_header = $message->rfc822_header
; /* here is the envelope located */
721 /* redefine the message for picking up the attachments */
722 $message = $message->entities
[0];
725 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
726 if (!count($entities)) {
727 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
729 $orig_header = $message->rfc822_header
;
732 $type0 = $message->type0
;
733 $type1 = $message->type1
;
734 foreach ($entities as $ent) {
735 $msg = $message->getEntity($ent);
736 $type0 = $msg->type0
;
737 $type1 = $msg->type1
;
738 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
739 $body_part_entity = $message->getEntity($ent);
740 $bodypart = decodeBody($unencoded_bodypart,
741 $body_part_entity->header
->encoding
);
742 if ($type1 == 'html') {
743 $bodypart = str_replace("\n", ' ', $bodypart);
744 $bodypart = preg_replace(array('/<\/?p>/i','/<div><\/div>/i','/<br\s*(\/)*>/i','/<\/?div>/i'), "\n", $bodypart);
745 $bodypart = str_replace(array(' ','>','<'),array(' ','>','<'),$bodypart);
746 $bodypart = strip_tags($bodypart);
748 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
749 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
750 if (mb_detect_encoding($bodypart) != 'ASCII') {
751 $bodypart = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $bodypart);
755 // charset encoding in compose form stuff
756 if (isset($body_part_entity->header
->parameters
['charset'])) {
757 $actual = $body_part_entity->header
->parameters
['charset'];
759 $actual = 'us-ascii';
762 if ( $actual && is_conversion_safe($actual) && $actual != $default_charset){
763 $bodypart = charset_convert($actual,$bodypart,$default_charset,false);
765 // end of charset encoding in compose
769 if ($default_use_priority) {
770 $mailprio = substr($orig_header->priority
,0,1);
779 $from_o = $orig_header->from
;
780 if (is_array($from_o)) {
781 if (isset($from_o[0])) {
782 $from_o = $from_o[0];
785 if (is_object($from_o)) {
786 $orig_from = $from_o->getAddress();
791 $identities = array();
792 if (count($idents) > 1) {
793 foreach($idents as $nr=>$data) {
794 $enc_from_name = '"'.$data['full_name'].'" <'. $data['email_address'].'>';
795 if($enc_from_name == $orig_from) {
799 $identities[] = $enc_from_name;
802 $identity_match = $orig_header->findAddress($identities);
803 if ($identity_match) {
804 $identity = $identity_match;
810 $use_signature = FALSE;
811 $composeMessage->rfc822_header
= $orig_header;
812 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
813 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
814 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
815 $send_from = $orig_header->getAddr_s('from');
816 $send_from_parts = new AddressStructure();
817 $send_from_parts = $orig_header->parseAddress($send_from);
818 $send_from_add = $send_from_parts->mailbox
. '@' . $send_from_parts->host
;
819 $identities = get_identities();
820 if (count($identities) > 0) {
821 foreach($identities as $iddata) {
822 if ($send_from_add == $iddata['email_address']) {
823 $identity = $iddata['index'];
828 $subject = decodeHeader($orig_header->subject
,false,false,true);
829 /* remember the references and in-reply-to headers in case of an reply */
830 $composeMessage->rfc822_header
->more_headers
['References'] = $orig_header->references
;
831 $composeMessage->rfc822_header
->more_headers
['In-Reply-To'] = $orig_header->in_reply_to
;
832 // rewrap the body to clean up quotations and line lengths
833 sqBodyWrap($body, $editor_size);
834 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
836 case ('edit_as_new'):
837 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
838 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
839 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
840 $subject = decodeHeader($orig_header->subject
,false,false,true);
841 $mailprio = $orig_header->priority
;
843 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
844 // rewrap the body to clean up quotations and line lengths
845 sqBodyWrap($body, $editor_size);
849 $subject = getforwardSubject(decodeHeader($orig_header->subject
,false,false,true));
850 $body = getforwardHeader($orig_header) . $body;
851 // the logic for calling sqUnWordWrap here would be to allow the browser to wrap the lines
852 // forwarded message text should be as undisturbed as possible, so commenting out this call
853 // sqUnWordWrap($body);
854 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
856 //add a blank line after the forward headers
857 $body = "\n" . $body;
859 case ('forward_as_attachment'):
860 $subject = getforwardSubject(decodeHeader($orig_header->subject
,false,false,true));
861 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
865 if(isset($orig_header->mail_followup_to
) && $orig_header->mail_followup_to
) {
866 $send_to = $orig_header->getAddr_s('mail_followup_to');
868 $send_to_cc = replyAllString($orig_header);
869 $send_to_cc = decodeHeader($send_to_cc,false,false,true);
872 // skip this if send_to was already set right above here
874 $send_to = $orig_header->reply_to
;
875 if (is_array($send_to) && count($send_to)) {
876 $send_to = $orig_header->getAddr_s('reply_to');
877 } else if (is_object($send_to)) { /* unneccesarry, just for failsafe purpose */
878 $send_to = $orig_header->getAddr_s('reply_to');
880 $send_to = $orig_header->getAddr_s('from');
883 $send_to = decodeHeader($send_to,false,false,true);
884 $subject = decodeHeader($orig_header->subject
,false,false,true);
885 $subject = str_replace('"', "'", $subject);
886 $subject = trim($subject);
887 if (substr(strtolower($subject), 0, 3) != 're:') {
888 $subject = 'Re: ' . $subject;
890 /* this corrects some wrapping/quoting problems on replies */
891 $rewrap_body = explode("\n", $body);
892 $from = (is_array($orig_header->from
)) ?
$orig_header->from
[0] : $orig_header->from
;
894 $strip_sigs = getPref($data_dir, $username, 'strip_sigs');
895 foreach ($rewrap_body as $line) {
896 if ($strip_sigs && substr($line,0,3) == '-- ') {
899 if (preg_match("/^(>+)/", $line, $matches)) {
901 $body .= $body_quote . str_replace("\n", "\n$body_quote$gt ", rtrim($line)) ."\n";
903 $body .= $body_quote . (!empty($body_quote) ?
' ' : '') . str_replace("\n", "\n$body_quote" . (!empty($body_quote) ?
' ' : ''), rtrim($line)) . "\n";
907 //rewrap the body to clean up quotations and line lengths
908 $body = sqBodyWrap ($body, $editor_size);
910 $body = getReplyCitation($from , $orig_header->date
) . $body;
911 $composeMessage->reply_rfc822_header
= $orig_header;
917 $compose_messages[$session] = $composeMessage;
918 sqsession_register($compose_messages, 'compose_messages');
919 session_write_close();
920 sqimap_logout($imapConnection);
922 $ret = array( 'send_to' => $send_to,
923 'send_to_cc' => $send_to_cc,
924 'send_to_bcc' => $send_to_bcc,
925 'subject' => $subject,
926 'mailprio' => $mailprio,
928 'identity' => $identity );
931 } /* function newMail() */
934 * downloads attachments from original message, stores them in attachment directory and adds
935 * them to composed message.
936 * @param object $message
937 * @param object $composeMessage
938 * @param integer $passed_id
939 * @param mixed $entities
940 * @param mixed $imapConnection
943 function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
944 global $attachment_dir, $username, $data_dir, $squirrelmail_language, $languages;
945 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
946 if (!count($message->entities
) ||
947 ($message->type0
== 'message' && $message->type1
== 'rfc822')) {
948 if ( !in_array($message->entity_id
, $entities) && $message->entity_id
) {
949 switch ($message->type0
) {
951 if ($message->type1
== 'rfc822') {
952 $filename = $message->rfc822_header
->subject
;
953 if ($filename == "") {
954 $filename = "untitled-".$message->entity_id
;
958 $filename = $message->getFilename();
962 if (!$message->mime_header
) { /* temporary hack */
963 $message->mime_header
= $message->header
;
965 $filename = $message->getFilename();
968 $filename = str_replace(' ', ' ', decodeHeader($filename));
969 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
970 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode')) {
971 $filename = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode', $filename);
973 $localfilename = GenerateRandomString(32, '', 7);
974 $full_localfilename = "$hashed_attachment_dir/$localfilename";
975 while (file_exists($full_localfilename)) {
976 $localfilename = GenerateRandomString(32, '', 7);
977 $full_localfilename = "$hashed_attachment_dir/$localfilename";
979 $message->att_local_name
= $full_localfilename;
981 $composeMessage->initAttachment($message->type0
.'/'.$message->type1
,$filename,
982 $full_localfilename);
984 /* Write Attachment to file */
985 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
986 mime_print_body_lines ($imapConnection, $passed_id, $message->entity_id
, $message->header
->encoding
, $fp);
990 for ($i=0, $entCount=count($message->entities
); $i<$entCount;$i++
) {
991 $composeMessage=getAttachments($message->entities
[$i], $composeMessage, $passed_id, $entities, $imapConnection);
994 return $composeMessage;
997 function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
998 $passed_ent_id='', $imapConnection) {
999 global $attachment_dir, $username, $data_dir;
1000 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1001 if (!$passed_ent_id) {
1002 $body_a = sqimap_run_command($imapConnection,
1003 'FETCH '.$passed_id.' RFC822',
1004 TRUE, $response, $readmessage,
1007 $body_a = sqimap_run_command($imapConnection,
1008 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
1009 TRUE, $response, $readmessage, TRUE);
1010 $message = $message->parent
;
1012 if ($response == 'OK') {
1013 $subject = encodeHeader($message->rfc822_header
->subject
);
1014 array_shift($body_a);
1016 $body = implode('', $body_a) . "\r\n";
1018 $localfilename = GenerateRandomString(32, 'FILE', 7);
1019 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1021 $fp = fopen($full_localfilename, 'w');
1022 fwrite ($fp, $body);
1024 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
1025 $full_localfilename);
1027 return $composeMessage;
1030 function showInputForm ($session, $values=false) {
1031 global $send_to, $send_to_cc, $body, $startMessage, $action,
1032 $color, $use_signature, $signature, $prefix_sig,
1033 $editor_size, $editor_height, $subject, $newmail,
1034 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
1035 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
1036 $username, $data_dir, $identity, $idents, $delete_draft,
1037 $mailprio, $compose_new_win, $saved_draft, $mail_sent, $sig_first,
1038 $username, $compose_messages, $composesession, $default_charset,
1039 $compose_onsubmit, $oTemplate;
1041 if (checkForJavascript()) {
1042 $onfocus = ' onfocus="alreadyFocused=true;"';
1043 $onfocus_array = array('onfocus' => 'alreadyFocused=true;');
1047 $onfocus_array = array();
1050 $composeMessage = $compose_messages[$session];
1052 $send_to = $values['send_to'];
1053 $send_to_cc = $values['send_to_cc'];
1054 $send_to_bcc = $values['send_to_bcc'];
1055 $subject = $values['subject'];
1056 $mailprio = $values['mailprio'];
1057 $body = $values['body'];
1058 $identity = (int) $values['identity'];
1060 $send_to = decodeHeader($send_to, true, false);
1061 $send_to_cc = decodeHeader($send_to_cc, true, false);
1062 $send_to_bcc = decodeHeader($send_to_bcc, true, false);
1065 if ($use_javascript_addr_book) {
1066 echo "\n". '<script type="text/javascript">'."\n<!--\n" .
1067 'function open_abook() { ' . "\n" .
1068 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
1069 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
1070 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
1071 ' nwin.opener = document.windows;' . "\n" .
1073 "// -->\n</script>\n\n";
1076 echo "\n" . '<form name="compose" action="compose.php" method="post" ' .
1077 'enctype="multipart/form-data"';
1079 $compose_onsubmit = array();
1080 do_hook('compose_form');
1082 // Plugins that use compose_form hook can add an array entry
1083 // to the globally scoped $compose_onsubmit; we add them up
1084 // here and format the form tag's full onsubmit handler.
1085 // Each plugin should use "return false" if they need to
1086 // stop form submission but otherwise should NOT use "return
1087 // true" to give other plugins the chance to do what they need
1088 // to do; SquirrelMail itself will add the final "return true".
1089 // Onsubmit text is enclosed inside of double quotes, so plugins
1090 // need to quote accordingly.
1091 if (checkForJavascript()) {
1092 $onsubmit_text = ' onsubmit="';
1093 if (empty($compose_onsubmit))
1094 $compose_onsubmit = array();
1095 else if (!is_array($compose_onsubmit))
1096 $compose_onsubmit = array($compose_onsubmit);
1098 foreach ($compose_onsubmit as $text) {
1099 $text = trim($text);
1100 if (substr($text, -1) != ';' && substr($text, -1) != '}')
1102 $onsubmit_text .= $text;
1105 echo $onsubmit_text . ' return true;"';
1111 echo addHidden('startMessage', $startMessage);
1113 if ($action == 'draft') {
1114 echo addHidden('delete_draft', $passed_id);
1116 if (isset($delete_draft)) {
1117 echo addHidden('delete_draft', $delete_draft);
1119 if (isset($session)) {
1120 echo addHidden('session', $session);
1123 if (isset($passed_id)) {
1124 echo addHidden('passed_id', $passed_id);
1127 if ($saved_draft == 'yes') {
1128 echo '<br /><div style="text-align: center;"><b>'. _("Draft Saved").'</div></b>';
1130 if ($mail_sent == 'yes') {
1131 echo '<br /><div style="text-align: center;"><b>'. _("Your Message has been sent.").'</div></b>';
1133 if ($compose_new_win == '1') {
1134 echo '<table align="center" bgcolor="'.$color[0].'" width="100%" border="0">'."\n" .
1135 ' <tr><td></td>'.html_tag( 'td', '', 'right' ).
1136 '<input type="button" name="Close" onclick="return self.close()" value="'.
1137 _("Close").'" /></td></tr>'."\n";
1139 echo '<table align="center" cellspacing="0" border="0">' . "\n";
1141 if ($location_of_buttons == 'top') {
1142 showComposeButtonRow();
1145 /* display select list for identities */
1146 if (count($idents) > 1) {
1147 $ident_list = array();
1148 foreach($idents as $id => $data) {
1150 $data['full_name'].' <'.$data['email_address'].'>';
1152 echo ' <tr>' . "\n" .
1153 html_tag( 'td', '', 'right', $color[4], 'width="10%"' ) .
1154 _("From:") . '</td>' . "\n" .
1155 html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
1157 addSelect('identity', $ident_list, $identity, TRUE);
1159 echo ' </td>' . "\n" .
1163 echo ' <tr>' . "\n" .
1164 html_tag( 'td', '', 'right', $color[4], 'width="10%"' ) .
1165 _("To") . ':</td>' . "\n" .
1166 html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
1167 addInput('send_to', $send_to, 60, 0, $onfocus_array). '<br />' . "\n" .
1171 html_tag( 'td', '', 'right', $color[4] ) .
1172 _("Cc") . ':</td>' . "\n" .
1173 html_tag( 'td', '', 'left', $color[4] ) .
1174 addInput('send_to_cc', $send_to_cc, 60, 0, $onfocus_array). '<br />' . "\n" .
1178 html_tag( 'td', '', 'right', $color[4] ) .
1179 _("Bcc") . ':</td>' . "\n" .
1180 html_tag( 'td', '', 'left', $color[4] ) .
1181 addInput('send_to_bcc', $send_to_bcc, 60, 0, $onfocus_array).'<br />' . "\n" .
1185 html_tag( 'td', '', 'right', $color[4] ) .
1186 _("Subject") . ':</td>' . "\n" .
1187 html_tag( 'td', '', 'left', $color[4] ) . "\n";
1188 echo ' '.addInput('subject', $subject, 60, 0, $onfocus_array).
1192 if ($location_of_buttons == 'between') {
1193 showComposeButtonRow();
1196 /* why this distinction? */
1197 if ($compose_new_win == '1') {
1198 echo ' <tr>' . "\n" .
1199 ' <td bgcolor="' . $color[0] . '" colspan="2" align="center">' . "\n" .
1200 ' <textarea name="body" id="body" rows="' . (int)$editor_height .
1201 '" cols="' . (int)$editor_size . '" wrap="virtual"' . $onfocus . '>';
1204 echo ' <tr>' . "\n" .
1205 ' <td bgcolor="' . $color[4] . '" colspan="2">' . "\n" .
1206 ' <textarea name="body" id="body" rows="' . (int)$editor_height .
1207 '" cols="' . (int)$editor_size . '" wrap="virtual"' . $onfocus . '>';
1210 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
1211 $signature = $idents[$identity]['signature'];
1213 if ($sig_first == '1') {
1215 * FIXME: test is specific to ja_JP translation implementation.
1216 * This test might apply incorrect conversion to other translations, but
1217 * use of 7bit iso-2022-jp charset in other translations might have other
1220 if ($default_charset == 'iso-2022-jp') {
1221 echo "\n\n".($prefix_sig==true?
"-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1223 echo "\n\n".($prefix_sig==true?
"-- \n":'').decodeHeader($signature,false,false);
1225 echo "\n\n".htmlspecialchars(decodeHeader($body,false,false));
1228 echo "\n\n".htmlspecialchars(decodeHeader($body,false,false));
1229 // FIXME: test is specific to ja_JP translation implementation. See above comments.
1230 if ($default_charset == 'iso-2022-jp') {
1231 echo "\n\n".($prefix_sig==true?
"-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1233 echo "\n\n".($prefix_sig==true?
"-- \n":'').decodeHeader($signature,false,false);
1237 echo htmlspecialchars(decodeHeader($body,false,false));
1239 echo '</textarea><br />' . "\n" .
1244 if ($location_of_buttons == 'bottom') {
1245 showComposeButtonRow();
1247 echo ' <tr>' . "\n" .
1248 html_tag( 'td', '', 'right', '', 'colspan="2"' ) . "\n" .
1249 ' ' . addSubmit(_("Send"), 'send').
1250 ' <br /><br />' . "\n" .
1255 /* This code is for attachments */
1256 if ((bool) ini_get('file_uploads')) {
1258 /* Calculate the max size for an uploaded file.
1259 * This is advisory for the user because we can't actually prevent
1260 * people to upload too large files. */
1262 /* php.ini vars which influence the max for uploads */
1263 $configvars = array('post_max_size', 'memory_limit', 'upload_max_filesize');
1264 foreach($configvars as $var) {
1265 /* skip 0 or empty values, and -1 which means 'unlimited' */
1266 if( $size = getByteSize(ini_get($var)) ) {
1267 if ( $size != '-1' ) {
1273 if(count($sizes) > 0) {
1274 $maxsize = '(max. ' . show_readable_size( min( $sizes ) ) . ')'
1275 . addHidden('MAX_FILE_SIZE', min( $sizes ));
1279 echo ' <tr>' . "\n" .
1280 ' <td colspan="2">' . "\n" .
1281 ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.
1282 ' border="0" bgcolor="'.$color[9].'">' . "\n" .
1285 ' <table width="100%" cellpadding="3" cellspacing="0" align="center"'.
1286 ' border="0">' . "\n" .
1288 html_tag( 'td', '', 'right', '', 'valign="middle"' ) .
1289 _("Attach:") . '</td>' . "\n" .
1290 html_tag( 'td', '', 'left', '', 'valign="middle"' ) .
1291 ' <input name="attachfile" size="48" type="file" />' . "\n" .
1292 ' <input type="submit" name="attach"' .
1293 ' value="' . _("Add") .'" />' . "\n" .
1299 if ($composeMessage->entities
) {
1300 foreach ($composeMessage->entities
as $key => $attachment) {
1301 $attached_file = $attachment->att_local_name
;
1302 if ($attachment->att_local_name ||
$attachment->body_part
) {
1303 $attached_filename = decodeHeader($attachment->mime_header
->getParameter('name'));
1304 $type = $attachment->mime_header
->type0
.'/'.
1305 $attachment->mime_header
->type1
;
1307 $s_a[] = '<table bgcolor="'.$color[0].
1308 '" border="0"><tr><td>'.
1309 addCheckBox('delete[]', FALSE, $key).
1310 "</td><td>\n" . $attached_filename .
1311 '</td><td>-</td><td> ' . $type . '</td><td>('.
1312 show_readable_size( filesize( $attached_file ) ) . ')</td></tr></table>'."\n";
1317 foreach ($s_a as $s) {
1318 echo '<tr>' . html_tag( 'td', '', 'left', $color[0], 'colspan="2"' ) . $s .'</td></tr>';
1320 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
1321 _("Delete selected attachments") . "\" />\n" .
1324 echo ' </table>' . "\n" .
1327 ' </table>' . "\n" .
1330 } // End of file_uploads if-block
1331 /* End of attachment code */
1332 echo '</table>' . "\n" .
1333 addHidden('username', $username).
1334 addHidden('smaction', $action).
1335 addHidden('mailbox', $mailbox);
1337 store the complete ComposeMessages array in a hidden input value
1338 so we can restore them in case of a session timeout.
1340 sqgetGlobalVar('QUERY_STRING', $queryString, SQ_SERVER
);
1341 echo addHidden('restoremessages', serialize($compose_messages)).
1342 addHidden('composesession', $composesession).
1343 addHidden('querystring', $queryString).
1345 if (!(bool) ini_get('file_uploads')) {
1346 /* File uploads are off, so we didn't show that part of the form.
1347 To avoid bogus bug reports, tell the user why. */
1348 echo '<p style="text-align:center">'
1349 . _("Because PHP file uploads are turned off, you can not attach files to this message. Please see your system administrator for details.")
1353 do_hook('compose_bottom');
1354 $oTemplate->display('footer.tpl');
1358 function showComposeButtonRow() {
1359 global $use_javascript_addr_book, $save_as_draft,
1360 $default_use_priority, $mailprio, $default_use_mdn,
1361 $request_mdn, $request_dr,
1362 $data_dir, $username;
1364 echo ' <tr>' . "\n" .
1365 ' <td></td>' . "\n" .
1367 if ($default_use_priority) {
1368 if(!isset($mailprio)) {
1371 echo ' ' . _("Priority") .
1372 addSelect('mailprio', array(
1375 '5' => _("Low") ), $mailprio, TRUE);
1377 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1378 if ($default_use_mdn) {
1379 if ($mdn_user_support) {
1380 echo ' ' . _("Receipt") .': '.
1381 addCheckBox('request_mdn', $request_mdn == '1', '1'). _("On Read").
1382 addCheckBox('request_dr', $request_dr == '1', '1'). _("On Delivery");
1386 echo ' </td>' . "\n" .
1389 ' <td></td>' . "\n" .
1391 ' <input type="submit" name="sigappend" value="' . _("Signature") . '" />' . "\n";
1392 if ($use_javascript_addr_book) {
1393 echo " <script type=\"text/javascript\"><!--\n document.write(\"".
1394 " <input type=button value=\\\""._("Addresses").
1395 "\\\" onclick=\\\"javascript:open_abook();\\\" />\");".
1396 " // --></script><noscript>\n".
1397 ' <input type="submit" name="html_addr_search" value="'.
1398 _("Addresses").'" />'.
1401 echo ' <input type="submit" name="html_addr_search" value="'.
1402 _("Addresses").'" />' . "\n";
1405 if ($save_as_draft) {
1406 echo ' <input type="submit" name ="draft" value="' . _("Save Draft") . "\" />\n";
1409 echo ' <input type="submit" name="send" value="'. _("Send") . '" />' . "\n";
1410 do_hook('compose_button_row');
1412 echo ' </td>' . "\n" .
1416 function checkInput ($show) {
1418 * I implemented the $show variable because the error messages
1419 * were getting sent before the page header. So, I check once
1420 * using $show=false, and then when i'm ready to display the error
1421 * message, show=true
1423 global $body, $send_to, $send_to_bcc, $subject, $color;
1425 if ($send_to == '' && $send_to_bcc == '') {
1427 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
1432 } /* function checkInput() */
1435 /* True if FAILURE */
1436 function saveAttachedFiles($session) {
1437 global $_FILES, $attachment_dir, $username,
1438 $data_dir, $compose_messages;
1440 /* get out of here if no file was attached at all */
1441 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1445 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1446 $localfilename = GenerateRandomString(32, '', 7);
1447 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1448 while (file_exists($full_localfilename)) {
1449 $localfilename = GenerateRandomString(32, '', 7);
1450 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1453 // m_u_f works better with restricted PHP installs (safe_mode, open_basedir),
1454 // if that doesn't work, try a simple rename.
1455 if (!@move_uploaded_file
($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
1456 if (!@rename
($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1460 $message = $compose_messages[$session];
1461 $type = strtolower($_FILES['attachfile']['type']);
1462 $name = $_FILES['attachfile']['name'];
1463 $message->initAttachment($type, $name, $full_localfilename);
1464 $compose_messages[$session] = $message;
1465 sqsession_register($compose_messages , 'compose_messages');
1468 /* parse values like 8M and 2k into bytes */
1469 function getByteSize($ini_size) {
1475 $ini_size = trim($ini_size);
1477 // if there's some kind of letter at the end of the string we need to multiply.
1478 if(!is_numeric(substr($ini_size, -1))) {
1480 switch(strtoupper(substr($ini_size, -1))) {
1482 $bytesize = 1073741824;
1485 $bytesize = 1048576;
1492 return ($bytesize * (int)substr($ini_size, 0, -1));
1500 * temporary function to make use of the deliver class.
1501 * In the future the responsible backend should be automaticly loaded
1502 * and conf.pl should show a list of available backends.
1503 * The message also should be constructed by the message class.
1505 function deliverMessage($composeMessage, $draft=false) {
1506 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
1507 $username, $popuser, $usernamedata, $identity, $idents, $data_dir,
1508 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
1509 $domain, $action, $default_move_to_sent, $move_to_sent;
1510 global $imapServerAddress, $imapPort, $sent_folder, $key;
1512 $rfc822_header = $composeMessage->rfc822_header
;
1514 $abook = addressbook_init(false, true);
1515 $rfc822_header->to
= $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
1516 $rfc822_header->cc
= $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
1517 $rfc822_header->bcc
= $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
1518 $rfc822_header->priority
= $mailprio;
1519 $rfc822_header->subject
= $subject;
1521 $special_encoding='';
1522 if (strtolower($default_charset) == 'iso-2022-jp') {
1523 if (mb_detect_encoding($body) == 'ASCII') {
1524 $special_encoding = '8bit';
1526 $body = mb_convert_encoding($body, 'JIS');
1527 $special_encoding = '7bit';
1530 $composeMessage->setBody($body);
1532 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
1533 $popuser = $usernamedata[1];
1534 $domain = $usernamedata[2];
1535 unset($usernamedata);
1537 $popuser = $username;
1540 $from_mail = $idents[$identity]['email_address'];
1541 $full_name = $idents[$identity]['full_name'];
1542 $reply_to = $idents[$identity]['reply_to'];
1544 $from_mail = "$popuser@$domain";
1546 $rfc822_header->from
= $rfc822_header->parseAddress($from_mail,true);
1548 $from = $rfc822_header->from
[0];
1549 if (!$from->host
) $from->host
= $domain;
1550 $full_name_encoded = encodeHeader($full_name);
1551 if ($full_name_encoded != $full_name) {
1552 $from_addr = $full_name_encoded .' <'.$from->mailbox
.'@'.$from->host
.'>';
1554 $from_addr = '"'.$full_name .'" <'.$from->mailbox
.'@'.$from->host
.'>';
1556 $rfc822_header->from
= $rfc822_header->parseAddress($from_addr,true);
1559 $rfc822_header->reply_to
= $rfc822_header->parseAddress($reply_to,true);
1561 /* Receipt: On Read */
1562 if (isset($request_mdn) && $request_mdn) {
1563 $rfc822_header->dnt
= $rfc822_header->parseAddress($from_mail,true);
1565 /* Receipt: On Delivery */
1566 if (isset($request_dr) && $request_dr) {
1567 $rfc822_header->more_headers
['Return-Receipt-To'] = $from_mail;
1569 /* multipart messages */
1570 if (count($composeMessage->entities
)) {
1571 $message_body = new Message();
1572 $message_body->body_part
= $composeMessage->body_part
;
1573 $composeMessage->body_part
= '';
1574 $mime_header = new MessageHeader
;
1575 $mime_header->type0
= 'text';
1576 $mime_header->type1
= 'plain';
1577 if ($special_encoding) {
1578 $mime_header->encoding
= $special_encoding;
1580 $mime_header->encoding
= '8bit';
1582 if ($default_charset) {
1583 $mime_header->parameters
['charset'] = $default_charset;
1585 $message_body->mime_header
= $mime_header;
1586 array_unshift($composeMessage->entities
, $message_body);
1587 $content_type = new ContentType('multipart/mixed');
1589 $content_type = new ContentType('text/plain');
1590 if ($special_encoding) {
1591 $rfc822_header->encoding
= $special_encoding;
1593 $rfc822_header->encoding
= '8bit';
1595 if ($default_charset) {
1596 $content_type->properties
['charset']=$default_charset;
1600 $rfc822_header->content_type
= $content_type;
1601 $composeMessage->rfc822_header
= $rfc822_header;
1603 /* Here you can modify the message structure just before we hand
1604 it over to deliver */
1605 $hookReturn = do_hook('compose_send', $composeMessage);
1606 /* Get any changes made by plugins to $composeMessage. */
1607 if ( is_object($hookReturn[1]) ) {
1608 $composeMessage = $hookReturn[1];
1611 if (!$useSendmail && !$draft) {
1612 require_once(SM_PATH
. 'class/deliver/Deliver_SMTP.class.php');
1613 $deliver = new Deliver_SMTP();
1614 global $smtpServerAddress, $smtpPort, $pop_before_smtp;
1616 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ?
true : false;
1617 get_smtp_user($user, $pass);
1618 $stream = $deliver->initStream($composeMessage,$domain,0,
1619 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
1620 } elseif (!$draft) {
1621 require_once(SM_PATH
. 'class/deliver/Deliver_SendMail.class.php');
1622 global $sendmail_path, $sendmail_args;
1623 $deliver = new Deliver_SendMail(array('sendmail_args'=>$sendmail_args));
1624 $stream = $deliver->initStream($composeMessage,$sendmail_path);
1626 global $draft_folder;
1627 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1628 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1630 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
1631 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1632 $imap_deliver = new Deliver_IMAP();
1633 $length = $imap_deliver->mail($composeMessage);
1634 sqimap_append ($imap_stream, $draft_folder, $length);
1635 $imap_deliver->mail($composeMessage, $imap_stream);
1636 sqimap_append_done ($imap_stream, $draft_folder);
1637 sqimap_logout($imap_stream);
1638 unset ($imap_deliver);
1639 $composeMessage->purgeAttachments();
1642 $msg = '<br />'.sprintf(_("Error: Draft folder %s does not exist."), htmlspecialchars($draft_folder));
1643 plain_error_message($msg, $color);
1649 $length = $deliver->mail($composeMessage, $stream);
1650 $success = $deliver->finalizeStream($stream);
1653 // $deliver->dlv_server_msg is not always server's reply
1654 $msg = $deliver->dlv_msg
;
1655 if (!empty($deliver->dlv_server_msg
)) {
1656 // add 'server replied' part only when it is not empty.
1657 // Delivery error can be generated by delivery class itself
1659 _("Server replied:") . ' ' . $deliver->dlv_ret_nr
. ' ' .
1660 nl2br(htmlspecialchars($deliver->dlv_server_msg
));
1662 plain_error_message($msg, $color);
1665 $move_to_sent = getPref($data_dir,$username,'move_to_sent');
1666 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
1668 /* Move to sent code */
1669 if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
1670 $svr_allow_sent = true;
1672 $svr_allow_sent = false;
1675 if (isset($sent_folder) && (($sent_folder != '') ||
($sent_folder != 'none'))
1676 && sqimap_mailbox_exists( $imap_stream, $sent_folder)) {
1682 if ((isset($move_to_sent) && ($move_to_sent != 0)) ||
(!isset($move_to_sent))) {
1683 $lcl_allow_sent = true;
1685 $lcl_allow_sent = false;
1688 if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) ||
($fld_sent && $lcl_allow_sent)) {
1689 global $passed_id, $mailbox, $action;
1690 if ($action == 'reply' ||
$action == 'reply_all') {
1691 $save_reply_with_orig=getPref($data_dir,$username,'save_reply_with_orig');
1692 if ($save_reply_with_orig) {
1693 $sent_folder = $mailbox;
1696 sqimap_append ($imap_stream, $sent_folder, $length);
1697 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1698 $imap_deliver = new Deliver_IMAP();
1699 $imap_deliver->mail($composeMessage, $imap_stream);
1700 sqimap_append_done ($imap_stream, $sent_folder);
1701 unset ($imap_deliver);
1704 global $passed_id, $mailbox, $action, $what, $iAccount,$startMessage;
1706 $composeMessage->purgeAttachments();
1707 if ($action == 'reply' ||
$action == 'reply_all') {
1708 require(SM_PATH
. 'functions/mailbox_display.php');
1709 $aMailbox = sqm_api_mailbox_select($imap_stream, $iAccount, $mailbox,array('setindex' => $what, 'offset' => $startMessage),array());
1710 // check if we are allowed to set the \\Answered flag
1711 if (in_array('\\answered',$aMailbox['PERMANENTFLAGS'], true)) {
1712 $aUpdatedMsgs = sqimap_toggle_flag($imap_stream, array($passed_id), '\\Answered', true, false);
1713 if (isset($aUpdatedMsgs[$passed_id]['FLAGS'])) {
1715 * Only update the cached headers if the header is
1718 if (isset($aMailbox['MSG_HEADERS'][$passed_id])) {
1719 $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'] = $aMsg['FLAGS'];
1724 * Write mailbox with updated seen flag information back to cache.
1726 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
1727 sqsession_register($mailbox_cache,'mailbox_cache');
1729 sqimap_logout($imap_stream);