27a8ccf091690247eda290d1f3292b09e1320f6a
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This code sends a mail.
11 * There are 4 modes of operation:
20 /* Path for SquirrelMail required files. */
21 define('SM_PATH','../');
23 /* SquirrelMail required files. */
24 require_once(SM_PATH
. 'include/validate.php');
25 require_once(SM_PATH
. 'functions/imap.php');
26 require_once(SM_PATH
. 'functions/date.php');
27 require_once(SM_PATH
. 'functions/mime.php');
28 require_once(SM_PATH
. 'functions/plugin.php');
29 require_once(SM_PATH
. 'functions/display_messages.php');
30 require_once(SM_PATH
. 'class/deliver/Deliver.class.php');
32 /* --------------------- Get globals ------------------------------------- */
33 $username = $_SESSION['username'];
34 $onetimepad = $_SESSION['onetimepad'];
35 $base_uri = $_SESSION['base_uri'];
36 $delimiter = $_SESSION['delimiter'];
38 if (isset($_POST['return'])) {
39 $html_addr_search_done = 'Use Addresses';
41 if ( isset($_SESSION['composesession']) ) {
42 $composesession = $_SESSION['composesession'];
44 sqextractGlobalVar('action');
45 sqextractGlobalVar('session');
46 sqextractGlobalVar('mailbox');
47 sqextractGlobalVar('identity');
48 sqextractGlobalVar('send_to');
49 sqextractGlobalVar('send_to_cc');
50 sqextractGlobalVar('send_to_bcc');
51 sqextractGlobalVar('subject');
52 sqextractGlobalVar('body');
53 sqextractGlobalVar('mailprio');
54 sqextractGlobalVar('request_mdn');
55 sqextractGlobalVar('request_dr');
56 sqextractGlobalVar('html_addr_search');
57 sqextractGlobalVar('mail_sent');
58 sqextractGlobalVar('passed_id');
59 sqextractGlobalVar('passed_ent_id');
60 sqextractGlobalVar('send');
62 if ( isset($_POST['sigappend']) ) {
63 $sigappend = $_POST['sigappend'];
65 /* From addressbook search */
66 if ( isset($_POST['from_htmladdr_search']) ) {
67 $from_htmladdr_search = $_POST['from_htmladdr_search'];
69 if ( isset($_POST['addr_search_done']) ) {
70 $html_addr_search_done = $_POST['addr_search_done'];
72 if ( isset($_POST['send_to_search']) ) {
73 $send_to_search = &$_POST['send_to_search'];
77 sqextractGlobalVar('attach');
78 if ( isset($_POST['do_delete']) ) {
79 $do_delete = $_POST['do_delete'];
81 if ( isset($_POST['delete']) ) {
82 $delete = &$_POST['delete'];
84 if ( isset($_SESSION['compose_messages']) ) {
85 $compose_messages = &$_SESSION['compose_messages'];
89 /* Forward message as attachment */
90 if ( isset($_GET['attachedmessages']) ) {
91 $attachedmessages = $_GET['attachedmessages'];
95 sqextractGlobalVar('draft');
96 sqextractGlobalVar('draft_id');
97 sqextractGlobalVar('ent_num');
98 sqextractGlobalVar('saved_draft');
99 sqextractGlobalVar('delete_draft');
101 $key = $_COOKIE['key'];
103 /* --------------------- Specific Functions ------------------------------ */
105 function replyAllString($header) {
106 global $include_self_reply_all, $username, $data_dir;
109 * 1) Remove the addresses we'll be sending the message 'to'
111 $url_replytoall_avoid_addrs = '';
112 if (isset($header->replyto
)) {
113 $excl_ar = $header->getAddr_a('replyto');
116 * 2) Remove our identities from the CC list (they still can be in the
117 * TO list) only if $include_self_reply_all is turned off
119 if (!$include_self_reply_all) {
120 $email_address = trim(getPref($data_dir, $username, 'email_address'));
121 $excl_ar[$email_address] = '';
123 $idents = getPref($data_dir, $username, 'identities');
124 if ($idents != '' && $idents > 1) {
125 for ($i = 1; $i < $idents; $i ++
) {
126 $cur_email_address = getPref($data_dir, $username,
127 'email_address' . $i);
128 $cur_email_address = strtolower($cur_email_address);
129 $excl_ar[$cur_email_address] = '';
135 * 3) get the addresses.
137 $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar);
140 * 4) generate the string.
142 $url_replytoallcc = '';
143 foreach( $url_replytoall_ar as $email => $personal) {
145 $url_replytoallcc .= ", \"$personal\" <$email>";
147 $url_replytoallcc .= ', '. $email;
150 $url_replytoallcc = substr($url_replytoallcc,2);
151 return $url_replytoallcc;
154 function getforwardHeader($orig_header) {
157 $display = array( _("Subject") => strlen(_("Subject")),
158 _("From") => strlen(_("From")),
159 _("Date") => strlen(_("Date")),
160 _("To") => strlen(_("To")),
161 _("Cc") => strlen(_("Cc")) );
162 $maxsize = max($display);
163 $indent = str_pad('',$maxsize+
2);
164 foreach($display as $key => $val) {
165 $display[$key] = $key .': '. str_pad('', $maxsize - $val);
167 $bodyTop = str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH
) .
168 "\n". $display[_("Subject")] .
169 decodeHeader($orig_header->subject
) . "\n" .
170 $display[_("From")] .
171 decodeHeader($orig_header->getAddr_s('from',"\n$indent")) . "\n" .
172 $display[_("Date")] .
173 getLongDateString( $orig_header->date
). "\n" .
175 decodeHeader($orig_header->getAddr_s('to',"\n$indent")) ."\n";
176 if ($orig_header->cc
!= array() && $orig_header->cc
!='') {
177 $bodyTop .= $display[_("Cc")] . decodeHeader($orig_header->getAddr_s('cc',"\n$indent")) . "\n";
179 $bodyTop .= str_pad('', $editor_size -2 , '-') .
183 /* ----------------------------------------------------------------------- */
185 /* OLD: should be adapted to composeMessage */
188 * If the session is expired during a post this restores the compose session
191 //$session_expired = false;
192 sqsession_unregister('session_expired_post');
193 if (false && sqsession_is_registered('session_expired_post')) {
194 global $session_expired_post, $session_expired;
196 * extra check for username so we don't display previous post data from
197 * another user during this session.
199 if ($session_expired_post['username'] != $username) {
200 sqsession_unregister('session_expired_post');
201 sqsession_unregister('session_expired');
203 foreach ($session_expired_post as $postvar => $val) {
213 $session_expired = true;
215 sqsession_unregister('session_expired_post');
216 sqsession_unregister('session_expired');
217 if (!isset($mailbox)) {
221 if ($compose_new_win == '1') {
222 compose_Header($color, $mailbox);
224 displayPageHeader($color, $mailbox);
226 showInputForm($session, false);
229 if (!isset($composesession)) {
231 sqsession_register(0,'composesession');
234 if (!isset($session) ||
(isset($newmessage) && $newmessage)) {
235 sqsession_unregister('composesession');
236 $session = "$composesession" +
1;
237 $composesession = $session;
238 sqsession_register($composesession,'composesession');
241 if (!isset($compose_messages)) {
242 $compose_messages = array();
244 if (!isset($compose_messages[$session]) ||
($compose_messages[$session] == NULL)) {
245 /* if (!array_key_exists($session, $compose_messages)) { /* We can only do this in PHP >= 4.1 */
246 $composeMessage = new Message();
247 $rfc822_header = new Rfc822Header();
248 $composeMessage->rfc822_header
= $rfc822_header;
249 $composeMessage->reply_rfc822_header
= '';
250 $compose_messages[$session] = $composeMessage;
251 sqsession_register($compose_messages,'compose_messages');
253 $composeMessage=$compose_messages[$session];
256 if (!isset($mailbox) ||
$mailbox == '' ||
($mailbox == 'None')) {
262 * Set $default_charset to correspond with the user's selection
263 * of language interface.
266 $composeMessage=$compose_messages[$session];
267 if (! sendMessage($composeMessage, true)) {
268 showInputForm($session);
271 $draft_message = _("Draft Email Saved");
272 /* If this is a resumed draft, then delete the original */
273 if(isset($delete_draft)) {
274 Header("Location: delete_message.php?mailbox=" . urlencode($draft_folder) .
275 "&message=$delete_draft&sort=$sort&startMessage=1&saved_draft=yes");
279 if ($compose_new_win == '1') {
280 Header("Location: compose.php?saved_draft=yes&session=$composesession");
284 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort".
285 "&startMessage=1¬e=".urlencode($draft_message));
293 if (isset($_FILES['attachfile']) &&
294 $_FILES['attachfile']['tmp_name'] &&
295 $_FILES['attachfile']['tmp_name'] != 'none') {
296 $AttachFailure = saveAttachedFiles($session);
298 if (checkInput(false) && !isset($AttachFailure)) {
299 $urlMailbox = urlencode (trim($mailbox));
300 if (! isset($passed_id)) {
304 * Set $default_charset to correspond with the user's selection
305 * of language interface.
309 * This is to change all newlines to \n
310 * We'll change them to \r\n later (in the sendMessage function)
312 $body = str_replace("\r\n", "\n", $body);
313 $body = str_replace("\r", "\n", $body);
316 * Rewrap $body so that no line is bigger than $editor_size
317 * This should only really kick in the sqWordWrap function
318 * if the browser doesn't support "VIRTUAL" as the wrap type.
320 $body = explode("\n", $body);
322 foreach ($body as $line) {
323 if( $line <> '-- ' ) {
324 $line = rtrim($line);
326 if (strlen($line) <= $editor_size +
1) {
327 $newBody .= $line . "\n";
329 sqWordWrap($line, $editor_size);
330 $newBody .= $line . "\n";
334 do_hook('compose_send');
335 $composeMessage=$compose_messages[$session];
337 $Result = sendMessage($composeMessage);
339 showInputForm($session);
342 if ( isset($delete_draft)) {
343 Header("Location: delete_message.php?mailbox=" . urlencode( $draft_folder ).
344 "&message=$delete_draft&sort=$sort&startMessage=1&mail_sent=yes");
347 if ($compose_new_win == '1') {
348 Header("Location: compose.php?mail_sent=yes");
351 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort".
355 if ($compose_new_win == '1') {
356 compose_Header($color, $mailbox);
359 displayPageHeader($color, $mailbox);
361 if (isset($AttachFailure)) {
362 plain_error_message(_("Could not move/copy file. File not attached"),
366 showInputForm($session);
367 /* sqimap_logout($imapConnection); */
369 } elseif (isset($html_addr_search_done)) {
370 if ($compose_new_win == '1') {
371 compose_Header($color, $mailbox);
374 displayPageHeader($color, $mailbox);
377 if (isset($send_to_search) && is_array($send_to_search)) {
378 foreach ($send_to_search as $k => $v) {
379 if (substr($k, 0, 1) == 'T') {
385 elseif (substr($k, 0, 1) == 'C') {
391 elseif (substr($k, 0, 1) == 'B') {
393 $send_to_bcc .= ', ';
399 showInputForm($session);
400 } elseif (isset($html_addr_search)) {
401 if (isset($_FILES['attachfile']) &&
402 $_FILES['attachfile']['tmp_name'] &&
403 $_FILES['attachfile']['tmp_name'] != 'none') {
404 if(saveAttachedFiles($session)) {
405 plain_error_message(_("Could not move/copy file. File not attached"), $color);
409 * I am using an include so as to elminiate an extra unnecessary
410 * click. If you can think of a better way, please implement it.
412 include_once('./addrbook_search_html.php');
413 } elseif (isset($attach)) {
414 if (saveAttachedFiles($session)) {
415 plain_error_message(_("Could not move/copy file. File not attached"), $color);
417 if ($compose_new_win == '1') {
418 compose_Header($color, $mailbox);
421 displayPageHeader($color, $mailbox);
423 showInputForm($session);
425 elseif (isset($sigappend)) {
426 $idents = getPref($data_dir, $username, 'identities', 0);
428 if ($identity == 'default') {
433 $signature = getSig($data_dir, $username, $no);
435 $body .= "\n\n".($prefix_sig==true?
"-- \n":'').$signature;
436 if ($compose_new_win == '1') {
437 compose_Header($color, $mailbox);
439 displayPageHeader($color, $mailbox);
441 showInputForm($session);
442 } elseif (isset($do_delete)) {
443 if ($compose_new_win == '1') {
444 compose_Header($color, $mailbox);
447 displayPageHeader($color, $mailbox);
450 if (isset($delete) && is_array($delete)) {
451 $composeMessage = $compose_messages[$session];
452 foreach($delete as $index) {
453 $attached_file = $composeMessage->entities
[$index]->att_local_name
;
454 unlink ($attached_file);
455 unset ($composeMessage->entities
[$index]);
457 $new_entities = array();
458 foreach ($composeMessage->entities
as $entity) {
459 $new_entities[] = $entity;
461 $composeMessage->entities
= $new_entities;
462 $compose_messages[$session] = $composeMessage;
463 sqsession_register($compose_messages, 'compose_messages');
464 // setPref($data_dir, $username, 'attachments', serialize($attachments));
466 showInputForm($session);
469 * This handles the default case as well as the error case
470 * (they had the same code) --> if (isset($smtpErrors))
473 if ($compose_new_win == '1') {
474 compose_Header($color, $mailbox);
476 displayPageHeader($color, $mailbox);
481 if (!isset($passed_ent_id)) {
484 if (!isset($passed_id)) {
487 if (!isset($mailbox)) {
490 if (!isset($action)) {
494 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
496 /* in case the origin is not read_body.php */
497 if (isset($send_to)) {
498 $values['send_to'] = $send_to;
500 if (isset($send_to_cc)) {
501 $values['send_to_cc'] = $send_to_cc;
503 if (isset($send_to_bcc)) {
504 $values['send_to_bcc'] = $send_to_bcc;
506 showInputForm($session, $values);
511 /**************** Only function definitions go below *************/
514 /* This function is used when not sending or adding attachments */
515 function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
516 global $editor_size, $default_use_priority, $body,
517 $use_signature, $composesession, $data_dir, $username,
518 $username, $key, $imapServerAddress, $imapPort, $compose_messages,
521 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
525 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
528 sqimap_mailbox_select($imapConnection, $mailbox);
529 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
532 if ($passed_ent_id) {
533 /* redefine the messsage in case of message/rfc822 */
534 $message = $message->getEntity($passed_ent_id);
535 /* message is an entity which contains the envelope and type0=message
536 * and type1=rfc822. The actual entities are childs from
537 * $message->entities[0]. That's where the encoding and is located
540 $entities = $message->entities
[0]->findDisplayEntity
541 (array(), $alt_order = array('text/plain'));
542 if (!count($entities)) {
543 $entities = $message->entities
[0]->findDisplayEntity
544 (array(), $alt_order = array('text/plain','html/plain'));
546 $orig_header = $message->rfc822_header
; /* here is the envelope located */
547 /* redefine the message for picking up the attachments */
548 $message = $message->entities
[0];
551 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
552 if (!count($entities)) {
553 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
555 $orig_header = $message->rfc822_header
;
558 $encoding = $message->header
->encoding
;
559 $type0 = $message->type0
;
560 $type1 = $message->type1
;
561 foreach ($entities as $ent) {
562 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
563 $body_part_entity = $message->getEntity($ent);
564 $bodypart = decodeBody($unencoded_bodypart,
565 $body_part_entity->header
->encoding
);
566 if ($type1 == 'html') {
567 $bodypart = strip_tags($bodypart);
571 if ($default_use_priority) {
572 $mailprio = substr($orig_header->priority
,0,1);
579 //ClearAttachments($session);
582 $idents = getPref($data_dir, $username, 'identities');
583 $from_o = $orig_header->from
;
584 if (is_object($from_o)) {
585 $orig_from = $from_o->getAddress();
589 if (!empty($idents) && $idents > 1) {
590 for ($i = 1; $i < $idents; $i++
) {
591 $enc_from_name = '"'.
595 '" <' . getPref($data_dir, $username,
596 'email_address' . $i) . '>';
597 if ($enc_from_name == $orig_from) {
606 $use_signature = FALSE;
607 $send_to = decodeHeader($orig_header->getAddr_s('to'));
608 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'));
609 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'));
610 $subject = decodeHeader($orig_header->subject
);
612 $body_ary = explode("\n", $body);
613 $cnt = count($body_ary) ;
615 for ($i=0; $i < $cnt; $i++
) {
616 if (!ereg("^[>\\s]*$", $body_ary[$i]) ||
!$body_ary[$i]) {
617 sqWordWrap($body_ary[$i], $editor_size );
618 $body .= $body_ary[$i] . "\n";
620 unset($body_ary[$i]);
623 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
625 case ('edit_as_new'):
626 $send_to = decodeHeader($orig_header->getAddr_s('to'));
627 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'));
628 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'));
629 $subject = decodeHeader($orig_header->subject
);
630 $mailprio = $orig_header->priority
;
632 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
637 $subject = decodeHeader($orig_header->subject
);
638 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
639 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
640 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
641 $subject = '[Fwd: ' . $subject . ']';
643 $body = getforwardHeader($orig_header) . $body;
645 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
646 $body = "\n" . $body;
648 case ('forward_as_attachment'):
649 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
653 $send_to_cc = replyAllString($orig_header);
655 $send_to = $orig_header->reply_to
;
656 if (is_object($send_to)) {
657 $send_to = decodeHeader($send_to->getAddr_s('reply_to'));
659 $send_to = decodeHeader($orig_header->getAddr_s('from'));
661 $subject = decodeHeader($orig_header->subject
);
662 $subject = str_replace('"', "'", $subject);
663 $subject = trim($subject);
664 if (substr(strtolower($subject), 0, 3) != 're:') {
665 $subject = 'Re: ' . $subject;
667 /* this corrects some wrapping/quoting problems on replies */
668 $rewrap_body = explode("\n", $body);
669 $from = (is_array($orig_header->from
)) ?
670 $orig_header->from
[0] : $orig_header->from
;
671 $body = getReplyCitation($from->getAddress(false));
673 $cnt = count($rewrap_body);
674 for ($i=0;$i<$cnt;$i++
) {
675 sqWordWrap($rewrap_body[$i], ($editor_size));
676 if (preg_match("/^(>+)/", $rewrap_body[$i], $matches)) {
678 $body .= '>' . str_replace("\n", "\n>$gt ", rtrim($rewrap_body[$i])) ."\n";
680 $body .= '> ' . str_replace("\n", "\n> ", rtrim($rewrap_body[$i])) . "\n";
682 unset($rewrap_body[$i]);
684 $composeMessage->reply_rfc822_header
= $orig_header;
689 $compose_messages[$session] = $composeMessage;
690 sqsession_register($compose_messages, 'compose_messages');
692 sqimap_logout($imapConnection);
694 $ret = array( 'send_to' => $send_to,
695 'send_to_cc' => $send_to_cc,
696 'send_to_bcc' => $send_to_bcc,
697 'subject' => $subject,
698 'mailprio' => $mailprio,
700 'identity' => $identity );
703 } /* function newMail() */
705 function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
706 global $attachment_dir, $username, $data_dir, $squirrelmail_language;
707 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
708 if (!count($message->entities
) ||
709 ($message->type0
== 'message' && $message->type1
== 'rfc822')) {
710 if ( !in_array($message->entity_id
, $entities) && $message->entity_id
) {
711 switch ($message->type0
) {
713 if ($message->type1
== 'rfc822') {
714 $filename = decodeHeader($message->rfc822_header
->subject
.'.eml');
715 if ($filename == "") {
716 $filename = "untitled-".$message->entity_id
.'.eml';
719 $filename = decodeHeader($message->getFilename());
723 $filename = decodeHeader($message->getFilename());
726 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
727 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
728 $filename = $languages[$squirrelmail_language]['XTRA_CODE']('encode', $filename);
731 $localfilename = GenerateRandomString(32, '', 7);
732 $full_localfilename = "$hashed_attachment_dir/$localfilename";
733 while (file_exists($full_localfilename)) {
734 $localfilename = GenerateRandomString(32, '', 7);
735 $full_localfilename = "$hashed_attachment_dir/$localfilename";
737 $message->att_local_name
= $full_localfilename;
738 if (!$message->mime_header
) { /* temporary hack */
739 $message->mime_header
= $message->header
;
742 $composeMessage->addEntity($message);
744 /* Write Attachment to file */
745 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
746 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
747 $passed_id, $message->entity_id
),
748 $message->header
->encoding
));
752 for ($i=0, $entCount=count($message->entities
); $i<$entCount;$i++
) {
753 $composeMessage=getAttachments($message->entities
[$i], $composeMessage, $passed_id, $entities, $imapConnection);
756 // setPref($data_dir, $username, 'attachments', serialize($attachments));
757 return $composeMessage;
760 function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
761 $passed_ent_id='', $imapConnection) {
762 global $attachments, $attachment_dir, $username, $data_dir, $uid_support;
763 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
764 if (!$passed_ent_id) {
765 $body_a = sqimap_run_command($imapConnection,
766 'FETCH '.$passed_id.' RFC822',
767 TRUE, $response, $readmessage,
770 $body_a = sqimap_run_command($imapConnection,
771 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
772 TRUE, $response, $readmessage, $uid_support);
773 $message = $message->parent
;
775 if ($response = 'OK') {
776 $subject = encodeHeader($message->rfc822_header
->subject
);
777 array_shift($body_a);
778 $body = implode('', $body_a) . "\r\n";
780 $localfilename = GenerateRandomString(32, 'FILE', 7);
781 $full_localfilename = "$hashed_attachment_dir/$localfilename";
783 $fp = fopen( $full_localfilename, 'w');
786 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
787 $full_localfilename);
789 return $composeMessage;
792 function showInputForm ($session, $values=false) {
793 global $send_to, $send_to_cc, $body,
794 $passed_body, $color, $use_signature, $signature, $prefix_sig,
795 $editor_size, $attachments, $subject, $newmail,
796 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
797 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
798 $username, $data_dir, $identity, $draft_id, $delete_draft,
799 $mailprio, $default_use_mdn, $mdn_user_support, $compose_new_win,
800 $saved_draft, $mail_sent, $sig_first, $edit_as_new, $action,
801 $username, $compose_messages;
803 $composeMessage = $compose_messages[$session];
805 $subject = decodeHeader($subject, false);
807 $send_to = $values['send_to'];
808 $send_to_cc = $values['send_to_cc'];
809 $send_to_bcc = $values['send_to_bcc'];
810 $subject = $values['subject'];
811 $mailprio = $values['mailprio'];
812 $body = $values['body'];
813 $identity = $values['identity'];
816 if ($use_javascript_addr_book) {
817 echo "\n". '<SCRIPT LANGUAGE=JavaScript><!--' . "\n" .
818 'function open_abook() { ' . "\n" .
819 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
820 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
821 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
822 ' nwin.opener = document.windows;' . "\n" .
824 '// --></SCRIPT>' . "\n\n";
827 echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
828 'ENCTYPE="multipart/form-data"';
829 do_hook("compose_form");
833 if ($action == 'draft') {
834 echo '<input type="hidden" name="delete_draft" value="' . $passed_id . "\">\n";
836 if (isset($delete_draft)) {
837 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
839 if (isset($session)) {
840 echo '<input type="hidden" name="session" value="' . $session . "\">\n";
843 if (isset($passed_id)) {
844 echo '<input type="hidden" name="passed_id" value="' . $passed_id . "\">\n";
847 if ($saved_draft == 'yes') {
848 echo '<BR><CENTER><B>'. _("Draft Saved").'</CENTER></B>';
850 if ($mail_sent == 'yes') {
851 echo '<BR><CENTER><B>'. _("Your Message has been sent").'</CENTER></B>';
853 echo '<TABLE ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
854 if ($compose_new_win == '1') {
855 echo '<TABLE ALIGN=CENTER BGCOLOR="'.$color[0].'" WIDTH="100%" BORDER=0>'."\n" .
856 ' <TR><TD></TD><TD ALIGN="RIGHT"><INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'></TD></TR>'."\n";
858 if ($location_of_buttons == 'top') {
859 showComposeButtonRow();
862 $idents = getPref($data_dir, $username, 'identities', 0);
864 echo ' <TR>' . "\n" .
865 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
868 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
869 '<select name=identity>' . "\n" .
870 '<option value=default>' .
871 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
872 $em = getPref($data_dir, $username, 'email_address');
874 echo htmlspecialchars(' <' . $em . '>') . "\n";
876 for ($i = 1; $i < $idents; $i ++
) {
877 echo '<option value="' . $i . '"';
878 if (isset($identity) && $identity == $i) {
881 echo '>' . htmlspecialchars(getPref($data_dir, $username,
883 $em = getPref($data_dir, $username, 'email_address' . $i);
885 echo htmlspecialchars(' <' . $em . '>') . "\n";
889 echo '</select>' . "\n" .
893 echo ' <TR>' . "\n" .
894 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' . "\n" .
896 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
897 ' <INPUT TYPE=text NAME="send_to" VALUE="' .
898 htmlspecialchars($send_to) . '" SIZE=60><BR>' . "\n" .
902 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
904 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
905 ' <INPUT TYPE=text NAME="send_to_cc" SIZE=60 VALUE="' .
906 htmlspecialchars($send_to_cc) . '"><BR>' . "\n" .
910 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
912 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
913 ' <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
914 htmlspecialchars($send_to_bcc) . '" SIZE=60><BR>' . "\n" .
915 '</TD></TR>' . "\n" .
917 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
919 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n";
920 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
921 htmlspecialchars($subject) . '">';
922 echo '</td></tr>' . "\n\n";
924 if ($location_of_buttons == 'between') {
925 showComposeButtonRow();
928 if ($compose_new_win == '1') {
929 echo ' <TR>' . "\n" .
930 ' <TD BGCOLOR="' . $color[0] . '" COLSPAN=2 ALIGN=CENTER>' . "\n" .
931 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
932 $editor_size . '" WRAP="VIRTUAL">';
935 echo ' <TR>' . "\n" .
936 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
937 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
938 $editor_size . '" WRAP="VIRTUAL">';
940 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
941 if ($sig_first == '1') {
942 if ($charset == 'iso-2022-jp') {
943 echo "\n\n".($prefix_sig==true?
"-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
945 echo "\n\n".($prefix_sig==true?
"-- \n":'').htmlspecialchars($signature);
947 echo "\n\n".htmlspecialchars($body);
950 echo "\n\n".htmlspecialchars($body);
951 if ($charset == 'iso-2022-jp') {
952 echo "\n\n".($prefix_sig==true?
"-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
954 echo "\n\n".($prefix_sig==true?
"-- \n":'').htmlspecialchars($signature);
959 echo htmlspecialchars($body);
961 echo '</TEXTAREA><BR>' . "\n" .
965 if ($location_of_buttons == 'bottom') {
966 showComposeButtonRow();
968 echo ' <TR><TD COLSPAN=2 ALIGN=RIGHT>' .
969 ' <INPUT TYPE=SUBMIT NAME=send VALUE="' . _("Send") . '">' .
970 ' <BR><BR>' .
971 ' </TD></TR>' . "\n";
974 /* This code is for attachments */
975 echo '<table width="100%" cellpadding="0" cellspacing="4" align="center" border="0">' .
977 ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">' .
979 ' <table width="100%" cellpadding="3" cellspacing="0" align="center" border="0">' .
981 ' <TD VALIGN=MIDDLE ALIGN=RIGHT>' . "\n" .
984 ' <TD VALIGN=MIDDLE ALIGN=LEFT>' . "\n" .
985 ' <INPUT NAME="attachfile" SIZE=48 TYPE="file">' . "\n" .
986 ' <input type="submit" name="attach"' .
987 ' value="' . _("Add") .'">' . "\n" .
993 if ($composeMessage->entities
) {
994 foreach ($composeMessage->entities
as $key => $attachment) {
995 $attached_file = $attachment->att_local_name
;
996 if ($attachment->att_local_name ||
$attachment->body_part
) {
997 $attached_filename = decodeHeader($attachment->mime_header
->getParameter('name'));
998 $type = $attachment->mime_header
->type0
.'/'.
999 $attachment->mime_header
->type1
;
1000 $s_a[] = '<input type="checkbox" name="delete[]" value="' .
1001 $key . "\">\n" . $attached_filename . ' - ' . $type .
1002 ' ('.show_readable_size( filesize( $attached_file ) )
1008 foreach ($s_a as $s) {
1009 echo '<tr><td align=left colspan="2" bgcolor="' . $color[0] . '">'.$s.'</td></tr>';
1011 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
1012 _("Delete selected attachments") . "\">\n" .
1015 echo ' </table></td></tr>' .
1019 /* End of attachment code */
1020 if ($compose_new_win == '1') {
1021 echo '</TABLE>'."\n";
1023 echo '</TABLE>' . "\n" .
1024 '<input type="hidden" name="username" value="'. $username . "\">\n" .
1025 '<input type=hidden name=action value=' . $action . ">\n" .
1026 '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
1029 do_hook('compose_bottom');
1030 echo '</BODY></HTML>' . "\n";
1034 function showComposeButtonRow() {
1035 global $use_javascript_addr_book, $save_as_draft,
1036 $default_use_priority, $mailprio, $default_use_mdn,
1037 $request_mdn, $request_dr,
1038 $data_dir, $username;
1040 echo " <TR><TD>\n</TD><TD>\n";
1041 if ($default_use_priority) {
1042 if(!isset($mailprio)) {
1045 echo _("Priority") .': <select name="mailprio">'.
1046 '<option value="1"'.($mailprio=='1'?
' selected':'').'>'. _("High") .'</option>'.
1047 '<option value="3"'.($mailprio=='3'?
' selected':'').'>'. _("Normal") .'</option>'.
1048 '<option value="5"'.($mailprio=='5'?
' selected':'').'>'. _("Low").'</option>'.
1051 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1052 if ($default_use_mdn) {
1053 if ($mdn_user_support) {
1054 echo "\n\t". _("Receipt") .': '.
1055 '<input type="checkbox" name="request_mdn" value=1'.
1056 ($request_mdn=='1'?
' checked':'') .'>'. _("On Read").
1057 ' <input type="checkbox" name="request_dr" value=1'.
1058 ($request_dr=='1'?
' checked':'') .'>'. _("On Delivery");
1062 echo " </td></tr>\n <TR><td>\n </td><td>\n" .
1063 "\n <INPUT TYPE=SUBMIT NAME=\"sigappend\" VALUE=\"". _("Signature") . "\">\n";
1064 if ($use_javascript_addr_book) {
1065 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"".
1066 " <input type=button value=\\\""._("Addresses").
1067 "\\\" onclick='javascript:open_abook();'>\");".
1068 " // --></SCRIPT><NOSCRIPT>\n".
1069 " <input type=submit name=\"html_addr_search\" value=\"".
1070 _("Addresses")."\">".
1073 echo " <input type=submit name=\"html_addr_search\" value=\"".
1074 _("Addresses")."\">";
1077 if ($save_as_draft) {
1078 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
1081 echo '<INPUT TYPE=submit NAME=send VALUE="'. _("Send") . "\">\n";
1082 do_hook('compose_button_row');
1084 echo " </TD></TR>\n\n";
1087 function checkInput ($show) {
1089 * I implemented the $show variable because the error messages
1090 * were getting sent before the page header. So, I check once
1091 * using $show=false, and then when i'm ready to display the error
1092 * message, show=true
1094 global $body, $send_to, $send_to_bcc, $subject, $color;
1096 if ($send_to == '' && $send_to_bcc == '') {
1098 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
1103 } /* function checkInput() */
1106 /* True if FAILURE */
1107 function saveAttachedFiles($session) {
1108 global $_FILES, $attachment_dir, $attachments, $username,
1109 $data_dir, $compose_messages;
1111 /* get out of here if no file was attached at all */
1112 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1116 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1117 $localfilename = GenerateRandomString(32, '', 7);
1118 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1119 while (file_exists($full_localfilename)) {
1120 $localfilename = GenerateRandomString(32, '', 7);
1121 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1124 if (!@rename
($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1125 if (function_exists("move_uploaded_file")) {
1126 if (!@move_uploaded_file
($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
1130 if (!@copy
($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1135 $message = $compose_messages[$session];
1136 $type = strtolower($_FILES['attachfile']['type']);
1137 $name = $_FILES['attachfile']['name'];
1138 $message->initAttachment($type, $name, $full_localfilename);
1139 $compose_messages[$session] = $message;
1142 function ClearAttachments($composeMessage) {
1143 if ($composeMessage->att_local_name
) {
1144 $attached_file = $composeMessage->att_local_name
;
1145 if (file_exists($attached_file)) {
1146 unlink($attached_file);
1149 for ($i=0, $entCount=count($composeMessage->entities
);$i< $entCount; ++
$i) {
1150 ClearAttachments($composeMessage->entities
[$i]);
1156 function getReplyCitation($orig_from) {
1157 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
1159 /* First, return an empty string when no citation style selected. */
1160 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
1164 /* Make sure our final value isn't an empty string. */
1165 if ($orig_from == '') {
1169 /* Otherwise, try to select the desired citation style. */
1170 switch ($reply_citation_style) {
1173 $end = ' ' . _("said") . ':';
1176 $start = '<' . _("quote") . ' ' . _("who") . '="';
1179 case 'user-defined':
1180 $start = $reply_citation_start .
1181 ($reply_citation_start == '' ?
'' : ' ');
1182 $end = $reply_citation_end;
1188 /* Build and return the citation string. */
1189 return ($start . $orig_from . $end . "\n");
1192 /* temporary function to make use of the deliver class.
1193 In the future the responsable backend should be automaticly loaded
1194 and conf.pl should show a list of available backends.
1195 The message also should be constructed by the message class.
1198 function sendMessage($composeMessage, $draft=false) {
1199 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
1200 $username, $popuser, $usernamedata, $identity, $data_dir,
1201 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
1203 global $imapServerAddress, $imapPort, $sent_folder, $key;
1205 $rfc822_header = $composeMessage->rfc822_header
;
1206 $rfc822_header->to
= $rfc822_header->parseAddress($send_to,true, array(), '', $domain);
1207 $rfc822_header->cc
= $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain);
1208 $rfc822_header->bcc
= $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain);
1209 $rfc822_header->priority
= $mailprio;
1210 $rfc822_header->subject
= $subject;
1211 $special_encoding='';
1212 if (strtolower($default_charset) == 'iso-2022-jp') {
1213 if (mb_detect_encoding($body) == 'ASCII') {
1214 $special_encoding = '8bit';
1216 $body = mb_convert_encoding($body, 'JIS');
1217 $special_encoding = '7bit';
1220 $composeMessage->setBody($body);
1222 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
1223 $popuser = $usernamedata[1];
1224 $domain = $usernamedata[2];
1225 unset($usernamedata);
1227 $popuser = $username;
1230 if (isset($identity) && $identity != 'default') {
1231 $from_mail = getPref($data_dir, $username,'email_address' . $identity);
1232 $full_name = getPref($data_dir, $username,'full_name' . $identity);
1233 $reply_to = getPref($data_dir, $username,'reply_to' . $identity);
1235 $from_mail = getPref($data_dir, $username, 'email_address');
1236 $full_name = getPref($data_dir, $username, 'full_name');
1237 $reply_to = getPref($data_dir, $username,'reply_to');
1240 $from_mail = "$popuser@$domain";
1243 $rfc822_header->from
= $rfc822_header->parseAddress($from_mail,true);
1245 $from = $rfc822_header->from
[0];
1246 if (!$from->host
) $from->host
= $domain;
1247 $from_addr = $full_name .' <'.$from->mailbox
.'@'.$from->host
.'>';
1248 $rfc822_header->from
= $rfc822_header->parseAddress($from_addr,true);
1251 $rfc822_header->reply_to
= $rfc822_header->parseAddress($reply_to,true);
1253 /* Receipt: On Read */
1254 if (isset($request_mdn) && $request_mdn) {
1255 $rfc822_header->dnt
= $rfc822_header->parseAddress($from_mail,true);
1257 /* Receipt: On Delivery */
1258 if (isset($request_dr) && $request_dr) {
1259 $rfc822_header->more_headers
['Return-Receipt-To'] = $from_mail;
1261 /* multipart messages */
1262 if (count($composeMessage->entities
)) {
1263 $message_body = new Message();
1264 $message_body->body_part
= $composeMessage->body_part
;
1265 $composeMessage->body_part
= '';
1266 $mime_header = new MessageHeader
;
1267 $mime_header->type0
= 'text';
1268 $mime_header->type1
= 'plain';
1269 if ($special_encoding) {
1270 $mime_header->encoding
= $special_encoding;
1272 $mime_header->encoding
= 'us-ascii';
1274 if ($default_charset) {
1275 $mime_header->parameters
['charset'] = $default_charset;
1277 $message_body->mime_header
= $mime_header;
1278 array_unshift($composeMessage->entities
, $message_body);
1279 $content_type = new ContentType('multipart/mixed');
1281 $content_type = new ContentType('text/plain');
1283 if ($default_charset) {
1284 $content_type->properties
['charset']=$default_charset;
1287 $rfc822_header->content_type
= $content_type;
1288 $composeMessage->rfc822_header
= $rfc822_header;
1290 if (!$useSendmail && !$draft) {
1291 require_once(SM_PATH
. 'class/deliver/Deliver_SMTP.class.php');
1292 $deliver = new Deliver_SMTP();
1293 global $smtpServerAddress, $smtpPort, $use_authenticated_smtp, $pop_before_smtp;
1294 if ($use_authenticated_smtp) {
1295 global $key, $onetimepad;
1297 $pass = OneTimePadDecrypt($key, $onetimepad);
1302 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ?
true : false;
1303 $stream = $deliver->initStream($composeMessage,$domain,0,
1304 $smtpServerAddress, $smtpPort, $authPop);
1305 } elseif (!$draft) {
1306 require_once(SM_PATH
. 'class/deliver/Deliver_SendMail.class.php');
1307 global $sendmail_path;
1308 $deliver = new Deliver_SendMail();
1309 $stream = $deliver->initStream($composeMessage,$sendmail_path);
1311 global $draft_folder;
1312 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1313 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1315 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
1316 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1317 $imap_deliver = new Deliver_IMAP();
1318 $length = $imap_deliver->mail($composeMessage);
1319 sqimap_append ($imap_stream, $draft_folder, $length);
1320 $imap_deliver->mail($composeMessage, $imap_stream);
1321 sqimap_append_done ($imap_stream);
1322 sqimap_logout($imap_stream);
1323 unset ($imap_deliver);
1326 $msg = '<br>Error: '._("Draft folder")." $draft_folder" . ' does not exist.';
1327 plain_error_message($msg, $color);
1333 $length = $deliver->mail($composeMessage, $stream);
1334 $succes = $deliver->finalizeStream($stream);
1337 $msg = $deliver->dlv_msg
. '<br>Server replied: '.$deliver->dlv_ret_nr
;
1338 plain_error_message($msg, $color);
1341 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1343 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
1344 sqimap_append ($imap_stream, $sent_folder, $length);
1345 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1346 $imap_deliver = new Deliver_IMAP();
1347 $imap_deliver->mail($composeMessage, $imap_stream);
1348 sqimap_append_done ($imap_stream);
1349 unset ($imap_deliver);
1351 global $passed_id, $mailbox, $action;
1352 ClearAttachments($composeMessage);
1353 if ($action == 'reply' ||
$action == 'reply_all') {
1354 sqimap_mailbox_select ($imap_stream, $mailbox);
1355 sqimap_messages_flag ($imap_stream, $passed_id, $passed_id, 'Answered', true);
1357 sqimap_logout($imap_stream);