5 * This code sends a mail.
7 * There are 4 modes of operation:
13 * @copyright © 1999-2007 The SquirrelMail Project Team
14 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
16 * @package squirrelmail
20 * Include the SquirrelMail initialization file.
22 require('../include/init.php');
24 /* SquirrelMail required files. */
25 require_once(SM_PATH
. 'functions/imap_general.php');
26 require_once(SM_PATH
. 'functions/imap_messages.php');
27 require_once(SM_PATH
. 'functions/date.php');
28 require_once(SM_PATH
. 'functions/mime.php');
29 require_once(SM_PATH
. 'functions/compose.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
);
44 // Turn on delayed error handling in case we wind up redirecting below
45 $oErrorHandler->setDelayedErrors(true);
47 /** SESSION/POST/GET VARS */
48 sqgetGlobalVar('session',$session);
49 sqgetGlobalVar('mailbox',$mailbox);
50 if(!sqgetGlobalVar('identity',$identity)) {
53 sqgetGlobalVar('send_to',$send_to);
54 sqgetGlobalVar('send_to_cc',$send_to_cc);
55 sqgetGlobalVar('send_to_bcc',$send_to_bcc);
56 sqgetGlobalVar('subject',$subject);
57 sqgetGlobalVar('body',$body);
58 sqgetGlobalVar('mailprio',$mailprio);
59 sqgetGlobalVar('request_mdn',$request_mdn);
60 sqgetGlobalVar('request_dr',$request_dr);
61 sqgetGlobalVar('html_addr_search',$html_addr_search);
62 sqgetGlobalVar('mail_sent',$mail_sent);
63 sqgetGlobalVar('passed_id',$passed_id);
64 sqgetGlobalVar('passed_ent_id',$passed_ent_id);
65 sqgetGlobalVar('send',$send);
67 sqgetGlobalVar('attach',$attach);
69 sqgetGlobalVar('draft',$draft);
70 sqgetGlobalVar('draft_id',$draft_id);
71 sqgetGlobalVar('ent_num',$ent_num);
72 sqgetGlobalVar('saved_draft',$saved_draft);
74 if ( sqgetGlobalVar('delete_draft',$delete_draft) ) {
75 $delete_draft = (int)$delete_draft;
78 if ( sqgetGlobalVar('startMessage',$startMessage) ) {
79 $startMessage = (int)$startMessage;
86 sqgetGlobalVar('sigappend', $sigappend, SQ_POST
);
87 sqgetGlobalVar('from_htmladdr_search', $from_htmladdr_search, SQ_POST
);
88 sqgetGlobalVar('addr_search_done', $html_addr_search_done, SQ_POST
);
89 sqgetGlobalVar('addr_search_cancel', $html_addr_search_cancel, SQ_POST
);
90 sqgetGlobalVar('send_to_search', $send_to_search, SQ_POST
);
91 sqgetGlobalVar('do_delete', $do_delete, SQ_POST
);
92 sqgetGlobalVar('delete', $delete, SQ_POST
);
93 sqgetGlobalVar('restoremessages', $restoremessages, SQ_POST
);
94 if ( sqgetGlobalVar('return', $temp, SQ_POST
) ) {
95 $html_addr_search_done = 'Use Addresses';
99 sqgetGlobalVar('attachedmessages', $attachedmessages, SQ_GET
);
100 if ( sqgetGlobalVar('account', $temp, SQ_GET
) ) {
101 $iAccount = (int) $temp;
108 if ( !sqgetGlobalVar('smaction',$action) )
110 if ( sqgetGlobalVar('smaction_reply',$tmp) ) $action = 'reply';
111 if ( sqgetGlobalVar('smaction_reply_all',$tmp) ) $action = 'reply_all';
112 if ( sqgetGlobalVar('smaction_forward',$tmp) ) $action = 'forward';
113 if ( sqgetGlobalVar('smaction_attache',$tmp) ) $action = 'forward_as_attachment';
114 if ( sqgetGlobalVar('smaction_draft',$tmp) ) $action = 'draft';
115 if ( sqgetGlobalVar('smaction_edit_new',$tmp) ) $action = 'edit_as_new';
119 * Here we decode the data passed in from mailto.php.
121 if ( sqgetGlobalVar('mailtodata', $mailtodata, SQ_GET
) ) {
122 $trtable = array('to' => 'send_to',
123 'cc' => 'send_to_cc',
124 'bcc' => 'send_to_bcc',
126 'subject' => 'subject');
127 $mtdata = unserialize($mailtodata);
129 foreach ($trtable as $f => $t) {
130 if ( !empty($mtdata[$f]) ) {
134 unset($mailtodata,$mtdata, $trtable);
137 /* Location (For HTTP 1.1 Header("Location: ...") redirects) */
138 $location = get_location();
139 /* Identities (fetch only once) */
140 $idents = get_identities();
142 /* --------------------- Specific Functions ------------------------------ */
144 function replyAllString($header) {
145 global $include_self_reply_all, $idents;
148 * 1) Remove the addresses we'll be sending the message 'to'
150 if (isset($header->reply_to
)) {
151 $excl_ar = $header->getAddr_a('reply_to');
154 * 2) Remove our identities from the CC list (they still can be in the
155 * TO list) only if $include_self_reply_all is turned off
157 if (!$include_self_reply_all) {
158 foreach($idents as $id) {
159 $excl_ar[strtolower(trim($id['email_address']))] = '';
164 * 3) get the addresses.
166 $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar);
169 * 4) generate the string.
171 $url_replytoallcc = '';
172 foreach( $url_replytoall_ar as $email => $personal) {
174 // if personal name contains address separator then surround
175 // the personal name with double quotes.
176 if (strpos($personal,',') !== false) {
177 $personal = '"'.$personal.'"';
179 $url_replytoallcc .= ", $personal <$email>";
181 $url_replytoallcc .= ', '. $email;
184 $url_replytoallcc = substr($url_replytoallcc,2);
186 return $url_replytoallcc;
190 * creates top line in reply citations
192 * Line style depends on user preferences.
193 * $orig_date argument is available only from 1.4.3 and 1.5.1 version.
194 * @param object $orig_from From: header object.
195 * @param integer $orig_date email's timestamp
196 * @return string reply citation
198 function getReplyCitation($orig_from, $orig_date) {
199 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
201 if (!is_object($orig_from)) {
204 $sOrig_from = decodeHeader($orig_from->getAddress(false),false,false,true);
207 /* First, return an empty string when no citation style selected. */
208 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
212 /* Make sure our final value isn't an empty string. */
213 if ($sOrig_from == '') {
217 /* Otherwise, try to select the desired citation style. */
218 switch ($reply_citation_style) {
221 * To translators: %s is for author's name
223 $full_reply_citation = sprintf(_("%s wrote:"),$sOrig_from);
226 $start = '<quote who="';
228 $full_reply_citation = $start . $sOrig_from . $end;
230 case 'date_time_author':
233 * first %s is for date string, second %s is for author's name. Date uses
234 * formating from "D, F j, Y g:i a" and "D, F j, Y H:i" translations.
236 * "On Sat, December 24, 2004 23:59, Santa wrote:"
237 * If you have to put author's name in front of date string, check comments about
238 * argument swapping at http://www.php.net/sprintf
240 $full_reply_citation = sprintf(_("On %s, %s wrote:"), getLongDateString($orig_date), $sOrig_from);
243 $start = $reply_citation_start .
244 ($reply_citation_start == '' ?
'' : ' ');
245 $end = $reply_citation_end;
246 $full_reply_citation = $start . $sOrig_from . $end;
252 /* Add line feed and return the citation string. */
253 return ($full_reply_citation . "\n");
257 * Creates header fields in forwarded email body
259 * $default_charset global must be set correctly before you call this function.
260 * @param object $orig_header
263 function getforwardHeader($orig_header) {
264 global $editor_size, $default_charset;
266 // using own strlen function in order to detect correct string length
267 $display = array( _("Subject") => sq_strlen(_("Subject"),$default_charset),
268 _("From") => sq_strlen(_("From"),$default_charset),
269 _("Date") => sq_strlen(_("Date"),$default_charset),
270 _("To") => sq_strlen(_("To"),$default_charset),
271 _("Cc") => sq_strlen(_("Cc"),$default_charset) );
272 $maxsize = max($display);
273 $indent = str_pad('',$maxsize+
2);
274 foreach($display as $key => $val) {
275 $display[$key] = $key .': '. str_pad('', $maxsize - $val);
277 $from = decodeHeader($orig_header->getAddr_s('from',"\n$indent"),false,false,true);
278 $from = str_replace(' ',' ',$from);
279 $to = decodeHeader($orig_header->getAddr_s('to',"\n$indent"),false,false,true);
280 $to = str_replace(' ',' ',$to);
281 $subject = decodeHeader($orig_header->subject
,false,false,true);
282 $subject = str_replace(' ',' ',$subject);
284 // using own str_pad function in order to create correct string pad
285 $bodyTop = sq_str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH
,$default_charset) .
286 "\n". $display[_("Subject")] . $subject . "\n" .
287 $display[_("From")] . $from . "\n" .
288 $display[_("Date")] . getLongDateString( $orig_header->date
, $orig_header->date_unparsed
). "\n" .
289 $display[_("To")] . $to . "\n";
290 if ($orig_header->cc
!= array() && $orig_header->cc
!='') {
291 $cc = decodeHeader($orig_header->getAddr_s('cc',"\n$indent"),false,false,true);
292 $cc = str_replace(' ',' ',$cc);
293 $bodyTop .= $display[_("Cc")] .$cc . "\n";
295 $bodyTop .= str_pad('', $editor_size -2 , '-') .
299 /* ----------------------------------------------------------------------- */
302 * If the session is expired during a post this restores the compose session
305 if (sqsession_is_registered('session_expired_post')) {
306 sqgetGlobalVar('session_expired_post', $session_expired_post, SQ_SESSION
);
308 * extra check for username so we don't display previous post data from
309 * another user during this session.
311 if ($session_expired_post['username'] != $username) {
312 unset($session_expired_post);
313 sqsession_unregister('session_expired_post');
314 session_write_close();
316 // these are the vars that we can set from the expired composed session
317 $compo_var_list = array ( 'send_to', 'send_to_cc','body','startMessage',
318 'passed_body','use_signature','signature','attachments','subject','newmail',
319 'send_to_bcc', 'passed_id', 'mailbox', 'from_htmladdr_search', 'identity',
320 'draft_id', 'delete_draft', 'mailprio', 'edit_as_new', 'compose_messsages',
321 'composesession', 'request_mdn', 'request_dr');
323 foreach ($compo_var_list as $var) {
324 if ( isset($session_expired_post[$var]) && !isset($
$var) ) {
325 $
$var = $session_expired_post[$var];
329 $compose_messages = unserialize(urldecode($restoremessages));
330 sqsession_register($compose_messages,'compose_messages');
331 sqsession_register($composesession,'composesession');
335 $session_expired = true;
337 unset($session_expired_post);
338 sqsession_unregister('session_expired_post');
339 session_write_close();
340 if (!isset($mailbox)) {
343 if ($compose_new_win == '1') {
344 compose_Header($color, $mailbox);
346 $sHeaderJs = (isset($sHeaderJs)) ?
$sHeaderJs : '';
347 if (strpos($action, 'reply') !== false && $reply_focus) {
348 $sBodyTagJs = 'onload="checkForm(\''.$replyfocus.'\');"';
350 $sBodyTagJs = 'onload="checkForm();"';
352 displayPageHeader($color, $mailbox,$sHeaderJs,$sBodyTagJs);
354 showInputForm($session, false);
357 if (!isset($composesession)) {
359 sqsession_register(0,'composesession');
361 $composesession = (int)$composesession;
364 if (!isset($session) ||
(isset($newmessage) && $newmessage)) {
365 sqsession_unregister('composesession');
366 $session = "$composesession" +
1;
367 $composesession = $session;
368 sqsession_register($composesession,'composesession');
370 if (!isset($compose_messages)) {
371 $compose_messages = array();
374 if (!isset($compose_messages[$session]) ||
($compose_messages[$session] == NULL)) {
375 $composeMessage = new Message();
376 $rfc822_header = new Rfc822Header();
377 $composeMessage->rfc822_header
= $rfc822_header;
378 $composeMessage->reply_rfc822_header
= '';
379 $compose_messages[$session] = $composeMessage;
381 sqsession_register($compose_messages,'compose_messages');
383 $composeMessage=$compose_messages[$session];
386 if (empty($mailbox)) {
392 * Set $default_charset to correspond with the user's selection
393 * of language interface.
396 $composeMessage=$compose_messages[$session];
397 if (! deliverMessage($composeMessage, true)) {
398 showInputForm($session);
401 unset($compose_messages[$session]);
402 $draft_message = _("Draft Email Saved");
403 /* If this is a resumed draft, then delete the original */
404 if(isset($delete_draft)) {
405 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, false);
406 sqimap_mailbox_select($imap_stream, $draft_folder);
407 // force bypass_trash=true because message should be saved when deliverMessage() returns true.
408 // in current implementation of sqimap_msgs_list_flag() single message id can
409 // be submitted as string. docs state that it should be array.
410 sqimap_msgs_list_delete($imap_stream, $draft_folder, $delete_draft, true);
412 sqimap_mailbox_expunge($imap_stream, $draft_folder, true);
414 sqimap_logout($imap_stream);
417 $oErrorHandler->saveDelayedErrors();
418 session_write_close();
420 if ($compose_new_win == '1') {
421 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
422 Header("Location: $location/compose.php?saved_draft=yes&session=$composesession");
424 echo ' <br><br><div style="text-align: center;"><a href="' . $location
425 . '/compose.php?saved_sent=yes&session=' . $composesession . '">'
426 . _("Return") . '</a></div>';
430 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
431 Header("Location: $location/right_main.php?mailbox=" . urlencode($draft_folder) .
432 "&startMessage=1¬e=".urlencode($draft_message));
434 echo ' <br><br><div style="text-align: center;"><a href="' . $location
435 . '/right_main.php?mailbox=' . urlencode($draft_folder)
436 . '&startMessage=1&note=' . urlencode($draft_message) .'">'
437 . _("Return") . '</a></div>';
445 if (isset($_FILES['attachfile']) &&
446 $_FILES['attachfile']['tmp_name'] &&
447 $_FILES['attachfile']['tmp_name'] != 'none') {
448 $AttachFailure = saveAttachedFiles($session);
450 if (checkInput(false) && !isset($AttachFailure)) {
451 if ($mailbox == "All Folders") {
452 /* We entered compose via the search results page */
453 $mailbox = 'INBOX'; /* Send 'em to INBOX, that's safe enough */
455 $urlMailbox = urlencode (trim($mailbox));
456 if (! isset($passed_id)) {
460 * Set $default_charset to correspond with the user's selection
461 * of language interface.
465 * This is to change all newlines to \n
466 * We'll change them to \r\n later (in the sendMessage function)
468 $body = str_replace("\r\n", "\n", $body);
469 $body = str_replace("\r", "\n", $body);
472 * Rewrap $body so that no line is bigger than $editor_size
474 $body = explode("\n", $body);
476 foreach ($body as $line) {
477 if( $line <> '-- ' ) {
478 $line = rtrim($line);
480 if (sq_strlen($line, $default_charset) <= $editor_size +
1) {
481 $newBody .= $line . "\n";
483 sqWordWrap($line, $editor_size, $default_charset);
484 $newBody .= $line . "\n";
491 $composeMessage=$compose_messages[$session];
493 $Result = deliverMessage($composeMessage);
500 // NOTE: this hook changed in 1.5.2 from sending $Result and
501 // $composeMessage as args #2 and #3 to being in an array
503 do_hook('compose_send_after', $temp=array(&$Result, &$composeMessage, &$mail_sent));
505 showInputForm($session);
508 unset($compose_messages[$session]);
510 /* if it is resumed draft, delete draft message */
511 if ( isset($delete_draft)) {
512 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, false);
513 sqimap_mailbox_select($imap_stream, $draft_folder);
514 // bypass_trash=true because message should be saved when deliverMessage() returns true.
515 // in current implementation of sqimap_msgs_list_flag() single message id can
516 // be submitted as string. docs state that it should be array.
517 sqimap_msgs_list_delete($imap_stream, $draft_folder, $delete_draft, true);
519 sqimap_mailbox_expunge($imap_stream, $draft_folder, true);
521 sqimap_logout($imap_stream);
524 * Store the error array in the session because they will be lost on a redirect
526 $oErrorHandler->saveDelayedErrors();
527 session_write_close();
529 if ($compose_new_win == '1') {
530 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
531 Header("Location: $location/compose.php?mail_sent=$mail_sent");
533 echo ' <br><br><div style="text-align: center;"><a href="' . $location
534 . '/compose.php?mail_sent=$mail_sent">'
535 . _("Return") . '</a></div>';
539 if ( !isset($pageheader_sent) ||
!$pageheader_sent ) {
540 Header("Location: $location/right_main.php?mailbox=$urlMailbox".
541 "&startMessage=$startMessage&mail_sent=$mail_sent");
543 echo ' <br><br><div style="text-align: center;"><a href="' . $location
544 . "/right_main.php?mailbox=$urlMailbox"
545 . "&startMessage=$startMessage&mail_sent=$mail_sent\">"
546 . _("Return") . '</a></div>';
551 if ($compose_new_win == '1') {
552 compose_Header($color, $mailbox);
555 displayPageHeader($color, $mailbox);
557 if (isset($AttachFailure)) {
558 plain_error_message(_("Could not move/copy file. File not attached"),
562 showInputForm($session);
563 /* sqimap_logout($imapConnection); */
565 } elseif (isset($html_addr_search_done)) {
566 if ($compose_new_win == '1') {
567 compose_Header($color, $mailbox);
570 displayPageHeader($color, $mailbox);
573 if (isset($send_to_search) && is_array($send_to_search)) {
574 foreach ($send_to_search as $k => $v) {
575 if (substr($k, 0, 1) == 'T') {
581 elseif (substr($k, 0, 1) == 'C') {
587 elseif (substr($k, 0, 1) == 'B') {
589 $send_to_bcc .= ', ';
595 showInputForm($session);
596 } elseif (isset($html_addr_search) && !isset($html_addr_search_cancel)) {
597 if (isset($_FILES['attachfile']) &&
598 $_FILES['attachfile']['tmp_name'] &&
599 $_FILES['attachfile']['tmp_name'] != 'none') {
600 if(saveAttachedFiles($session)) {
601 plain_error_message(_("Could not move/copy file. File not attached"));
605 * I am using an include so as to elminiate an extra unnecessary
606 * click. If you can think of a better way, please implement it.
608 include_once('./addrbook_search_html.php');
609 } elseif (isset($attach)) {
610 if ($compose_new_win == '1') {
611 compose_Header($color, $mailbox);
613 displayPageHeader($color, $mailbox);
615 if (saveAttachedFiles($session)) {
616 plain_error_message(_("Could not move/copy file. File not attached"));
618 showInputForm($session);
620 elseif (isset($sigappend)) {
621 $signature = $idents[$identity]['signature'];
623 $body .= "\n\n".($prefix_sig==true?
"-- \n":'').$signature;
624 if ($compose_new_win == '1') {
625 compose_Header($color, $mailbox);
627 displayPageHeader($color, $mailbox);
629 showInputForm($session);
630 } elseif (isset($do_delete)) {
631 if ($compose_new_win == '1') {
632 compose_Header($color, $mailbox);
634 displayPageHeader($color, $mailbox);
637 if (isset($delete) && is_array($delete)) {
638 $composeMessage = $compose_messages[$session];
639 foreach($delete as $index) {
640 if (!empty($composeMessage->entities
) && isset($composeMessage->entities
[$index])) {
641 $composeMessage->entities
[$index]->purgeAttachments();
642 unset ($composeMessage->entities
[$index]);
645 $new_entities = array();
646 foreach ($composeMessage->entities
as $entity) {
647 $new_entities[] = $entity;
649 $composeMessage->entities
= $new_entities;
650 $compose_messages[$session] = $composeMessage;
651 sqsession_register($compose_messages, 'compose_messages');
653 showInputForm($session);
656 * This handles the default case as well as the error case
657 * (they had the same code) --> if (isset($smtpErrors))
660 if ($compose_new_win == '1') {
661 compose_Header($color, $mailbox);
663 displayPageHeader($color, $mailbox);
668 if (!isset($passed_ent_id)) {
671 if (!isset($passed_id)) {
674 if (!isset($mailbox)) {
677 if (!isset($action)) {
681 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
683 /* in case the origin is not read_body.php */
684 if (isset($send_to)) {
685 $values['send_to'] = $send_to;
687 if (isset($send_to_cc)) {
688 $values['send_to_cc'] = $send_to_cc;
690 if (isset($send_to_bcc)) {
691 $values['send_to_bcc'] = $send_to_bcc;
693 if (isset($subject)) {
694 $values['subject'] = $subject;
696 showInputForm($session, $values);
701 /**************** Only function definitions go below *************/
703 function getforwardSubject($subject)
705 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
706 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
707 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
708 $subject = '[Fwd: ' . $subject . ']';
713 /* This function is used when not sending or adding attachments */
714 function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
715 global $editor_size, $default_use_priority, $body, $idents,
716 $use_signature, $data_dir, $username,
717 $key, $imapServerAddress, $imapPort, $compose_messages,
718 $composeMessage, $body_quote, $request_mdn, $request_dr,
719 $default_use_mdn, $mdn_user_support;
720 global $languages, $squirrelmail_language, $default_charset;
723 * Set $default_charset to correspond with the user's selection
724 * of language interface. $default_charset global is not correct,
725 * if message is composed in new window.
729 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
733 $imapConnection = sqimap_login($username, false, $imapServerAddress,
736 sqimap_mailbox_select($imapConnection, $mailbox);
737 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
740 if ($passed_ent_id) {
741 /* redefine the messsage in case of message/rfc822 */
742 $message = $message->getEntity($passed_ent_id);
743 /* message is an entity which contains the envelope and type0=message
744 * and type1=rfc822. The actual entities are childs from
745 * $message->entities[0]. That's where the encoding and is located
748 $entities = $message->entities
[0]->findDisplayEntity
749 (array(), $alt_order = array('text/plain'));
750 if (!count($entities)) {
751 $entities = $message->entities
[0]->findDisplayEntity
752 (array(), $alt_order = array('text/plain','html/plain'));
754 $orig_header = $message->rfc822_header
; /* here is the envelope located */
755 /* redefine the message for picking up the attachments */
756 $message = $message->entities
[0];
759 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
760 if (!count($entities)) {
761 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
763 $orig_header = $message->rfc822_header
;
766 $type0 = $message->type0
;
767 $type1 = $message->type1
;
768 foreach ($entities as $ent) {
769 $msg = $message->getEntity($ent);
770 $type0 = $msg->type0
;
771 $type1 = $msg->type1
;
772 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
773 $body_part_entity = $message->getEntity($ent);
774 $bodypart = decodeBody($unencoded_bodypart,
775 $body_part_entity->header
->encoding
);
776 if ($type1 == 'html') {
777 $bodypart = str_replace("\n", ' ', $bodypart);
778 $bodypart = preg_replace(array('/<\/?p>/i','/<div><\/div>/i','/<br\s*(\/)*>/i','/<\/?div>/i'), "\n", $bodypart);
779 $bodypart = str_replace(array(' ','>','<'),array(' ','>','<'),$bodypart);
780 $bodypart = strip_tags($bodypart);
782 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
783 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
784 if (mb_detect_encoding($bodypart) != 'ASCII') {
785 $bodypart = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $bodypart);
789 // charset encoding in compose form stuff
790 if (isset($body_part_entity->header
->parameters
['charset'])) {
791 $actual = $body_part_entity->header
->parameters
['charset'];
793 $actual = 'us-ascii';
796 if ( $actual && is_conversion_safe($actual) && $actual != $default_charset){
797 $bodypart = charset_convert($actual,$bodypart,$default_charset,false);
799 // end of charset encoding in compose
803 if ($default_use_priority) {
804 $mailprio = substr($orig_header->priority
,0,1);
813 $from_o = $orig_header->from
;
814 if (is_array($from_o)) {
815 if (isset($from_o[0])) {
816 $from_o = $from_o[0];
819 if (is_object($from_o)) {
820 $orig_from = $from_o->getAddress();
825 $identities = array();
826 if (count($idents) > 1) {
827 foreach($idents as $nr=>$data) {
828 $enc_from_name = '"'.$data['full_name'].'" <'. $data['email_address'].'>';
829 if($enc_from_name == $orig_from) {
833 $identities[] = $enc_from_name;
836 $identity_match = $orig_header->findAddress($identities);
837 if ($identity_match) {
838 $identity = $identity_match;
844 $use_signature = FALSE;
845 $composeMessage->rfc822_header
= $orig_header;
846 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
847 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
848 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
849 $send_from = $orig_header->getAddr_s('from');
850 $send_from_parts = new AddressStructure();
851 $send_from_parts = $orig_header->parseAddress($send_from);
852 $send_from_add = $send_from_parts->mailbox
. '@' . $send_from_parts->host
;
853 $identities = get_identities();
854 if (count($identities) > 0) {
855 foreach($identities as $iddata) {
856 if ($send_from_add == $iddata['email_address']) {
857 $identity = $iddata['index'];
862 $subject = decodeHeader($orig_header->subject
,false,false,true);
864 // Remember the receipt settings
865 $request_mdn = $mdn_user_support && !empty($orig_header->dnt
) ?
'1' : '0';
866 $request_dr = $mdn_user_support && !empty($orig_header->drnt
) ?
'1' : '0';
868 /* remember the references and in-reply-to headers in case of an reply */
869 $composeMessage->rfc822_header
->more_headers
['References'] = $orig_header->references
;
870 $composeMessage->rfc822_header
->more_headers
['In-Reply-To'] = $orig_header->in_reply_to
;
871 // rewrap the body to clean up quotations and line lengths
872 sqBodyWrap($body, $editor_size);
873 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
875 case ('edit_as_new'):
876 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
877 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
878 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
879 $subject = decodeHeader($orig_header->subject
,false,false,true);
880 $mailprio = $orig_header->priority
;
882 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
883 // rewrap the body to clean up quotations and line lengths
884 sqBodyWrap($body, $editor_size);
888 $subject = getforwardSubject(decodeHeader($orig_header->subject
,false,false,true));
889 $body = getforwardHeader($orig_header) . $body;
890 // the logic for calling sqUnWordWrap here would be to allow the browser to wrap the lines
891 // forwarded message text should be as undisturbed as possible, so commenting out this call
892 // sqUnWordWrap($body);
893 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
895 //add a blank line after the forward headers
896 $body = "\n" . $body;
898 case ('forward_as_attachment'):
899 $subject = getforwardSubject(decodeHeader($orig_header->subject
,false,false,true));
900 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
904 if(isset($orig_header->mail_followup_to
) && $orig_header->mail_followup_to
) {
905 $send_to = $orig_header->getAddr_s('mail_followup_to');
907 $send_to_cc = replyAllString($orig_header);
908 $send_to_cc = decodeHeader($send_to_cc,false,false,true);
911 // skip this if send_to was already set right above here
913 $send_to = $orig_header->reply_to
;
914 if (is_array($send_to) && count($send_to)) {
915 $send_to = $orig_header->getAddr_s('reply_to');
916 } else if (is_object($send_to)) { /* unneccesarry, just for failsafe purpose */
917 $send_to = $orig_header->getAddr_s('reply_to');
919 $send_to = $orig_header->getAddr_s('from');
922 $send_to = decodeHeader($send_to,false,false,true);
923 $subject = decodeHeader($orig_header->subject
,false,false,true);
924 $subject = str_replace('"', "'", $subject);
925 $subject = trim($subject);
926 if (substr(strtolower($subject), 0, 3) != 're:') {
927 $subject = 'Re: ' . $subject;
929 /* this corrects some wrapping/quoting problems on replies */
930 $rewrap_body = explode("\n", $body);
931 $from = (is_array($orig_header->from
) && !empty($orig_header->from
)) ?
$orig_header->from
[0] : $orig_header->from
;
933 $strip_sigs = getPref($data_dir, $username, 'strip_sigs');
934 foreach ($rewrap_body as $line) {
935 if ($strip_sigs && substr($line,0,3) == '-- ') {
938 if (preg_match("/^(>+)/", $line, $matches)) {
940 $body .= $body_quote . str_replace("\n", "\n$body_quote$gt ", rtrim($line)) ."\n";
942 $body .= $body_quote . (!empty($body_quote) ?
' ' : '') . str_replace("\n", "\n$body_quote" . (!empty($body_quote) ?
' ' : ''), rtrim($line)) . "\n";
946 //rewrap the body to clean up quotations and line lengths
947 $body = sqBodyWrap ($body, $editor_size);
949 $body = getReplyCitation($from , $orig_header->date
) . $body;
950 $composeMessage->reply_rfc822_header
= $orig_header;
956 $compose_messages[$session] = $composeMessage;
957 sqsession_register($compose_messages, 'compose_messages');
958 session_write_close();
959 sqimap_logout($imapConnection);
961 $ret = array( 'send_to' => $send_to,
962 'send_to_cc' => $send_to_cc,
963 'send_to_bcc' => $send_to_bcc,
964 'subject' => $subject,
965 'mailprio' => $mailprio,
967 'identity' => $identity );
970 } /* function newMail() */
973 * downloads attachments from original message, stores them in attachment directory and adds
974 * them to composed message.
975 * @param object $message
976 * @param object $composeMessage
977 * @param integer $passed_id
978 * @param mixed $entities
979 * @param mixed $imapConnection
982 function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
983 global $squirrelmail_language, $languages;
985 if (!count($message->entities
) ||
986 ($message->type0
== 'message' && $message->type1
== 'rfc822')) {
987 if ( !in_array($message->entity_id
, $entities) && $message->entity_id
) {
988 switch ($message->type0
) {
990 if ($message->type1
== 'rfc822') {
991 $filename = $message->rfc822_header
->subject
;
992 if ($filename == "") {
993 $filename = "untitled-".$message->entity_id
;
997 $filename = $message->getFilename();
1001 if (!$message->mime_header
) { /* temporary hack */
1002 $message->mime_header
= $message->header
;
1004 $filename = $message->getFilename();
1007 $filename = str_replace(' ', ' ', decodeHeader($filename));
1008 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1009 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode')) {
1010 $filename = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode', $filename);
1012 $localfilename = sq_get_attach_tempfile();
1013 $message->att_local_name
= $localfilename;
1015 $composeMessage->initAttachment($message->type0
.'/'.$message->type1
,$filename,
1018 /* Write Attachment to file */
1019 $fp = fopen ($localfilename, 'wb');
1020 mime_print_body_lines ($imapConnection, $passed_id, $message->entity_id
, $message->header
->encoding
, $fp);
1024 for ($i=0, $entCount=count($message->entities
); $i<$entCount;$i++
) {
1025 $composeMessage=getAttachments($message->entities
[$i], $composeMessage, $passed_id, $entities, $imapConnection);
1028 return $composeMessage;
1031 function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
1032 $passed_ent_id='', $imapConnection) {
1033 if (!$passed_ent_id) {
1034 $body_a = sqimap_run_command($imapConnection,
1035 'FETCH '.$passed_id.' RFC822',
1036 TRUE, $response, $readmessage,
1039 $body_a = sqimap_run_command($imapConnection,
1040 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
1041 TRUE, $response, $readmessage, TRUE);
1042 $message = $message->parent
;
1044 if ($response == 'OK') {
1045 $subject = encodeHeader($message->rfc822_header
->subject
);
1046 array_shift($body_a);
1048 $body = implode('', $body_a) . "\r\n";
1050 $localfilename = sq_get_attach_tempfile();
1051 $fp = fopen($localfilename, 'wb');
1052 fwrite ($fp, $body);
1054 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
1057 return $composeMessage;
1060 function showInputForm ($session, $values=false) {
1061 global $send_to, $send_to_cc, $body, $startMessage, $action,
1062 $color, $use_signature, $signature, $prefix_sig,
1063 $editor_size, $editor_height, $subject, $newmail,
1064 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
1065 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
1066 $username, $data_dir, $identity, $idents, $delete_draft,
1067 $mailprio, $compose_new_win, $saved_draft, $mail_sent, $sig_first,
1068 $compose_messages, $composesession, $default_charset,
1069 $compose_onsubmit, $oTemplate, $oErrorHandler;
1071 if (checkForJavascript()) {
1072 $onfocus = ' onfocus="alreadyFocused=true;"';
1073 $onfocus_array = array('onfocus' => 'alreadyFocused=true;');
1077 $onfocus_array = array();
1080 $composeMessage = $compose_messages[$session];
1082 $send_to = $values['send_to'];
1083 $send_to_cc = $values['send_to_cc'];
1084 $send_to_bcc = $values['send_to_bcc'];
1085 $subject = $values['subject'];
1086 $mailprio = $values['mailprio'];
1087 $body = $values['body'];
1088 $identity = (int) $values['identity'];
1090 $send_to = decodeHeader($send_to, true, false);
1091 $send_to_cc = decodeHeader($send_to_cc, true, false);
1092 $send_to_bcc = decodeHeader($send_to_bcc, true, false);
1095 if ($use_javascript_addr_book) {
1096 echo "\n". '<script type="text/javascript">'."\n<!--\n" .
1097 'function open_abook() { ' . "\n" .
1098 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
1099 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
1100 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
1101 ' nwin.opener = document.windows;' . "\n" .
1103 "// -->\n</script>\n\n";
1106 echo "\n" . '<form name="compose" action="compose.php" method="post" ' .
1107 'enctype="multipart/form-data"';
1109 $compose_onsubmit = array();
1111 do_hook('compose_form', $null);
1113 // Plugins that use compose_form hook can add an array entry
1114 // to the globally scoped $compose_onsubmit; we add them up
1115 // here and format the form tag's full onsubmit handler.
1116 // Each plugin should use "return false" if they need to
1117 // stop form submission but otherwise should NOT use "return
1118 // true" to give other plugins the chance to do what they need
1119 // to do; SquirrelMail itself will add the final "return true".
1120 // Onsubmit text is enclosed inside of double quotes, so plugins
1121 // need to quote accordingly.
1122 if (checkForJavascript()) {
1123 $onsubmit_text = ' onsubmit="';
1124 if (empty($compose_onsubmit))
1125 $compose_onsubmit = array();
1126 else if (!is_array($compose_onsubmit))
1127 $compose_onsubmit = array($compose_onsubmit);
1129 foreach ($compose_onsubmit as $text) {
1130 $text = trim($text);
1131 if (substr($text, -1) != ';' && substr($text, -1) != '}')
1133 $onsubmit_text .= $text;
1136 echo $onsubmit_text . ' return true;"';
1142 echo addHidden('startMessage', $startMessage);
1144 if ($action == 'draft') {
1145 echo addHidden('delete_draft', $passed_id);
1147 if (isset($delete_draft)) {
1148 echo addHidden('delete_draft', $delete_draft);
1150 if (isset($session)) {
1151 echo addHidden('session', $session);
1154 if (isset($passed_id)) {
1155 echo addHidden('passed_id', $passed_id);
1158 if ($saved_draft == 'yes') {
1159 $oTemplate->assign('note', _("Your draft has been saved."));
1160 $oTemplate->display('note.tpl');
1162 if ($mail_sent == 'yes') {
1163 $oTemplate->assign('note', _("Your mail has been sent."));
1164 $oTemplate->display('note.tpl');
1166 if ($compose_new_win == '1') {
1167 $oTemplate->display('compose_newwin_close.tpl');
1170 if ($location_of_buttons == 'top') {
1171 showComposeButtonRow();
1174 $identities = array();
1175 if (count($idents) > 1) {
1177 foreach($idents as $id => $data) {
1178 $identities[$id] = $data['full_name'].' <'.$data['email_address'].'>';
1182 $oTemplate->assign('identities', $identities);
1183 $oTemplate->assign('identity_def', $identity);
1184 $oTemplate->assign('input_onfocus', 'onfocus="'.join(' ', $onfocus_array).'"');
1186 $oTemplate->assign('to', htmlspecialchars($send_to));
1187 $oTemplate->assign('cc', htmlspecialchars($send_to_cc));
1188 $oTemplate->assign('bcc', htmlspecialchars($send_to_bcc));
1189 $oTemplate->assign('subject', htmlspecialchars($subject));
1191 $oTemplate->display('compose_header.tpl');
1193 if ($location_of_buttons == 'between') {
1194 showComposeButtonRow();
1198 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
1199 $signature = $idents[$identity]['signature'];
1201 if ($sig_first == '1') {
1203 * FIXME: test is specific to ja_JP translation implementation.
1204 * This test might apply incorrect conversion to other translations, but
1205 * use of 7bit iso-2022-jp charset in other translations might have other
1208 if ($default_charset == 'iso-2022-jp') {
1209 $body_str = "\n\n".($prefix_sig==true?
"-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1211 $body_str = "\n\n".($prefix_sig==true?
"-- \n":'').decodeHeader($signature,false,false);
1213 $body_str .= "\n\n".htmlspecialchars(decodeHeader($body,false,false));
1215 $body_str = "\n\n".htmlspecialchars(decodeHeader($body,false,false));
1216 // FIXME: test is specific to ja_JP translation implementation. See above comments.
1217 if ($default_charset == 'iso-2022-jp') {
1218 $body_str .= "\n\n".($prefix_sig==true?
"-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1220 $body_str .= "\n\n".($prefix_sig==true?
"-- \n":'').decodeHeader($signature,false,false);
1224 $body_str = htmlspecialchars(decodeHeader($body,false,false));
1227 $oTemplate->assign('editor_width', (int)$editor_size);
1228 $oTemplate->assign('editor_height', (int)$editor_height);
1229 $oTemplate->assign('input_onfocus', 'onfocus="'.join(' ', $onfocus_array).'"');
1230 $oTemplate->assign('body', $body_str);
1231 $oTemplate->assign('show_bottom_send', $location_of_buttons!='bottom');
1233 $oTemplate->display ('compose_body.tpl');
1235 if ($location_of_buttons == 'bottom') {
1236 showComposeButtonRow();
1239 /* This code is for attachments */
1240 if ((bool) ini_get('file_uploads')) {
1242 /* Calculate the max size for an uploaded file.
1243 * This is advisory for the user because we can't actually prevent
1244 * people to upload too large files. */
1246 /* php.ini vars which influence the max for uploads */
1247 $configvars = array('post_max_size', 'memory_limit', 'upload_max_filesize');
1248 foreach($configvars as $var) {
1249 /* skip 0 or empty values, and -1 which means 'unlimited' */
1250 if( $size = getByteSize(ini_get($var)) ) {
1251 if ( $size != '-1' ) {
1258 if ($composeMessage->entities
) {
1259 foreach ($composeMessage->entities
as $key => $attachment) {
1260 $attached_file = $attachment->att_local_name
;
1261 if ($attachment->att_local_name ||
$attachment->body_part
) {
1262 $attached_filename = decodeHeader($attachment->mime_header
->getParameter('name'));
1263 $type = $attachment->mime_header
->type0
.'/'.
1264 $attachment->mime_header
->type1
;
1268 $a['FileName'] = $attached_filename;
1269 $a['ContentType'] = $type;
1270 $a['Size'] = filesize($attached_file);
1277 $oTemplate->assign('max_file_size', empty($max) ?
-1 : $max);
1278 $oTemplate->assign('attachments', $attach);
1280 $oTemplate->display('compose_attachments.tpl');
1281 } // End of file_uploads if-block
1282 /* End of attachment code */
1284 //FIXME: no direct echoing to browser, no HTML output in core!
1285 echo addHidden('username', $username).
1286 addHidden('smaction', $action).
1287 addHidden('mailbox', $mailbox);
1289 store the complete ComposeMessages array in a hidden input value
1290 so we can restore them in case of a session timeout.
1292 sqgetGlobalVar('QUERY_STRING', $queryString, SQ_SERVER
);
1293 //FIXME: no direct echoing to browser, no HTML output in core!
1294 echo addHidden('restoremessages', urlencode(serialize($compose_messages))).
1295 addHidden('composesession', $composesession).
1296 addHidden('querystring', $queryString).
1298 if (!(bool) ini_get('file_uploads')) {
1299 /* File uploads are off, so we didn't show that part of the form.
1300 To avoid bogus bug reports, tell the user why. */
1301 //FIXME: no direct echoing to browser, no HTML output in core!
1302 echo '<p style="text-align:center">'
1303 . _("Because PHP file uploads are turned off, you can not attach files to this message. Please see your system administrator for details.")
1307 if ($compose_new_win=='1') {
1308 $oTemplate->display('compose_newwin_close.tpl');
1311 do_hook('compose_bottom', $null);
1313 $oErrorHandler->setDelayedErrors(false);
1314 $oTemplate->display('footer.tpl');
1318 function showComposeButtonRow() {
1319 global $use_javascript_addr_book, $save_as_draft,
1320 $default_use_priority, $mailprio, $default_use_mdn,
1321 $request_mdn, $request_dr,
1322 $data_dir, $username;
1324 global $oTemplate, $buffer_hook;
1326 if ($default_use_priority) {
1327 $priorities = array('1'=>_("High"), '3'=>_("Normal"), '5'=>_("Low"));
1328 $priority = isset($mailprio) ?
$mailprio : 3;
1330 $priorities = array();
1334 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1336 if ($use_javascript_addr_book && checkForJavascript()) {
1337 $addr_book = addButton(_("Addresses"), null, array('onclick' => 'javascript:open_abook();'));
1339 $addr_book = addSubmit(_("Addresses"), 'html_addr_search');
1342 $oTemplate->assign('allow_priority', $default_use_priority==1);
1343 $oTemplate->assign('priority_list', $priorities);
1344 $oTemplate->assign('current_priority', $priority);
1346 $oTemplate->assign('notifications_enabled', $mdn_user_support==1);
1347 $oTemplate->assign('read_receipt', $request_mdn=='1');
1348 $oTemplate->assign('delivery_receipt', $request_dr=='1');
1350 $oTemplate->assign('drafts_enabled', $save_as_draft);
1351 $oTemplate->assign('address_book_button', $addr_book);
1353 $oTemplate->display('compose_buttons.tpl');
1356 function checkInput ($show) {
1358 * I implemented the $show variable because the error messages
1359 * were getting sent before the page header. So, I check once
1360 * using $show=false, and then when i'm ready to display the error
1361 * message, show=true
1363 global $send_to, $send_to_bcc;
1365 if ($send_to == '' && $send_to_bcc == '') {
1367 plain_error_message(_("You have not filled in the \"To:\" field."));
1372 } /* function checkInput() */
1375 /* True if FAILURE */
1376 function saveAttachedFiles($session) {
1377 global $compose_messages;
1379 /* get out of here if no file was attached at all */
1380 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1384 $localfilename = sq_get_attach_tempfile();
1386 // m_u_f works better with restricted PHP installs (safe_mode, open_basedir),
1387 // if that doesn't work, try a simple rename.
1388 if (!@move_uploaded_file
($_FILES['attachfile']['tmp_name'],$localfilename)) {
1389 if (!@rename
($_FILES['attachfile']['tmp_name'], $localfilename)) {
1393 $message = $compose_messages[$session];
1394 $type = strtolower($_FILES['attachfile']['type']);
1395 $name = $_FILES['attachfile']['name'];
1396 $message->initAttachment($type, $name, $localfilename);
1397 $compose_messages[$session] = $message;
1398 sqsession_register($compose_messages , 'compose_messages');
1401 /* parse values like 8M and 2k into bytes */
1402 function getByteSize($ini_size) {
1408 $ini_size = trim($ini_size);
1410 // if there's some kind of letter at the end of the string we need to multiply.
1411 if(!is_numeric(substr($ini_size, -1))) {
1413 switch(strtoupper(substr($ini_size, -1))) {
1415 $bytesize = 1073741824;
1418 $bytesize = 1048576;
1425 return ($bytesize * (int)substr($ini_size, 0, -1));
1433 * temporary function to make use of the deliver class.
1434 * In the future the responsible backend should be automaticly loaded
1435 * and conf.pl should show a list of available backends.
1436 * The message also should be constructed by the message class.
1438 function deliverMessage($composeMessage, $draft=false) {
1439 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
1440 $username, $popuser, $usernamedata, $identity, $idents, $data_dir,
1441 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
1442 $domain, $action, $default_move_to_sent, $move_to_sent;
1443 global $imapServerAddress, $imapPort, $sent_folder, $key;
1445 $rfc822_header = $composeMessage->rfc822_header
;
1447 $abook = addressbook_init(false, true);
1448 $rfc822_header->to
= $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
1449 $rfc822_header->cc
= $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
1450 $rfc822_header->bcc
= $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
1451 $rfc822_header->priority
= $mailprio;
1452 $rfc822_header->subject
= $subject;
1454 $special_encoding='';
1455 if (strtolower($default_charset) == 'iso-2022-jp') {
1456 if (mb_detect_encoding($body) == 'ASCII') {
1457 $special_encoding = '8bit';
1459 $body = mb_convert_encoding($body, 'JIS');
1460 $special_encoding = '7bit';
1463 $composeMessage->setBody($body);
1465 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
1466 $popuser = $usernamedata[1];
1467 $domain = $usernamedata[2];
1468 unset($usernamedata);
1470 $popuser = $username;
1473 $from_mail = $idents[$identity]['email_address'];
1474 $full_name = $idents[$identity]['full_name'];
1475 $reply_to = $idents[$identity]['reply_to'];
1477 $from_mail = "$popuser@$domain";
1479 $rfc822_header->from
= $rfc822_header->parseAddress($from_mail,true);
1481 $from = $rfc822_header->from
[0];
1482 if (!$from->host
) $from->host
= $domain;
1483 $full_name_encoded = encodeHeader($full_name);
1484 if ($full_name_encoded != $full_name) {
1485 $from_addr = $full_name_encoded .' <'.$from->mailbox
.'@'.$from->host
.'>';
1487 $from_addr = '"'.$full_name .'" <'.$from->mailbox
.'@'.$from->host
.'>';
1489 $rfc822_header->from
= $rfc822_header->parseAddress($from_addr,true);
1492 $rfc822_header->reply_to
= $rfc822_header->parseAddress($reply_to,true);
1494 /* Receipt: On Read */
1495 if (isset($request_mdn) && $request_mdn) {
1496 $rfc822_header->dnt
= $rfc822_header->parseAddress($from_mail,true);
1497 } elseif (isset($rfc822_header->dnt
)) {
1498 unset($rfc822_header->dnt
);
1501 /* Receipt: On Delivery */
1502 if (isset($request_dr) && $request_dr) {
1503 $rfc822_header->more_headers
['Return-Receipt-To'] = $from_mail;
1504 } elseif (isset($rfc822_header->more_headers
['Return-Receipt-To'])) {
1505 unset($rfc822_header->more_headers
['Return-Receipt-To']);
1508 /* multipart messages */
1509 if (count($composeMessage->entities
)) {
1510 $message_body = new Message();
1511 $message_body->body_part
= $composeMessage->body_part
;
1512 $composeMessage->body_part
= '';
1513 $mime_header = new MessageHeader
;
1514 $mime_header->type0
= 'text';
1515 $mime_header->type1
= 'plain';
1516 if ($special_encoding) {
1517 $mime_header->encoding
= $special_encoding;
1519 $mime_header->encoding
= '8bit';
1521 if ($default_charset) {
1522 $mime_header->parameters
['charset'] = $default_charset;
1524 $message_body->mime_header
= $mime_header;
1525 array_unshift($composeMessage->entities
, $message_body);
1526 $content_type = new ContentType('multipart/mixed');
1528 $content_type = new ContentType('text/plain');
1529 if ($special_encoding) {
1530 $rfc822_header->encoding
= $special_encoding;
1532 $rfc822_header->encoding
= '8bit';
1534 if ($default_charset) {
1535 $content_type->properties
['charset']=$default_charset;
1539 $rfc822_header->content_type
= $content_type;
1540 $composeMessage->rfc822_header
= $rfc822_header;
1542 /* Here you can modify the message structure just before we hand
1543 it over to deliver; plugin authors note that $composeMessage
1544 is sent and modified by reference since 1.5.2 */
1545 do_hook('compose_send', $composeMessage);
1547 if (!$useSendmail && !$draft) {
1548 require_once(SM_PATH
. 'class/deliver/Deliver_SMTP.class.php');
1549 $deliver = new Deliver_SMTP();
1550 global $smtpServerAddress, $smtpPort, $pop_before_smtp;
1552 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ?
true : false;
1553 get_smtp_user($user, $pass);
1554 $stream = $deliver->initStream($composeMessage,$domain,0,
1555 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
1556 } elseif (!$draft) {
1557 require_once(SM_PATH
. 'class/deliver/Deliver_SendMail.class.php');
1558 global $sendmail_path, $sendmail_args;
1559 // Check for outdated configuration
1560 if (!isset($sendmail_args)) {
1561 if ($sendmail_path=='/var/qmail/bin/qmail-inject') {
1562 $sendmail_args = '';
1564 $sendmail_args = '-i -t';
1567 $deliver = new Deliver_SendMail(array('sendmail_args'=>$sendmail_args));
1568 $stream = $deliver->initStream($composeMessage,$sendmail_path);
1570 global $draft_folder;
1571 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1572 $imap_stream = sqimap_login($username, false, $imapServerAddress,
1574 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
1575 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1576 $imap_deliver = new Deliver_IMAP();
1577 $length = $imap_deliver->mail($composeMessage);
1578 sqimap_append ($imap_stream, $draft_folder, $length);
1579 $imap_deliver->mail($composeMessage, $imap_stream);
1580 sqimap_append_done ($imap_stream, $draft_folder);
1581 sqimap_logout($imap_stream);
1582 unset ($imap_deliver);
1583 $composeMessage->purgeAttachments();
1586 $msg = '<br />'.sprintf(_("Error: Draft folder %s does not exist."), htmlspecialchars($draft_folder));
1587 plain_error_message($msg);
1593 $length = $deliver->mail($composeMessage, $stream);
1594 $success = $deliver->finalizeStream($stream);
1597 // $deliver->dlv_server_msg is not always server's reply
1598 $msg = _("Message not sent.") . "<br />\n" .
1600 if (!empty($deliver->dlv_server_msg
)) {
1601 // add 'server replied' part only when it is not empty.
1602 // Delivery error can be generated by delivery class itself
1604 _("Server replied:") . ' ' . $deliver->dlv_ret_nr
. ' ' .
1605 nl2br(htmlspecialchars($deliver->dlv_server_msg
));
1607 plain_error_message($msg);
1610 $move_to_sent = getPref($data_dir,$username,'move_to_sent');
1611 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
1613 /* Move to sent code */
1614 if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
1615 $svr_allow_sent = true;
1617 $svr_allow_sent = false;
1620 if (isset($sent_folder) && (($sent_folder != '') ||
($sent_folder != 'none'))
1621 && sqimap_mailbox_exists( $imap_stream, $sent_folder)) {
1627 if ((isset($move_to_sent) && ($move_to_sent != 0)) ||
(!isset($move_to_sent))) {
1628 $lcl_allow_sent = true;
1630 $lcl_allow_sent = false;
1633 if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) ||
($fld_sent && $lcl_allow_sent)) {
1634 global $passed_id, $mailbox, $action;
1635 if ($action == 'reply' ||
$action == 'reply_all') {
1636 $save_reply_with_orig=getPref($data_dir,$username,'save_reply_with_orig');
1637 if ($save_reply_with_orig) {
1638 $sent_folder = $mailbox;
1641 sqimap_append ($imap_stream, $sent_folder, $length);
1642 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1643 $imap_deliver = new Deliver_IMAP();
1644 $imap_deliver->mail($composeMessage, $imap_stream);
1645 sqimap_append_done ($imap_stream, $sent_folder);
1646 unset ($imap_deliver);
1649 global $passed_id, $mailbox, $action, $what, $iAccount,$startMessage;
1651 $composeMessage->purgeAttachments();
1652 if ($action=='reply' ||
$action=='reply_all' ||
$action=='forward' ||
$action=='forward_as_attachment') {
1653 require(SM_PATH
. 'functions/mailbox_display.php');
1654 $aMailbox = sqm_api_mailbox_select($imap_stream, $iAccount, $mailbox,array('setindex' => $what, 'offset' => $startMessage),array());
1658 // check if we are allowed to set the \\Answered flag
1659 if (in_array('\\answered',$aMailbox['PERMANENTFLAGS'], true)) {
1660 $aUpdatedMsgs = sqimap_toggle_flag($imap_stream, array($passed_id), '\\Answered', true, false);
1661 if (isset($aUpdatedMsgs[$passed_id]['FLAGS'])) {
1663 * Only update the cached headers if the header is
1666 if (isset($aMailbox['MSG_HEADERS'][$passed_id])) {
1667 $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'] = $aMsg['FLAGS'];
1673 case 'forward_as_attachment':
1674 // check if we are allowed to set the $Forwarded flag (RFC 4550 paragraph 2.8)
1675 if (in_array('$forwarded',$aMailbox['PERMANENTFLAGS'], true) ||
1676 in_array('\\*',$aMailbox['PERMANENTFLAGS'])) {
1678 $aUpdatedMsgs = sqimap_toggle_flag($imap_stream, array($passed_id), '$Forwarded', true, false);
1679 if (isset($aUpdatedMsgs[$passed_id]['FLAGS'])) {
1680 if (isset($aMailbox['MSG_HEADERS'][$passed_id])) {
1681 $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'] = $aMsg['FLAGS'];
1689 * Write mailbox with updated seen flag information back to cache.
1691 if(isset($aUpdatedMsgs[$passed_id])) {
1692 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
1693 sqsession_register($mailbox_cache,'mailbox_cache');
1696 sqimap_logout($imap_stream);