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 ------------------------------------- */
38 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
40 sqgetGlobalVar('composesession', $composesession, SQ_SESSION
);
41 sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION
);
42 sqgetGlobalVar('delayed_errors', $delayed_errors, SQ_SESSION
);
43 if (is_array($delayed_errors)) {
44 $oErrorHandler->AssignDelayedErrors($delayed_errors);
45 sqsession_unregister("delayed_errors");
48 /** SESSION/POST/GET VARS */
49 sqgetGlobalVar('session',$session);
50 sqgetGlobalVar('mailbox',$mailbox);
51 if(!sqgetGlobalVar('identity',$identity)) {
54 sqgetGlobalVar('send_to',$send_to);
55 sqgetGlobalVar('send_to_cc',$send_to_cc);
56 sqgetGlobalVar('send_to_bcc',$send_to_bcc);
57 sqgetGlobalVar('subject',$subject);
58 sqgetGlobalVar('body',$body);
59 sqgetGlobalVar('mailprio',$mailprio);
60 sqgetGlobalVar('request_mdn',$request_mdn);
61 sqgetGlobalVar('request_dr',$request_dr);
62 sqgetGlobalVar('html_addr_search',$html_addr_search);
63 sqgetGlobalVar('mail_sent',$mail_sent);
64 sqgetGlobalVar('passed_id',$passed_id);
65 sqgetGlobalVar('passed_ent_id',$passed_ent_id);
66 sqgetGlobalVar('send',$send);
68 sqgetGlobalVar('attach',$attach);
70 sqgetGlobalVar('draft',$draft);
71 sqgetGlobalVar('draft_id',$draft_id);
72 sqgetGlobalVar('ent_num',$ent_num);
73 sqgetGlobalVar('saved_draft',$saved_draft);
74 sqgetGlobalVar('delete_draft',$delete_draft);
75 if ( sqgetGlobalVar('startMessage',$startMessage) ) {
76 $startMessage = (int)$startMessage;
83 sqgetGlobalVar('sigappend', $sigappend, SQ_POST
);
84 sqgetGlobalVar('from_htmladdr_search', $from_htmladdr_search, SQ_POST
);
85 sqgetGlobalVar('addr_search_done', $html_addr_search_done, SQ_POST
);
86 sqgetGlobalVar('addr_search_cancel', $html_addr_search_cancel, SQ_POST
);
87 sqgetGlobalVar('send_to_search', $send_to_search, SQ_POST
);
88 sqgetGlobalVar('do_delete', $do_delete, SQ_POST
);
89 sqgetGlobalVar('delete', $delete, SQ_POST
);
90 sqgetGlobalVar('restoremessages', $restoremessages, SQ_POST
);
91 if ( sqgetGlobalVar('return', $temp, SQ_POST
) ) {
92 $html_addr_search_done = 'Use Addresses';
96 sqgetGlobalVar('attachedmessages', $attachedmessages, SQ_GET
);
97 if ( sqgetGlobalVar('account', $temp, SQ_GET
) ) {
98 $iAccount = (int) $temp;
105 if ( !sqgetGlobalVar('smaction',$action) )
107 if ( sqgetGlobalVar('smaction_reply',$tmp) ) $action = 'reply';
108 if ( sqgetGlobalVar('smaction_reply_all',$tmp) ) $action = 'reply_all';
109 if ( sqgetGlobalVar('smaction_forward',$tmp) ) $action = 'forward';
110 if ( sqgetGlobalVar('smaction_attache',$tmp) ) $action = 'forward_as_attachment';
111 if ( sqgetGlobalVar('smaction_draft',$tmp) ) $action = 'draft';
112 if ( sqgetGlobalVar('smaction_edit_new',$tmp) ) $action = 'edit_as_new';
115 /* Location (For HTTP 1.1 Header("Location: ...") redirects) */
116 $location = get_location();
117 /* Identities (fetch only once) */
118 $idents = get_identities();
120 /* --------------------- Specific Functions ------------------------------ */
122 function replyAllString($header) {
123 global $include_self_reply_all, $idents;
126 * 1) Remove the addresses we'll be sending the message 'to'
128 if (isset($header->replyto
)) {
129 $excl_ar = $header->getAddr_a('replyto');
132 * 2) Remove our identities from the CC list (they still can be in the
133 * TO list) only if $include_self_reply_all is turned off
135 if (!$include_self_reply_all) {
136 foreach($idents as $id) {
137 $excl_ar[strtolower(trim($id['email_address']))] = '';
142 * 3) get the addresses.
144 $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar);
147 * 4) generate the string.
149 $url_replytoallcc = '';
150 foreach( $url_replytoall_ar as $email => $personal) {
152 // if personal name contains address separator then surround
153 // the personal name with double quotes.
154 if (strpos($personal,',') !== false) {
155 $personal = '"'.$personal.'"';
157 $url_replytoallcc .= ", $personal <$email>";
159 $url_replytoallcc .= ', '. $email;
162 $url_replytoallcc = substr($url_replytoallcc,2);
164 return $url_replytoallcc;
168 * creates top line in reply citations
170 * Line style depends on user preferences.
171 * $orig_date argument is available only from 1.4.3 and 1.5.1 version.
172 * @param object $orig_from From: header object.
173 * @param integer $orig_date email's timestamp
174 * @return string reply citation
176 function getReplyCitation($orig_from, $orig_date) {
177 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
179 if (!is_object($orig_from)) {
182 $sOrig_from = decodeHeader($orig_from->getAddress(false),false,false,true);
185 /* First, return an empty string when no citation style selected. */
186 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
190 /* Make sure our final value isn't an empty string. */
191 if ($sOrig_from == '') {
195 /* Otherwise, try to select the desired citation style. */
196 switch ($reply_citation_style) {
199 * To translators: %s is for author's name
201 $full_reply_citation = sprintf(_("%s wrote:"),$sOrig_from);
204 $start = '<quote who="';
206 $full_reply_citation = $start . $sOrig_from . $end;
208 case 'date_time_author':
211 * first %s is for date string, second %s is for author's name. Date uses
212 * formating from "D, F j, Y g:i a" and "D, F j, Y H:i" translations.
214 * "On Sat, December 24, 2004 23:59, Santa wrote:"
215 * If you have to put author's name in front of date string, check comments about
216 * argument swapping at http://www.php.net/sprintf
218 $full_reply_citation = sprintf(_("On %s, %s wrote:"), getLongDateString($orig_date), $sOrig_from);
221 $start = $reply_citation_start .
222 ($reply_citation_start == '' ?
'' : ' ');
223 $end = $reply_citation_end;
224 $full_reply_citation = $start . $sOrig_from . $end;
230 /* Add line feed and return the citation string. */
231 return ($full_reply_citation . "\n");
235 * Creates header fields in forwarded email body
237 * $default_charset global must be set correctly before you call this function.
238 * @param object $orig_header
241 function getforwardHeader($orig_header) {
242 global $editor_size, $default_charset;
244 // using own strlen function in order to detect correct string length
245 $display = array( _("Subject") => sq_strlen(_("Subject"),$default_charset),
246 _("From") => sq_strlen(_("From"),$default_charset),
247 _("Date") => sq_strlen(_("Date"),$default_charset),
248 _("To") => sq_strlen(_("To"),$default_charset),
249 _("Cc") => sq_strlen(_("Cc"),$default_charset) );
250 $maxsize = max($display);
251 $indent = str_pad('',$maxsize+
2);
252 foreach($display as $key => $val) {
253 $display[$key] = $key .': '. str_pad('', $maxsize - $val);
255 $from = decodeHeader($orig_header->getAddr_s('from',"\n$indent"),false,false,true);
256 $from = str_replace(' ',' ',$from);
257 $to = decodeHeader($orig_header->getAddr_s('to',"\n$indent"),false,false,true);
258 $to = str_replace(' ',' ',$to);
259 $subject = decodeHeader($orig_header->subject
,false,false,true);
260 $subject = str_replace(' ',' ',$subject);
262 // using own str_pad function in order to create correct string pad
263 $bodyTop = sq_str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH
,$default_charset) .
264 "\n". $display[_("Subject")] . $subject . "\n" .
265 $display[_("From")] . $from . "\n" .
266 $display[_("Date")] . getLongDateString( $orig_header->date
). "\n" .
267 $display[_("To")] . $to . "\n";
268 if ($orig_header->cc
!= array() && $orig_header->cc
!='') {
269 $cc = decodeHeader($orig_header->getAddr_s('cc',"\n$indent"),false,false,true);
270 $cc = str_replace(' ',' ',$cc);
271 $bodyTop .= $display[_("Cc")] .$cc . "\n";
273 $bodyTop .= str_pad('', $editor_size -2 , '-') .
277 /* ----------------------------------------------------------------------- */
280 * If the session is expired during a post this restores the compose session
283 if (sqsession_is_registered('session_expired_post')) {
284 sqgetGlobalVar('session_expired_post', $session_expired_post, SQ_SESSION
);
286 * extra check for username so we don't display previous post data from
287 * another user during this session.
289 if ($session_expired_post['username'] != $username) {
290 unset($session_expired_post);
291 sqsession_unregister('session_expired_post');
292 session_write_close();
294 // these are the vars that we can set from the expired composed session
295 $compo_var_list = array ( 'send_to', 'send_to_cc','body','startMessage',
296 'passed_body','use_signature','signature','attachments','subject','newmail',
297 'send_to_bcc', 'passed_id', 'mailbox', 'from_htmladdr_search', 'identity',
298 'draft_id', 'delete_draft', 'mailprio', 'edit_as_new', 'compose_messsages',
299 'composesession', 'request_mdn', 'request_dr');
301 foreach ($compo_var_list as $var) {
302 if ( isset($session_expired_post[$var]) && !isset($
$var) ) {
303 $
$var = $session_expired_post[$var];
307 $compose_messages = unserialize(urldecode($restoremessages));
308 sqsession_register($compose_messages,'compose_messages');
309 sqsession_register($composesession,'composesession');
313 $session_expired = true;
315 unset($session_expired_post);
316 sqsession_unregister('session_expired_post');
317 session_write_close();
318 if (!isset($mailbox)) {
321 if ($compose_new_win == '1') {
322 compose_Header($color, $mailbox);
324 $sHeaderJs = (isset($sHeaderJs)) ?
$sHeaderJs : '';
325 if (strpos($action, 'reply') !== false && $reply_focus) {
326 $sBodyTagJs = 'onload="checkForm(\''.$replyfocus.'\');"';
328 $sBodyTagJs = 'onload="checkForm();"';
330 displayPageHeader($color, $mailbox,$sHeaderJs,$sBodyTagJs);
332 showInputForm($session, false);
335 if (!isset($composesession)) {
337 sqsession_register(0,'composesession');
340 if (!isset($session) ||
(isset($newmessage) && $newmessage)) {
341 sqsession_unregister('composesession');
342 $session = "$composesession" +
1;
343 $composesession = $session;
344 sqsession_register($composesession,'composesession');
346 if (!isset($compose_messages)) {
347 $compose_messages = array();
350 if (!isset($compose_messages[$session]) ||
($compose_messages[$session] == NULL)) {
351 $composeMessage = new Message();
352 $rfc822_header = new Rfc822Header();
353 $composeMessage->rfc822_header
= $rfc822_header;
354 $composeMessage->reply_rfc822_header
= '';
355 $compose_messages[$session] = $composeMessage;
357 sqsession_register($compose_messages,'compose_messages');
359 $composeMessage=$compose_messages[$session];
362 if (!isset($mailbox) ||
$mailbox == '' ||
($mailbox == 'None')) {
368 * Set $default_charset to correspond with the user's selection
369 * of language interface.
372 $composeMessage=$compose_messages[$session];
373 if (! deliverMessage($composeMessage, true)) {
374 showInputForm($session);
377 unset($compose_messages[$session]);
378 $draft_message = _("Draft Email Saved");
379 /* If this is a resumed draft, then delete the original */
380 if(isset($delete_draft)) {
381 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, false);
382 sqimap_mailbox_select($imap_stream, $draft_folder);
383 // force bypass_trash=true because message should be saved when deliverMessage() returns true.
384 // in current implementation of sqimap_msgs_list_flag() single message id can
385 // be submitted as string. docs state that it should be array.
386 sqimap_msgs_list_delete($imap_stream, $draft_folder, $delete_draft, true);
388 sqimap_mailbox_expunge($imap_stream, $draft_folder, true);
390 sqimap_logout($imap_stream);
392 if (count($oErrorHandler->aErrors
)) {
393 sqsession_register($oErrorHandler->aErrors
,"delayed_errors");
395 session_write_close();
396 if ($compose_new_win == '1') {
397 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
398 Header("Location: $location/compose.php?saved_draft=yes&session=$composesession");
400 echo ' <br><br><div style="text-align: center;"><a href="' . $location
401 . '/compose.php?saved_sent=yes&session=' . $composesession . '">'
402 . _("Return") . '</a></div>';
406 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
407 Header("Location: $location/right_main.php?mailbox=" . urlencode($draft_folder) .
408 "&startMessage=1¬e=".urlencode($draft_message));
410 echo ' <br><br><div style="text-align: center;"><a href="' . $location
411 . '/right_main.php?mailbox=' . urlencode($draft_folder)
412 . '&startMessage=1&note=' . urlencode($draft_message) .'">'
413 . _("Return") . '</a></div>';
421 if (isset($_FILES['attachfile']) &&
422 $_FILES['attachfile']['tmp_name'] &&
423 $_FILES['attachfile']['tmp_name'] != 'none') {
424 $AttachFailure = saveAttachedFiles($session);
426 if (checkInput(false) && !isset($AttachFailure)) {
427 if ($mailbox == "All Folders") {
428 /* We entered compose via the search results page */
429 $mailbox = 'INBOX'; /* Send 'em to INBOX, that's safe enough */
431 $urlMailbox = urlencode (trim($mailbox));
432 if (! isset($passed_id)) {
436 * Set $default_charset to correspond with the user's selection
437 * of language interface.
441 * This is to change all newlines to \n
442 * We'll change them to \r\n later (in the sendMessage function)
444 $body = str_replace("\r\n", "\n", $body);
445 $body = str_replace("\r", "\n", $body);
448 * Rewrap $body so that no line is bigger than $editor_size
450 $body = explode("\n", $body);
452 foreach ($body as $line) {
453 if( $line <> '-- ' ) {
454 $line = rtrim($line);
456 if (sq_strlen($line,$default_charset) <= $editor_size +
1) {
457 $newBody .= $line . "\n";
459 sqWordWrap($line, $editor_size,$default_charset);
460 $newBody .= $line . "\n";
467 $composeMessage=$compose_messages[$session];
469 $Result = deliverMessage($composeMessage);
471 do_hook('compose_send_after', $Result, $composeMessage);
473 showInputForm($session);
476 unset($compose_messages[$session]);
478 /* if it is resumed draft, delete draft message */
479 if ( isset($delete_draft)) {
480 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, false);
481 sqimap_mailbox_select($imap_stream, $draft_folder);
482 // bypass_trash=true because message should be saved when deliverMessage() returns true.
483 // in current implementation of sqimap_msgs_list_flag() single message id can
484 // be submitted as string. docs state that it should be array.
485 sqimap_msgs_list_delete($imap_stream, $draft_folder, $delete_draft, true);
487 sqimap_mailbox_expunge($imap_stream, $draft_folder, true);
489 sqimap_logout($imap_stream);
492 * Store the error array in the session because they will be lost on a redirect
494 if (count($oErrorHandler->aErrors
)) {
495 sqsession_register($oErrorHandler->aErrors
,"delayed_errors");
497 session_write_close();
498 if ($compose_new_win == '1') {
499 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
500 Header("Location: $location/compose.php?mail_sent=yes");
502 echo ' <br><br><div style="text-align: center;"><a href="' . $location
503 . '/compose.php?mail_sent=yes">'
504 . _("Return") . '</a></div>';
508 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
509 Header("Location: $location/right_main.php?mailbox=$urlMailbox".
510 "&startMessage=$startMessage&mail_sent=yes");
512 echo ' <br><br><div style="text-align: center;"><a href="' . $location
513 . "/right_main.php?mailbox=$urlMailbox"
514 . "&startMessage=$startMessage&mail_sent=yes\">"
515 . _("Return") . '</a></div>';
520 if ($compose_new_win == '1') {
521 compose_Header($color, $mailbox);
524 displayPageHeader($color, $mailbox);
526 if (isset($AttachFailure)) {
527 plain_error_message(_("Could not move/copy file. File not attached"),
531 showInputForm($session);
532 /* sqimap_logout($imapConnection); */
534 } elseif (isset($html_addr_search_done)) {
535 if ($compose_new_win == '1') {
536 compose_Header($color, $mailbox);
539 displayPageHeader($color, $mailbox);
542 if (isset($send_to_search) && is_array($send_to_search)) {
543 foreach ($send_to_search as $k => $v) {
544 if (substr($k, 0, 1) == 'T') {
550 elseif (substr($k, 0, 1) == 'C') {
556 elseif (substr($k, 0, 1) == 'B') {
558 $send_to_bcc .= ', ';
564 showInputForm($session);
565 } elseif (isset($html_addr_search) && !isset($html_addr_search_cancel)) {
566 if (isset($_FILES['attachfile']) &&
567 $_FILES['attachfile']['tmp_name'] &&
568 $_FILES['attachfile']['tmp_name'] != 'none') {
569 if(saveAttachedFiles($session)) {
570 plain_error_message(_("Could not move/copy file. File not attached"));
574 * I am using an include so as to elminiate an extra unnecessary
575 * click. If you can think of a better way, please implement it.
577 include_once('./addrbook_search_html.php');
578 } elseif (isset($attach)) {
579 if ($compose_new_win == '1') {
580 compose_Header($color, $mailbox);
582 displayPageHeader($color, $mailbox);
584 if (saveAttachedFiles($session)) {
585 plain_error_message(_("Could not move/copy file. File not attached"));
587 showInputForm($session);
589 elseif (isset($sigappend)) {
590 $signature = $idents[$identity]['signature'];
592 $body .= "\n\n".($prefix_sig==true?
"-- \n":'').$signature;
593 if ($compose_new_win == '1') {
594 compose_Header($color, $mailbox);
596 displayPageHeader($color, $mailbox);
598 showInputForm($session);
599 } elseif (isset($do_delete)) {
600 if ($compose_new_win == '1') {
601 compose_Header($color, $mailbox);
603 displayPageHeader($color, $mailbox);
606 if (isset($delete) && is_array($delete)) {
607 $composeMessage = $compose_messages[$session];
608 foreach($delete as $index) {
609 if (!empty($composeMessage->entities
) && isset($composeMessage->entities
[$index])) {
610 $composeMessage->entities
[$index]->purgeAttachments();
611 unset ($composeMessage->entities
[$index]);
614 $new_entities = array();
615 foreach ($composeMessage->entities
as $entity) {
616 $new_entities[] = $entity;
618 $composeMessage->entities
= $new_entities;
619 $compose_messages[$session] = $composeMessage;
620 sqsession_register($compose_messages, 'compose_messages');
622 showInputForm($session);
625 * This handles the default case as well as the error case
626 * (they had the same code) --> if (isset($smtpErrors))
629 if ($compose_new_win == '1') {
630 compose_Header($color, $mailbox);
632 displayPageHeader($color, $mailbox);
637 if (!isset($passed_ent_id)) {
640 if (!isset($passed_id)) {
643 if (!isset($mailbox)) {
646 if (!isset($action)) {
650 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
652 /* in case the origin is not read_body.php */
653 if (isset($send_to)) {
654 $values['send_to'] = $send_to;
656 if (isset($send_to_cc)) {
657 $values['send_to_cc'] = $send_to_cc;
659 if (isset($send_to_bcc)) {
660 $values['send_to_bcc'] = $send_to_bcc;
662 if (isset($subject)) {
663 $values['subject'] = $subject;
665 showInputForm($session, $values);
670 /**************** Only function definitions go below *************/
672 function getforwardSubject($subject)
674 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
675 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
676 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
677 $subject = '[Fwd: ' . $subject . ']';
682 /* This function is used when not sending or adding attachments */
683 function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
684 global $editor_size, $default_use_priority, $body, $idents,
685 $use_signature, $data_dir, $username,
686 $key, $imapServerAddress, $imapPort, $compose_messages,
687 $composeMessage, $body_quote;
688 global $languages, $squirrelmail_language, $default_charset;
691 * Set $default_charset to correspond with the user's selection
692 * of language interface. $default_charset global is not correct,
693 * if message is composed in new window.
697 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
701 $imapConnection = sqimap_login($username, false, $imapServerAddress,
704 sqimap_mailbox_select($imapConnection, $mailbox);
705 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
708 if ($passed_ent_id) {
709 /* redefine the messsage in case of message/rfc822 */
710 $message = $message->getEntity($passed_ent_id);
711 /* message is an entity which contains the envelope and type0=message
712 * and type1=rfc822. The actual entities are childs from
713 * $message->entities[0]. That's where the encoding and is located
716 $entities = $message->entities
[0]->findDisplayEntity
717 (array(), $alt_order = array('text/plain'));
718 if (!count($entities)) {
719 $entities = $message->entities
[0]->findDisplayEntity
720 (array(), $alt_order = array('text/plain','html/plain'));
722 $orig_header = $message->rfc822_header
; /* here is the envelope located */
723 /* redefine the message for picking up the attachments */
724 $message = $message->entities
[0];
727 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
728 if (!count($entities)) {
729 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
731 $orig_header = $message->rfc822_header
;
734 $type0 = $message->type0
;
735 $type1 = $message->type1
;
736 foreach ($entities as $ent) {
737 $msg = $message->getEntity($ent);
738 $type0 = $msg->type0
;
739 $type1 = $msg->type1
;
740 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
741 $body_part_entity = $message->getEntity($ent);
742 $bodypart = decodeBody($unencoded_bodypart,
743 $body_part_entity->header
->encoding
);
744 if ($type1 == 'html') {
745 $bodypart = str_replace("\n", ' ', $bodypart);
746 $bodypart = preg_replace(array('/<\/?p>/i','/<div><\/div>/i','/<br\s*(\/)*>/i','/<\/?div>/i'), "\n", $bodypart);
747 $bodypart = str_replace(array(' ','>','<'),array(' ','>','<'),$bodypart);
748 $bodypart = strip_tags($bodypart);
750 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
751 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
752 if (mb_detect_encoding($bodypart) != 'ASCII') {
753 $bodypart = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $bodypart);
757 // charset encoding in compose form stuff
758 if (isset($body_part_entity->header
->parameters
['charset'])) {
759 $actual = $body_part_entity->header
->parameters
['charset'];
761 $actual = 'us-ascii';
764 if ( $actual && is_conversion_safe($actual) && $actual != $default_charset){
765 $bodypart = charset_convert($actual,$bodypart,$default_charset,false);
767 // end of charset encoding in compose
771 if ($default_use_priority) {
772 $mailprio = substr($orig_header->priority
,0,1);
781 $from_o = $orig_header->from
;
782 if (is_array($from_o)) {
783 if (isset($from_o[0])) {
784 $from_o = $from_o[0];
787 if (is_object($from_o)) {
788 $orig_from = $from_o->getAddress();
793 $identities = array();
794 if (count($idents) > 1) {
795 foreach($idents as $nr=>$data) {
796 $enc_from_name = '"'.$data['full_name'].'" <'. $data['email_address'].'>';
797 if($enc_from_name == $orig_from) {
801 $identities[] = $enc_from_name;
804 $identity_match = $orig_header->findAddress($identities);
805 if ($identity_match) {
806 $identity = $identity_match;
812 $use_signature = FALSE;
813 $composeMessage->rfc822_header
= $orig_header;
814 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
815 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
816 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
817 $send_from = $orig_header->getAddr_s('from');
818 $send_from_parts = new AddressStructure();
819 $send_from_parts = $orig_header->parseAddress($send_from);
820 $send_from_add = $send_from_parts->mailbox
. '@' . $send_from_parts->host
;
821 $identities = get_identities();
822 if (count($identities) > 0) {
823 foreach($identities as $iddata) {
824 if ($send_from_add == $iddata['email_address']) {
825 $identity = $iddata['index'];
830 $subject = decodeHeader($orig_header->subject
,false,false,true);
831 /* remember the references and in-reply-to headers in case of an reply */
832 $composeMessage->rfc822_header
->more_headers
['References'] = $orig_header->references
;
833 $composeMessage->rfc822_header
->more_headers
['In-Reply-To'] = $orig_header->in_reply_to
;
834 // rewrap the body to clean up quotations and line lengths
835 sqBodyWrap($body, $editor_size);
836 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
838 case ('edit_as_new'):
839 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
840 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
841 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
842 $subject = decodeHeader($orig_header->subject
,false,false,true);
843 $mailprio = $orig_header->priority
;
845 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
846 // rewrap the body to clean up quotations and line lengths
847 sqBodyWrap($body, $editor_size);
851 $subject = getforwardSubject(decodeHeader($orig_header->subject
,false,false,true));
852 $body = getforwardHeader($orig_header) . $body;
853 // the logic for calling sqUnWordWrap here would be to allow the browser to wrap the lines
854 // forwarded message text should be as undisturbed as possible, so commenting out this call
855 // sqUnWordWrap($body);
856 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
858 //add a blank line after the forward headers
859 $body = "\n" . $body;
861 case ('forward_as_attachment'):
862 $subject = getforwardSubject(decodeHeader($orig_header->subject
,false,false,true));
863 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
867 if(isset($orig_header->mail_followup_to
) && $orig_header->mail_followup_to
) {
868 $send_to = $orig_header->getAddr_s('mail_followup_to');
870 $send_to_cc = replyAllString($orig_header);
871 $send_to_cc = decodeHeader($send_to_cc,false,false,true);
874 // skip this if send_to was already set right above here
876 $send_to = $orig_header->reply_to
;
877 if (is_array($send_to) && count($send_to)) {
878 $send_to = $orig_header->getAddr_s('reply_to');
879 } else if (is_object($send_to)) { /* unneccesarry, just for failsafe purpose */
880 $send_to = $orig_header->getAddr_s('reply_to');
882 $send_to = $orig_header->getAddr_s('from');
885 $send_to = decodeHeader($send_to,false,false,true);
886 $subject = decodeHeader($orig_header->subject
,false,false,true);
887 $subject = str_replace('"', "'", $subject);
888 $subject = trim($subject);
889 if (substr(strtolower($subject), 0, 3) != 're:') {
890 $subject = 'Re: ' . $subject;
892 /* this corrects some wrapping/quoting problems on replies */
893 $rewrap_body = explode("\n", $body);
894 $from = (is_array($orig_header->from
)) ?
$orig_header->from
[0] : $orig_header->from
;
896 $strip_sigs = getPref($data_dir, $username, 'strip_sigs');
897 foreach ($rewrap_body as $line) {
898 if ($strip_sigs && substr($line,0,3) == '-- ') {
901 if (preg_match("/^(>+)/", $line, $matches)) {
903 $body .= $body_quote . str_replace("\n", "\n$body_quote$gt ", rtrim($line)) ."\n";
905 $body .= $body_quote . (!empty($body_quote) ?
' ' : '') . str_replace("\n", "\n$body_quote" . (!empty($body_quote) ?
' ' : ''), rtrim($line)) . "\n";
909 //rewrap the body to clean up quotations and line lengths
910 $body = sqBodyWrap ($body, $editor_size);
912 $body = getReplyCitation($from , $orig_header->date
) . $body;
913 $composeMessage->reply_rfc822_header
= $orig_header;
919 $compose_messages[$session] = $composeMessage;
920 sqsession_register($compose_messages, 'compose_messages');
921 session_write_close();
922 sqimap_logout($imapConnection);
924 $ret = array( 'send_to' => $send_to,
925 'send_to_cc' => $send_to_cc,
926 'send_to_bcc' => $send_to_bcc,
927 'subject' => $subject,
928 'mailprio' => $mailprio,
930 'identity' => $identity );
933 } /* function newMail() */
936 * downloads attachments from original message, stores them in attachment directory and adds
937 * them to composed message.
938 * @param object $message
939 * @param object $composeMessage
940 * @param integer $passed_id
941 * @param mixed $entities
942 * @param mixed $imapConnection
945 function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
946 global $attachment_dir, $username, $data_dir, $squirrelmail_language, $languages;
947 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
948 if (!count($message->entities
) ||
949 ($message->type0
== 'message' && $message->type1
== 'rfc822')) {
950 if ( !in_array($message->entity_id
, $entities) && $message->entity_id
) {
951 switch ($message->type0
) {
953 if ($message->type1
== 'rfc822') {
954 $filename = $message->rfc822_header
->subject
;
955 if ($filename == "") {
956 $filename = "untitled-".$message->entity_id
;
960 $filename = $message->getFilename();
964 if (!$message->mime_header
) { /* temporary hack */
965 $message->mime_header
= $message->header
;
967 $filename = $message->getFilename();
970 $filename = str_replace(' ', ' ', decodeHeader($filename));
971 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
972 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode')) {
973 $filename = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode', $filename);
975 $localfilename = GenerateRandomString(32, '', 7);
976 $full_localfilename = "$hashed_attachment_dir/$localfilename";
977 while (file_exists($full_localfilename)) {
978 $localfilename = GenerateRandomString(32, '', 7);
979 $full_localfilename = "$hashed_attachment_dir/$localfilename";
981 $message->att_local_name
= $full_localfilename;
983 $composeMessage->initAttachment($message->type0
.'/'.$message->type1
,$filename,
984 $full_localfilename);
986 /* Write Attachment to file */
987 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
988 mime_print_body_lines ($imapConnection, $passed_id, $message->entity_id
, $message->header
->encoding
, $fp);
992 for ($i=0, $entCount=count($message->entities
); $i<$entCount;$i++
) {
993 $composeMessage=getAttachments($message->entities
[$i], $composeMessage, $passed_id, $entities, $imapConnection);
996 return $composeMessage;
999 function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
1000 $passed_ent_id='', $imapConnection) {
1001 global $attachment_dir, $username, $data_dir;
1002 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1003 if (!$passed_ent_id) {
1004 $body_a = sqimap_run_command($imapConnection,
1005 'FETCH '.$passed_id.' RFC822',
1006 TRUE, $response, $readmessage,
1009 $body_a = sqimap_run_command($imapConnection,
1010 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
1011 TRUE, $response, $readmessage, TRUE);
1012 $message = $message->parent
;
1014 if ($response == 'OK') {
1015 $subject = encodeHeader($message->rfc822_header
->subject
);
1016 array_shift($body_a);
1018 $body = implode('', $body_a) . "\r\n";
1020 $localfilename = GenerateRandomString(32, 'FILE', 7);
1021 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1023 $fp = fopen($full_localfilename, 'w');
1024 fwrite ($fp, $body);
1026 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
1027 $full_localfilename);
1029 return $composeMessage;
1032 function showInputForm ($session, $values=false) {
1033 global $send_to, $send_to_cc, $body, $startMessage, $action,
1034 $color, $use_signature, $signature, $prefix_sig,
1035 $editor_size, $editor_height, $subject, $newmail,
1036 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
1037 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
1038 $username, $data_dir, $identity, $idents, $delete_draft,
1039 $mailprio, $compose_new_win, $saved_draft, $mail_sent, $sig_first,
1040 $compose_messages, $composesession, $default_charset,
1041 $compose_onsubmit, $oTemplate;
1043 if (checkForJavascript()) {
1044 $onfocus = ' onfocus="alreadyFocused=true;"';
1045 $onfocus_array = array('onfocus' => 'alreadyFocused=true;');
1049 $onfocus_array = array();
1052 $composeMessage = $compose_messages[$session];
1054 $send_to = $values['send_to'];
1055 $send_to_cc = $values['send_to_cc'];
1056 $send_to_bcc = $values['send_to_bcc'];
1057 $subject = $values['subject'];
1058 $mailprio = $values['mailprio'];
1059 $body = $values['body'];
1060 $identity = (int) $values['identity'];
1062 $send_to = decodeHeader($send_to, true, false);
1063 $send_to_cc = decodeHeader($send_to_cc, true, false);
1064 $send_to_bcc = decodeHeader($send_to_bcc, true, false);
1067 if ($use_javascript_addr_book) {
1068 echo "\n". '<script type="text/javascript">'."\n<!--\n" .
1069 'function open_abook() { ' . "\n" .
1070 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
1071 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
1072 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
1073 ' nwin.opener = document.windows;' . "\n" .
1075 "// -->\n</script>\n\n";
1078 echo "\n" . '<form name="compose" action="compose.php" method="post" ' .
1079 'enctype="multipart/form-data"';
1081 $compose_onsubmit = array();
1082 do_hook('compose_form');
1084 // Plugins that use compose_form hook can add an array entry
1085 // to the globally scoped $compose_onsubmit; we add them up
1086 // here and format the form tag's full onsubmit handler.
1087 // Each plugin should use "return false" if they need to
1088 // stop form submission but otherwise should NOT use "return
1089 // true" to give other plugins the chance to do what they need
1090 // to do; SquirrelMail itself will add the final "return true".
1091 // Onsubmit text is enclosed inside of double quotes, so plugins
1092 // need to quote accordingly.
1093 if (checkForJavascript()) {
1094 $onsubmit_text = ' onsubmit="';
1095 if (empty($compose_onsubmit))
1096 $compose_onsubmit = array();
1097 else if (!is_array($compose_onsubmit))
1098 $compose_onsubmit = array($compose_onsubmit);
1100 foreach ($compose_onsubmit as $text) {
1101 $text = trim($text);
1102 if (substr($text, -1) != ';' && substr($text, -1) != '}')
1104 $onsubmit_text .= $text;
1107 echo $onsubmit_text . ' return true;"';
1113 echo addHidden('startMessage', $startMessage);
1115 if ($action == 'draft') {
1116 echo addHidden('delete_draft', $passed_id);
1118 if (isset($delete_draft)) {
1119 echo addHidden('delete_draft', $delete_draft);
1121 if (isset($session)) {
1122 echo addHidden('session', $session);
1125 if (isset($passed_id)) {
1126 echo addHidden('passed_id', $passed_id);
1129 if ($saved_draft == 'yes') {
1130 echo '<br /><div style="text-align: center;"><b>'. _("Draft Saved").'</div></b>';
1132 if ($mail_sent == 'yes') {
1133 echo '<br /><div style="text-align: center;"><b>'. _("Your Message has been sent.").'</div></b>';
1135 if ($compose_new_win == '1') {
1136 echo '<table align="center" bgcolor="'.$color[0].'" width="100%" border="0">'."\n" .
1137 ' <tr><td></td>'.html_tag( 'td', '', 'right' ).
1138 '<input type="button" name="Close" onclick="return self.close()" value="'.
1139 _("Close").'" /></td></tr>'."\n";
1141 echo '<table align="center" cellspacing="0" border="0">' . "\n";
1143 if ($location_of_buttons == 'top') {
1144 showComposeButtonRow();
1147 /* display select list for identities */
1148 if (count($idents) > 1) {
1149 $ident_list = array();
1150 foreach($idents as $id => $data) {
1152 $data['full_name'].' <'.$data['email_address'].'>';
1154 echo ' <tr>' . "\n" .
1155 html_tag( 'td', '', 'right', $color[4], 'width="10%"' ) .
1156 '<label for="identity">' . _("From:") . '</label></td>' . "\n" .
1157 html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
1159 addSelect('identity', $ident_list, $identity, TRUE);
1161 echo ' </td>' . "\n" .
1165 echo ' <tr>' . "\n" .
1166 html_tag( 'td', '', 'right', $color[4], 'width="10%"' ) .
1167 '<label for="send_to">' . _("To") . '</label>:</td>' . "\n" .
1168 html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
1169 addInput('send_to', $send_to, 60, 0, $onfocus_array). '<br />' . "\n" .
1173 html_tag( 'td', '', 'right', $color[4] ) .
1174 '<label for="send_to_cc">' . _("Cc") . '</label>:</td>' . "\n" .
1175 html_tag( 'td', '', 'left', $color[4] ) .
1176 addInput('send_to_cc', $send_to_cc, 60, 0, $onfocus_array). '<br />' . "\n" .
1180 html_tag( 'td', '', 'right', $color[4] ) .
1181 '<label for="send_to_bcc">' . _("Bcc") . '</label>:</td>' . "\n" .
1182 html_tag( 'td', '', 'left', $color[4] ) .
1183 addInput('send_to_bcc', $send_to_bcc, 60, 0, $onfocus_array).'<br />' . "\n" .
1187 html_tag( 'td', '', 'right', $color[4] ) .
1188 '<label for="subject">' . _("Subject") . '</label>:</td>' . "\n" .
1189 html_tag( 'td', '', 'left', $color[4] ) . "\n";
1190 echo ' '.addInput('subject', $subject, 60, 0, $onfocus_array).
1194 if ($location_of_buttons == 'between') {
1195 showComposeButtonRow();
1199 * When message is compose in new window, different colors are used.
1201 if ($compose_new_win == '1') {
1202 echo ' <tr>' . "\n" .
1203 ' <td bgcolor="' . $color[0] . '" colspan="2" align="center">' . "\n" .
1204 ' <textarea name="body" id="body" rows="' . (int)$editor_height .
1205 '" cols="' . (int)$editor_size . '"' . $onfocus . '>';
1208 echo ' <tr>' . "\n" .
1209 ' <td bgcolor="' . $color[4] . '" colspan="2">' . "\n" .
1210 ' <textarea name="body" id="body" rows="' . (int)$editor_height .
1211 '" cols="' . (int)$editor_size . '"' . $onfocus . '>';
1214 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
1215 $signature = $idents[$identity]['signature'];
1217 if ($sig_first == '1') {
1219 * FIXME: test is specific to ja_JP translation implementation.
1220 * This test might apply incorrect conversion to other translations, but
1221 * use of 7bit iso-2022-jp charset in other translations might have other
1224 if ($default_charset == 'iso-2022-jp') {
1225 echo "\n\n".($prefix_sig==true?
"-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1227 echo "\n\n".($prefix_sig==true?
"-- \n":'').decodeHeader($signature,false,false);
1229 echo "\n\n".htmlspecialchars(decodeHeader($body,false,false));
1232 echo "\n\n".htmlspecialchars(decodeHeader($body,false,false));
1233 // FIXME: test is specific to ja_JP translation implementation. See above comments.
1234 if ($default_charset == 'iso-2022-jp') {
1235 echo "\n\n".($prefix_sig==true?
"-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1237 echo "\n\n".($prefix_sig==true?
"-- \n":'').decodeHeader($signature,false,false);
1241 echo htmlspecialchars(decodeHeader($body,false,false));
1243 echo '</textarea><br />' . "\n" .
1248 if ($location_of_buttons == 'bottom') {
1249 showComposeButtonRow();
1251 echo ' <tr>' . "\n" .
1252 html_tag( 'td', '', 'right', '', 'colspan="2"' ) . "\n" .
1253 ' ' . addSubmit(_("Send"), 'send').
1254 ' <br /><br />' . "\n" .
1259 /* This code is for attachments */
1260 if ((bool) ini_get('file_uploads')) {
1262 /* Calculate the max size for an uploaded file.
1263 * This is advisory for the user because we can't actually prevent
1264 * people to upload too large files. */
1266 /* php.ini vars which influence the max for uploads */
1267 $configvars = array('post_max_size', 'memory_limit', 'upload_max_filesize');
1268 foreach($configvars as $var) {
1269 /* skip 0 or empty values, and -1 which means 'unlimited' */
1270 if( $size = getByteSize(ini_get($var)) ) {
1271 if ( $size != '-1' ) {
1277 if(count($sizes) > 0) {
1278 $maxsize = '(max. ' . show_readable_size( min( $sizes ) ) . ')'
1279 . addHidden('MAX_FILE_SIZE', min( $sizes ));
1283 echo ' <tr>' . "\n" .
1284 ' <td colspan="2">' . "\n" .
1285 ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.
1286 ' border="0" bgcolor="'.$color[9].'">' . "\n" .
1289 ' <table width="100%" cellpadding="3" cellspacing="0" align="center"'.
1290 ' border="0">' . "\n" .
1292 html_tag( 'td', '', 'right', '', 'valign="middle"' ) .
1293 _("Attach:") . '</td>' . "\n" .
1294 html_tag( 'td', '', 'left', '', 'valign="middle"' ) .
1295 ' <input name="attachfile" size="48" type="file" />' . "\n" .
1296 ' <input type="submit" name="attach"' .
1297 ' value="' . _("Add") .'" />' . "\n" .
1303 if ($composeMessage->entities
) {
1304 foreach ($composeMessage->entities
as $key => $attachment) {
1305 $attached_file = $attachment->att_local_name
;
1306 if ($attachment->att_local_name ||
$attachment->body_part
) {
1307 $attached_filename = decodeHeader($attachment->mime_header
->getParameter('name'));
1308 $type = $attachment->mime_header
->type0
.'/'.
1309 $attachment->mime_header
->type1
;
1311 $s_a[] = '<table bgcolor="'.$color[0].
1312 '" border="0"><tr><td>'.
1313 addCheckBox('delete[]', FALSE, $key).
1314 "</td><td>\n" . $attached_filename .
1315 '</td><td>-</td><td> ' . $type . '</td><td>('.
1316 show_readable_size( filesize( $attached_file ) ) . ')</td></tr></table>'."\n";
1321 foreach ($s_a as $s) {
1322 echo '<tr>' . html_tag( 'td', '', 'left', $color[0], 'colspan="2"' ) . $s .'</td></tr>';
1324 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
1325 _("Delete selected attachments") . "\" />\n" .
1328 echo ' </table>' . "\n" .
1331 ' </table>' . "\n" .
1334 } // End of file_uploads if-block
1335 /* End of attachment code */
1336 echo '</table>' . "\n" .
1337 addHidden('username', $username).
1338 addHidden('smaction', $action).
1339 addHidden('mailbox', $mailbox);
1341 store the complete ComposeMessages array in a hidden input value
1342 so we can restore them in case of a session timeout.
1344 sqgetGlobalVar('QUERY_STRING', $queryString, SQ_SERVER
);
1345 echo addHidden('restoremessages', serialize($compose_messages)).
1346 addHidden('composesession', $composesession).
1347 addHidden('querystring', $queryString).
1349 if (!(bool) ini_get('file_uploads')) {
1350 /* File uploads are off, so we didn't show that part of the form.
1351 To avoid bogus bug reports, tell the user why. */
1352 echo '<p style="text-align:center">'
1353 . _("Because PHP file uploads are turned off, you can not attach files to this message. Please see your system administrator for details.")
1357 do_hook('compose_bottom');
1358 $oTemplate->display('footer.tpl');
1362 function showComposeButtonRow() {
1363 global $use_javascript_addr_book, $save_as_draft,
1364 $default_use_priority, $mailprio, $default_use_mdn,
1365 $request_mdn, $request_dr,
1366 $data_dir, $username;
1368 echo ' <tr>' . "\n" .
1369 ' <td></td>' . "\n" .
1371 if ($default_use_priority) {
1372 if(!isset($mailprio)) {
1375 echo ' <label for="mailprio">' . _("Priority") . '</label>: '.
1376 addSelect('mailprio', array(
1379 '5' => _("Low") ), $mailprio, TRUE);
1381 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1382 if ($default_use_mdn) {
1383 if ($mdn_user_support) {
1384 echo ' ' . _("Receipt") .': '.
1385 addCheckBox('request_mdn', $request_mdn == '1', '1') .
1386 '<label for="request_mdn">' . _("On Read") . '</label>' .
1387 addCheckBox('request_dr', $request_dr == '1', '1') .
1388 '<label for="request_dr">' . _("On Delivery") . '</label>';
1392 echo ' </td>' . "\n" .
1395 ' <td></td>' . "\n" .
1397 ' <input type="submit" name="sigappend" value="' . _("Signature") . '" />' . "\n";
1398 if ($use_javascript_addr_book) {
1399 echo " <script type=\"text/javascript\"><!--\n document.write(\"".
1400 " <input type=button value=\\\""._("Addresses").
1401 "\\\" onclick=\\\"javascript:open_abook();\\\" />\");".
1402 " // --></script><noscript>\n".
1403 ' <input type="submit" name="html_addr_search" value="'.
1404 _("Addresses").'" />'.
1407 echo ' <input type="submit" name="html_addr_search" value="'.
1408 _("Addresses").'" />' . "\n";
1411 if ($save_as_draft) {
1412 echo ' <input type="submit" name ="draft" value="' . _("Save Draft") . "\" />\n";
1415 echo ' <input type="submit" name="send" value="'. _("Send") . '" />' . "\n";
1416 do_hook('compose_button_row');
1418 echo ' </td>' . "\n" .
1422 function checkInput ($show) {
1424 * I implemented the $show variable because the error messages
1425 * were getting sent before the page header. So, I check once
1426 * using $show=false, and then when i'm ready to display the error
1427 * message, show=true
1429 global $send_to, $send_to_bcc;
1431 if ($send_to == '' && $send_to_bcc == '') {
1433 plain_error_message(_("You have not filled in the \"To:\" field."));
1438 } /* function checkInput() */
1441 /* True if FAILURE */
1442 function saveAttachedFiles($session) {
1443 global $_FILES, $attachment_dir, $username,
1444 $data_dir, $compose_messages;
1446 /* get out of here if no file was attached at all */
1447 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1451 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1452 $localfilename = GenerateRandomString(32, '', 7);
1453 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1454 while (file_exists($full_localfilename)) {
1455 $localfilename = GenerateRandomString(32, '', 7);
1456 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1459 // m_u_f works better with restricted PHP installs (safe_mode, open_basedir),
1460 // if that doesn't work, try a simple rename.
1461 if (!@move_uploaded_file
($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
1462 if (!@rename
($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1466 $message = $compose_messages[$session];
1467 $type = strtolower($_FILES['attachfile']['type']);
1468 $name = $_FILES['attachfile']['name'];
1469 $message->initAttachment($type, $name, $full_localfilename);
1470 $compose_messages[$session] = $message;
1471 sqsession_register($compose_messages , 'compose_messages');
1474 /* parse values like 8M and 2k into bytes */
1475 function getByteSize($ini_size) {
1481 $ini_size = trim($ini_size);
1483 // if there's some kind of letter at the end of the string we need to multiply.
1484 if(!is_numeric(substr($ini_size, -1))) {
1486 switch(strtoupper(substr($ini_size, -1))) {
1488 $bytesize = 1073741824;
1491 $bytesize = 1048576;
1498 return ($bytesize * (int)substr($ini_size, 0, -1));
1506 * temporary function to make use of the deliver class.
1507 * In the future the responsible backend should be automaticly loaded
1508 * and conf.pl should show a list of available backends.
1509 * The message also should be constructed by the message class.
1511 function deliverMessage($composeMessage, $draft=false) {
1512 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
1513 $username, $popuser, $usernamedata, $identity, $idents, $data_dir,
1514 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
1515 $domain, $action, $default_move_to_sent, $move_to_sent;
1516 global $imapServerAddress, $imapPort, $sent_folder, $key;
1518 $rfc822_header = $composeMessage->rfc822_header
;
1520 $abook = addressbook_init(false, true);
1521 $rfc822_header->to
= $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
1522 $rfc822_header->cc
= $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
1523 $rfc822_header->bcc
= $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
1524 $rfc822_header->priority
= $mailprio;
1525 $rfc822_header->subject
= $subject;
1527 $special_encoding='';
1528 if (strtolower($default_charset) == 'iso-2022-jp') {
1529 if (mb_detect_encoding($body) == 'ASCII') {
1530 $special_encoding = '8bit';
1532 $body = mb_convert_encoding($body, 'JIS');
1533 $special_encoding = '7bit';
1536 $composeMessage->setBody($body);
1538 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
1539 $popuser = $usernamedata[1];
1540 $domain = $usernamedata[2];
1541 unset($usernamedata);
1543 $popuser = $username;
1546 $from_mail = $idents[$identity]['email_address'];
1547 $full_name = $idents[$identity]['full_name'];
1548 $reply_to = $idents[$identity]['reply_to'];
1550 $from_mail = "$popuser@$domain";
1552 $rfc822_header->from
= $rfc822_header->parseAddress($from_mail,true);
1554 $from = $rfc822_header->from
[0];
1555 if (!$from->host
) $from->host
= $domain;
1556 $full_name_encoded = encodeHeader($full_name);
1557 if ($full_name_encoded != $full_name) {
1558 $from_addr = $full_name_encoded .' <'.$from->mailbox
.'@'.$from->host
.'>';
1560 $from_addr = '"'.$full_name .'" <'.$from->mailbox
.'@'.$from->host
.'>';
1562 $rfc822_header->from
= $rfc822_header->parseAddress($from_addr,true);
1565 $rfc822_header->reply_to
= $rfc822_header->parseAddress($reply_to,true);
1567 /* Receipt: On Read */
1568 if (isset($request_mdn) && $request_mdn) {
1569 $rfc822_header->dnt
= $rfc822_header->parseAddress($from_mail,true);
1571 /* Receipt: On Delivery */
1572 if (isset($request_dr) && $request_dr) {
1573 $rfc822_header->more_headers
['Return-Receipt-To'] = $from_mail;
1575 /* multipart messages */
1576 if (count($composeMessage->entities
)) {
1577 $message_body = new Message();
1578 $message_body->body_part
= $composeMessage->body_part
;
1579 $composeMessage->body_part
= '';
1580 $mime_header = new MessageHeader
;
1581 $mime_header->type0
= 'text';
1582 $mime_header->type1
= 'plain';
1583 if ($special_encoding) {
1584 $mime_header->encoding
= $special_encoding;
1586 $mime_header->encoding
= '8bit';
1588 if ($default_charset) {
1589 $mime_header->parameters
['charset'] = $default_charset;
1591 $message_body->mime_header
= $mime_header;
1592 array_unshift($composeMessage->entities
, $message_body);
1593 $content_type = new ContentType('multipart/mixed');
1595 $content_type = new ContentType('text/plain');
1596 if ($special_encoding) {
1597 $rfc822_header->encoding
= $special_encoding;
1599 $rfc822_header->encoding
= '8bit';
1601 if ($default_charset) {
1602 $content_type->properties
['charset']=$default_charset;
1606 $rfc822_header->content_type
= $content_type;
1607 $composeMessage->rfc822_header
= $rfc822_header;
1609 /* Here you can modify the message structure just before we hand
1610 it over to deliver */
1611 $hookReturn = do_hook('compose_send', $composeMessage);
1612 /* Get any changes made by plugins to $composeMessage. */
1613 if ( is_object($hookReturn[1]) ) {
1614 $composeMessage = $hookReturn[1];
1617 if (!$useSendmail && !$draft) {
1618 require_once(SM_PATH
. 'class/deliver/Deliver_SMTP.class.php');
1619 $deliver = new Deliver_SMTP();
1620 global $smtpServerAddress, $smtpPort, $pop_before_smtp;
1622 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ?
true : false;
1623 get_smtp_user($user, $pass);
1624 $stream = $deliver->initStream($composeMessage,$domain,0,
1625 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
1626 } elseif (!$draft) {
1627 require_once(SM_PATH
. 'class/deliver/Deliver_SendMail.class.php');
1628 global $sendmail_path, $sendmail_args;
1629 // Check for outdated configuration
1630 if (!isset($sendmail_args)) {
1631 if ($sendmail_path=='/var/qmail/bin/qmail-inject') {
1632 $sendmail_args = '';
1634 $sendmail_args = '-i -t';
1637 $deliver = new Deliver_SendMail(array('sendmail_args'=>$sendmail_args));
1638 $stream = $deliver->initStream($composeMessage,$sendmail_path);
1640 global $draft_folder;
1641 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1642 $imap_stream = sqimap_login($username, false, $imapServerAddress,
1644 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
1645 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1646 $imap_deliver = new Deliver_IMAP();
1647 $length = $imap_deliver->mail($composeMessage);
1648 sqimap_append ($imap_stream, $draft_folder, $length);
1649 $imap_deliver->mail($composeMessage, $imap_stream);
1650 sqimap_append_done ($imap_stream, $draft_folder);
1651 sqimap_logout($imap_stream);
1652 unset ($imap_deliver);
1653 $composeMessage->purgeAttachments();
1656 $msg = '<br />'.sprintf(_("Error: Draft folder %s does not exist."), htmlspecialchars($draft_folder));
1657 plain_error_message($msg);
1663 $length = $deliver->mail($composeMessage, $stream);
1664 $success = $deliver->finalizeStream($stream);
1667 // $deliver->dlv_server_msg is not always server's reply
1668 $msg = $deliver->dlv_msg
;
1669 if (!empty($deliver->dlv_server_msg
)) {
1670 // add 'server replied' part only when it is not empty.
1671 // Delivery error can be generated by delivery class itself
1673 _("Server replied:") . ' ' . $deliver->dlv_ret_nr
. ' ' .
1674 nl2br(htmlspecialchars($deliver->dlv_server_msg
));
1676 plain_error_message($msg);
1679 $move_to_sent = getPref($data_dir,$username,'move_to_sent');
1680 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
1682 /* Move to sent code */
1683 if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
1684 $svr_allow_sent = true;
1686 $svr_allow_sent = false;
1689 if (isset($sent_folder) && (($sent_folder != '') ||
($sent_folder != 'none'))
1690 && sqimap_mailbox_exists( $imap_stream, $sent_folder)) {
1696 if ((isset($move_to_sent) && ($move_to_sent != 0)) ||
(!isset($move_to_sent))) {
1697 $lcl_allow_sent = true;
1699 $lcl_allow_sent = false;
1702 if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) ||
($fld_sent && $lcl_allow_sent)) {
1703 global $passed_id, $mailbox, $action;
1704 if ($action == 'reply' ||
$action == 'reply_all') {
1705 $save_reply_with_orig=getPref($data_dir,$username,'save_reply_with_orig');
1706 if ($save_reply_with_orig) {
1707 $sent_folder = $mailbox;
1710 sqimap_append ($imap_stream, $sent_folder, $length);
1711 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1712 $imap_deliver = new Deliver_IMAP();
1713 $imap_deliver->mail($composeMessage, $imap_stream);
1714 sqimap_append_done ($imap_stream, $sent_folder);
1715 unset ($imap_deliver);
1718 global $passed_id, $mailbox, $action, $what, $iAccount,$startMessage;
1720 $composeMessage->purgeAttachments();
1721 if ($action == 'reply' ||
$action == 'reply_all') {
1722 require(SM_PATH
. 'functions/mailbox_display.php');
1723 $aMailbox = sqm_api_mailbox_select($imap_stream, $iAccount, $mailbox,array('setindex' => $what, 'offset' => $startMessage),array());
1724 // check if we are allowed to set the \\Answered flag
1725 if (in_array('\\answered',$aMailbox['PERMANENTFLAGS'], true)) {
1726 $aUpdatedMsgs = sqimap_toggle_flag($imap_stream, array($passed_id), '\\Answered', true, false);
1727 if (isset($aUpdatedMsgs[$passed_id]['FLAGS'])) {
1729 * Only update the cached headers if the header is
1732 if (isset($aMailbox['MSG_HEADERS'][$passed_id])) {
1733 $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'] = $aMsg['FLAGS'];
1738 * Write mailbox with updated seen flag information back to cache.
1740 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
1741 sqsession_register($mailbox_cache,'mailbox_cache');
1743 sqimap_logout($imap_stream);