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');
31 require_once(SM_PATH
. 'functions/addressbook.php');
33 /* --------------------- Get globals ------------------------------------- */
34 $username = $_SESSION['username'];
35 $onetimepad = $_SESSION['onetimepad'];
36 $base_uri = $_SESSION['base_uri'];
37 $delimiter = $_SESSION['delimiter'];
39 if (isset($_POST['return'])) {
40 $html_addr_search_done = 'Use Addresses';
42 if ( isset($_SESSION['composesession']) ) {
43 $composesession = $_SESSION['composesession'];
45 sqextractGlobalVar('action');
46 sqextractGlobalVar('session');
47 sqextractGlobalVar('mailbox');
48 sqextractGlobalVar('identity');
49 sqextractGlobalVar('send_to');
50 sqextractGlobalVar('send_to_cc');
51 sqextractGlobalVar('send_to_bcc');
52 sqextractGlobalVar('subject');
53 sqextractGlobalVar('body');
54 sqextractGlobalVar('mailprio');
55 sqextractGlobalVar('request_mdn');
56 sqextractGlobalVar('request_dr');
57 sqextractGlobalVar('html_addr_search');
58 sqextractGlobalVar('mail_sent');
59 sqextractGlobalVar('passed_id');
60 sqextractGlobalVar('passed_ent_id');
61 sqextractGlobalVar('send');
63 if ( isset($_POST['sigappend']) ) {
64 $sigappend = $_POST['sigappend'];
66 /* From addressbook search */
67 if ( isset($_POST['from_htmladdr_search']) ) {
68 $from_htmladdr_search = $_POST['from_htmladdr_search'];
70 if ( isset($_POST['addr_search_done']) ) {
71 $html_addr_search_done = $_POST['addr_search_done'];
73 if ( isset($_POST['send_to_search']) ) {
74 $send_to_search = &$_POST['send_to_search'];
78 sqextractGlobalVar('attach');
79 if ( isset($_POST['do_delete']) ) {
80 $do_delete = $_POST['do_delete'];
82 if ( isset($_POST['delete']) ) {
83 $delete = &$_POST['delete'];
85 if ( isset($_SESSION['compose_messages']) ) {
86 $compose_messages = &$_SESSION['compose_messages'];
90 /* Forward message as attachment */
91 if ( isset($_GET['attachedmessages']) ) {
92 $attachedmessages = $_GET['attachedmessages'];
96 sqextractGlobalVar('draft');
97 sqextractGlobalVar('draft_id');
98 sqextractGlobalVar('ent_num');
99 sqextractGlobalVar('saved_draft');
100 sqextractGlobalVar('delete_draft');
102 $key = $_COOKIE['key'];
104 /* --------------------- Specific Functions ------------------------------ */
106 function replyAllString($header) {
107 global $include_self_reply_all, $username, $data_dir;
110 * 1) Remove the addresses we'll be sending the message 'to'
112 $url_replytoall_avoid_addrs = '';
113 if (isset($header->replyto
)) {
114 $excl_ar = $header->getAddr_a('replyto');
117 * 2) Remove our identities from the CC list (they still can be in the
118 * TO list) only if $include_self_reply_all is turned off
120 if (!$include_self_reply_all) {
121 $email_address = strtolower(trim(getPref($data_dir, $username, 'email_address')));
122 $excl_ar[$email_address] = '';
123 $idents = getPref($data_dir, $username, 'identities');
124 if ($idents != '' && $idents > 1) {
126 for ($i = 1; $i < $idents; $i ++
) {
127 $cur_email_address = getPref($data_dir, $username,
128 'email_address' . $i);
129 $cur_email_address = strtolower(trim($cur_email_address));
130 $excl_ar[$cur_email_address] = '';
136 * 3) get the addresses.
138 $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar);
141 * 4) generate the string.
143 $url_replytoallcc = '';
144 foreach( $url_replytoall_ar as $email => $personal) {
146 $url_replytoallcc .= ", \"$personal\" <$email>";
148 $url_replytoallcc .= ', '. $email;
151 $url_replytoallcc = substr($url_replytoallcc,2);
152 return $url_replytoallcc;
155 function getforwardHeader($orig_header) {
158 $display = array( _("Subject") => strlen(_("Subject")),
159 _("From") => strlen(_("From")),
160 _("Date") => strlen(_("Date")),
161 _("To") => strlen(_("To")),
162 _("Cc") => strlen(_("Cc")) );
163 $maxsize = max($display);
164 $indent = str_pad('',$maxsize+
2);
165 foreach($display as $key => $val) {
166 $display[$key] = $key .': '. str_pad('', $maxsize - $val);
168 $bodyTop = str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH
) .
169 "\n". $display[_("Subject")] .
170 decodeHeader($orig_header->subject
) . "\n" .
171 $display[_("From")] .
172 decodeHeader($orig_header->getAddr_s('from',"\n$indent")) . "\n" .
173 $display[_("Date")] .
174 getLongDateString( $orig_header->date
). "\n" .
176 decodeHeader($orig_header->getAddr_s('to',"\n$indent")) ."\n";
177 if ($orig_header->cc
!= array() && $orig_header->cc
!='') {
178 $bodyTop .= $display[_("Cc")] . decodeHeader($orig_header->getAddr_s('cc',"\n$indent")) . "\n";
180 $bodyTop .= str_pad('', $editor_size -2 , '-') .
184 /* ----------------------------------------------------------------------- */
187 * If the session is expired during a post this restores the compose session
190 if (sqsession_is_registered('session_expired_post')) {
191 $session_expired_post = $_SESSION['session_expired_post'];
193 * extra check for username so we don't display previous post data from
194 * another user during this session.
196 if ($session_expired_post['username'] != $username) {
197 unset($session_expired_post);
198 sqsession_unregister('session_expired_post');
199 session_write_close();
201 foreach ($session_expired_post as $postvar => $val) {
208 $compose_messages = unserialize(urldecode($restoremessages));
209 sqsession_register($compose_messages,'compose_messages');
210 sqsession_register($composesession,'composesession');
214 $session_expired = true;
216 unset($session_expired_post);
217 sqsession_unregister('session_expired_post');
218 session_write_close();
219 if (!isset($mailbox)) {
223 if ($compose_new_win == '1') {
224 compose_Header($color, $mailbox);
226 displayPageHeader($color, $mailbox);
228 showInputForm($session, false);
231 if (!isset($composesession)) {
233 sqsession_register(0,'composesession');
236 if (!isset($session) ||
(isset($newmessage) && $newmessage)) {
237 sqsession_unregister('composesession');
238 $session = "$composesession" +
1;
239 $composesession = $session;
240 sqsession_register($composesession,'composesession');
242 if (!isset($compose_messages)) {
243 $compose_messages = array();
245 if (!isset($compose_messages[$session]) ||
($compose_messages[$session] == NULL)) {
246 /* if (!array_key_exists($session, $compose_messages)) { /* We can only do this in PHP >= 4.1 */
247 $composeMessage = new Message();
248 $rfc822_header = new Rfc822Header();
249 $composeMessage->rfc822_header
= $rfc822_header;
250 $composeMessage->reply_rfc822_header
= '';
251 $compose_messages[$session] = $composeMessage;
252 sqsession_register($compose_messages,'compose_messages');
254 $composeMessage=$compose_messages[$session];
257 if (!isset($mailbox) ||
$mailbox == '' ||
($mailbox == 'None')) {
263 * Set $default_charset to correspond with the user's selection
264 * of language interface.
267 $composeMessage=$compose_messages[$session];
268 if (! deliverMessage($composeMessage, true)) {
269 showInputForm($session);
272 unset($compose_messages[$session]);
273 $draft_message = _("Draft Email Saved");
274 /* If this is a resumed draft, then delete the original */
275 if(isset($delete_draft)) {
276 Header("Location: delete_message.php?mailbox=" . urlencode($draft_folder) .
277 "&message=$delete_draft&sort=$sort&startMessage=1&saved_draft=yes");
281 if ($compose_new_win == '1') {
282 Header("Location: compose.php?saved_draft=yes&session=$composesession");
286 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort".
287 "&startMessage=1¬e=".urlencode($draft_message));
295 if (isset($_FILES['attachfile']) &&
296 $_FILES['attachfile']['tmp_name'] &&
297 $_FILES['attachfile']['tmp_name'] != 'none') {
298 $AttachFailure = saveAttachedFiles($session);
300 if (checkInput(false) && !isset($AttachFailure)) {
301 $urlMailbox = urlencode (trim($mailbox));
302 if (! isset($passed_id)) {
306 * Set $default_charset to correspond with the user's selection
307 * of language interface.
311 * This is to change all newlines to \n
312 * We'll change them to \r\n later (in the sendMessage function)
314 $body = str_replace("\r\n", "\n", $body);
315 $body = str_replace("\r", "\n", $body);
318 * Rewrap $body so that no line is bigger than $editor_size
319 * This should only really kick in the sqWordWrap function
320 * if the browser doesn't support "VIRTUAL" as the wrap type.
322 $body = explode("\n", $body);
324 foreach ($body as $line) {
325 if( $line <> '-- ' ) {
326 $line = rtrim($line);
328 if (strlen($line) <= $editor_size +
1) {
329 $newBody .= $line . "\n";
331 sqWordWrap($line, $editor_size);
332 $newBody .= $line . "\n";
336 do_hook('compose_send');
337 $composeMessage=$compose_messages[$session];
339 $Result = deliverMessage($composeMessage);
341 showInputForm($session);
344 unset($compose_messages[$session]);
345 if ( isset($delete_draft)) {
346 Header("Location: delete_message.php?mailbox=" . urlencode( $draft_folder ).
347 "&message=$delete_draft&sort=$sort&startMessage=1&mail_sent=yes");
350 if ($compose_new_win == '1') {
352 Header("Location: compose.php?mail_sent=yes");
355 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort".
359 if ($compose_new_win == '1') {
360 compose_Header($color, $mailbox);
363 displayPageHeader($color, $mailbox);
365 if (isset($AttachFailure)) {
366 plain_error_message(_("Could not move/copy file. File not attached"),
370 showInputForm($session);
371 /* sqimap_logout($imapConnection); */
373 } elseif (isset($html_addr_search_done)) {
374 if ($compose_new_win == '1') {
375 compose_Header($color, $mailbox);
378 displayPageHeader($color, $mailbox);
381 if (isset($send_to_search) && is_array($send_to_search)) {
382 foreach ($send_to_search as $k => $v) {
383 if (substr($k, 0, 1) == 'T') {
389 elseif (substr($k, 0, 1) == 'C') {
395 elseif (substr($k, 0, 1) == 'B') {
397 $send_to_bcc .= ', ';
403 showInputForm($session);
404 } elseif (isset($html_addr_search)) {
405 if (isset($_FILES['attachfile']) &&
406 $_FILES['attachfile']['tmp_name'] &&
407 $_FILES['attachfile']['tmp_name'] != 'none') {
408 if(saveAttachedFiles($session)) {
409 plain_error_message(_("Could not move/copy file. File not attached"), $color);
413 * I am using an include so as to elminiate an extra unnecessary
414 * click. If you can think of a better way, please implement it.
416 include_once('./addrbook_search_html.php');
417 } elseif (isset($attach)) {
418 if (saveAttachedFiles($session)) {
419 plain_error_message(_("Could not move/copy file. File not attached"), $color);
421 if ($compose_new_win == '1') {
422 compose_Header($color, $mailbox);
425 displayPageHeader($color, $mailbox);
427 showInputForm($session);
429 elseif (isset($sigappend)) {
430 $idents = getPref($data_dir, $username, 'identities', 0);
432 if ($identity == 'default') {
437 $signature = getSig($data_dir, $username, $no);
439 $body .= "\n\n".($prefix_sig==true?
"-- \n":'').$signature;
440 if ($compose_new_win == '1') {
441 compose_Header($color, $mailbox);
443 displayPageHeader($color, $mailbox);
445 showInputForm($session);
446 } elseif (isset($do_delete)) {
447 if ($compose_new_win == '1') {
448 compose_Header($color, $mailbox);
451 displayPageHeader($color, $mailbox);
454 if (isset($delete) && is_array($delete)) {
455 $composeMessage = $compose_messages[$session];
456 foreach($delete as $index) {
457 $attached_file = $composeMessage->entities
[$index]->att_local_name
;
458 unlink ($attached_file);
459 unset ($composeMessage->entities
[$index]);
461 $new_entities = array();
462 foreach ($composeMessage->entities
as $entity) {
463 $new_entities[] = $entity;
465 $composeMessage->entities
= $new_entities;
466 $compose_messages[$session] = $composeMessage;
467 sqsession_register($compose_messages, 'compose_messages');
469 showInputForm($session);
472 * This handles the default case as well as the error case
473 * (they had the same code) --> if (isset($smtpErrors))
476 if ($compose_new_win == '1') {
477 compose_Header($color, $mailbox);
479 displayPageHeader($color, $mailbox);
484 if (!isset($passed_ent_id)) {
487 if (!isset($passed_id)) {
490 if (!isset($mailbox)) {
493 if (!isset($action)) {
497 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
499 /* in case the origin is not read_body.php */
500 if (isset($send_to)) {
501 $values['send_to'] = $send_to;
503 if (isset($send_to_cc)) {
504 $values['send_to_cc'] = $send_to_cc;
506 if (isset($send_to_bcc)) {
507 $values['send_to_bcc'] = $send_to_bcc;
509 showInputForm($session, $values);
514 /**************** Only function definitions go below *************/
517 /* This function is used when not sending or adding attachments */
518 function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
519 global $editor_size, $default_use_priority, $body,
520 $use_signature, $composesession, $data_dir, $username,
521 $username, $key, $imapServerAddress, $imapPort, $compose_messages,
524 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
528 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
531 sqimap_mailbox_select($imapConnection, $mailbox);
532 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
535 if ($passed_ent_id) {
536 /* redefine the messsage in case of message/rfc822 */
537 $message = $message->getEntity($passed_ent_id);
538 /* message is an entity which contains the envelope and type0=message
539 * and type1=rfc822. The actual entities are childs from
540 * $message->entities[0]. That's where the encoding and is located
543 $entities = $message->entities
[0]->findDisplayEntity
544 (array(), $alt_order = array('text/plain'));
545 if (!count($entities)) {
546 $entities = $message->entities
[0]->findDisplayEntity
547 (array(), $alt_order = array('text/plain','html/plain'));
549 $orig_header = $message->rfc822_header
; /* here is the envelope located */
550 /* redefine the message for picking up the attachments */
551 $message = $message->entities
[0];
554 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
555 if (!count($entities)) {
556 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
558 $orig_header = $message->rfc822_header
;
561 $encoding = $message->header
->encoding
;
562 $type0 = $message->type0
;
563 $type1 = $message->type1
;
564 foreach ($entities as $ent) {
565 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
566 $body_part_entity = $message->getEntity($ent);
567 $bodypart = decodeBody($unencoded_bodypart,
568 $body_part_entity->header
->encoding
);
569 if ($type1 == 'html') {
570 $bodypart = strip_tags($bodypart);
574 if ($default_use_priority) {
575 $mailprio = substr($orig_header->priority
,0,1);
582 //ClearAttachments($session);
585 $idents = getPref($data_dir, $username, 'identities');
586 $from_o = $orig_header->from
;
587 if (is_object($from_o)) {
588 $orig_from = $from_o->getAddress();
592 $identities = array();
593 if (!empty($idents) && $idents > 1) {
594 $identities[] = '"'. getPref($data_dir, $username, 'full_name')
595 . '" <' . getPref($data_dir, $username, 'email_address') . '>';
596 for ($i = 1; $i < $idents; $i++
) {
597 $enc_from_name = '"'.
598 getPref($data_dir, $username, 'full_name' . $i) .
600 getPref($data_dir, $username, 'email_address' . $i) . '>';
601 if ($enc_from_name == $orig_from && $i) {
605 $identities[] = $enc_from_name;
607 $identity_match = $orig_header->findAddress($identities);
608 if ($identity_match) {
609 $identity = $identity_match;
615 $use_signature = FALSE;
616 $send_to = decodeHeader($orig_header->getAddr_s('to'));
617 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'));
618 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'));
619 $subject = decodeHeader($orig_header->subject
);
621 $body_ary = explode("\n", $body);
622 $cnt = count($body_ary) ;
624 for ($i=0; $i < $cnt; $i++
) {
625 if (!ereg("^[>\\s]*$", $body_ary[$i]) ||
!$body_ary[$i]) {
626 sqWordWrap($body_ary[$i], $editor_size );
627 $body .= $body_ary[$i] . "\n";
629 unset($body_ary[$i]);
632 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
634 case ('edit_as_new'):
635 $send_to = decodeHeader($orig_header->getAddr_s('to'));
636 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'));
637 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'));
638 $subject = decodeHeader($orig_header->subject
);
639 $mailprio = $orig_header->priority
;
641 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
646 $subject = decodeHeader($orig_header->subject
);
647 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
648 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
649 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
650 $subject = '[Fwd: ' . $subject . ']';
652 $body = getforwardHeader($orig_header) . $body;
654 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
655 $body = "\n" . $body;
657 case ('forward_as_attachment'):
658 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
662 $send_to_cc = replyAllString($orig_header);
664 $send_to = $orig_header->reply_to
;
665 if (is_array($send_to) && count($send_to)) {
666 $send_to = decodeHeader($orig_header->getAddr_s('reply_to'));
667 } else if (is_object($send_to)) { /* unnessecarry, just for falesafe purpose */
668 $send_to = decodeHeader($orig_header->getAddr_s('reply_to'));
670 $send_to = decodeHeader($orig_header->getAddr_s('from'));
672 $subject = decodeHeader($orig_header->subject
);
673 $subject = str_replace('"', "'", $subject);
674 $subject = trim($subject);
675 if (substr(strtolower($subject), 0, 3) != 're:') {
676 $subject = 'Re: ' . $subject;
678 /* this corrects some wrapping/quoting problems on replies */
679 $rewrap_body = explode("\n", $body);
680 $from = (is_array($orig_header->from
)) ?
681 $orig_header->from
[0] : $orig_header->from
;
682 $body = getReplyCitation($from->getAddress(false));
684 $cnt = count($rewrap_body);
685 for ($i=0;$i<$cnt;$i++
) {
686 sqWordWrap($rewrap_body[$i], ($editor_size));
687 if (preg_match("/^(>+)/", $rewrap_body[$i], $matches)) {
689 $body .= '>' . str_replace("\n", "\n>$gt ", rtrim($rewrap_body[$i])) ."\n";
691 $body .= '> ' . str_replace("\n", "\n> ", rtrim($rewrap_body[$i])) . "\n";
693 unset($rewrap_body[$i]);
695 $composeMessage->reply_rfc822_header
= $orig_header;
700 $compose_messages[$session] = $composeMessage;
701 sqsession_register($compose_messages, 'compose_messages');
702 session_write_close();
703 sqimap_logout($imapConnection);
705 $ret = array( 'send_to' => $send_to,
706 'send_to_cc' => $send_to_cc,
707 'send_to_bcc' => $send_to_bcc,
708 'subject' => $subject,
709 'mailprio' => $mailprio,
711 'identity' => $identity );
714 } /* function newMail() */
716 function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
717 global $attachment_dir, $username, $data_dir, $squirrelmail_language;
718 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
719 if (!count($message->entities
) ||
720 ($message->type0
== 'message' && $message->type1
== 'rfc822')) {
721 if ( !in_array($message->entity_id
, $entities) && $message->entity_id
) {
722 switch ($message->type0
) {
724 if ($message->type1
== 'rfc822') {
725 $filename = decodeHeader($message->rfc822_header
->subject
.'.eml');
726 if ($filename == "") {
727 $filename = "untitled-".$message->entity_id
.'.eml';
730 $filename = decodeHeader($message->getFilename());
734 $filename = decodeHeader($message->getFilename());
737 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
738 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
739 $filename = $languages[$squirrelmail_language]['XTRA_CODE']('encode', $filename);
742 $localfilename = GenerateRandomString(32, '', 7);
743 $full_localfilename = "$hashed_attachment_dir/$localfilename";
744 while (file_exists($full_localfilename)) {
745 $localfilename = GenerateRandomString(32, '', 7);
746 $full_localfilename = "$hashed_attachment_dir/$localfilename";
748 $message->att_local_name
= $full_localfilename;
749 if (!$message->mime_header
) { /* temporary hack */
750 $message->mime_header
= $message->header
;
753 $composeMessage->addEntity($message);
755 /* Write Attachment to file */
756 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
757 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
758 $passed_id, $message->entity_id
),
759 $message->header
->encoding
));
763 for ($i=0, $entCount=count($message->entities
); $i<$entCount;$i++
) {
764 $composeMessage=getAttachments($message->entities
[$i], $composeMessage, $passed_id, $entities, $imapConnection);
767 // setPref($data_dir, $username, 'attachments', serialize($attachments));
768 return $composeMessage;
771 function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
772 $passed_ent_id='', $imapConnection) {
773 global $attachments, $attachment_dir, $username, $data_dir, $uid_support;
774 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
775 if (!$passed_ent_id) {
776 $body_a = sqimap_run_command($imapConnection,
777 'FETCH '.$passed_id.' RFC822',
778 TRUE, $response, $readmessage,
781 $body_a = sqimap_run_command($imapConnection,
782 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
783 TRUE, $response, $readmessage, $uid_support);
784 $message = $message->parent
;
786 if ($response = 'OK') {
787 $subject = encodeHeader($message->rfc822_header
->subject
);
788 array_shift($body_a);
789 $body = implode('', $body_a) . "\r\n";
791 $localfilename = GenerateRandomString(32, 'FILE', 7);
792 $full_localfilename = "$hashed_attachment_dir/$localfilename";
794 $fp = fopen( $full_localfilename, 'w');
797 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
798 $full_localfilename);
800 return $composeMessage;
803 function showInputForm ($session, $values=false) {
804 global $send_to, $send_to_cc, $body,
805 $passed_body, $color, $use_signature, $signature, $prefix_sig,
806 $editor_size, $attachments, $subject, $newmail,
807 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
808 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
809 $username, $data_dir, $identity, $draft_id, $delete_draft,
810 $mailprio, $default_use_mdn, $mdn_user_support, $compose_new_win,
811 $saved_draft, $mail_sent, $sig_first, $edit_as_new, $action,
812 $username, $compose_messages, $composesession, $default_charset;
814 $composeMessage = $compose_messages[$session];
816 $subject = decodeHeader($subject, false);
818 $send_to = $values['send_to'];
819 $send_to_cc = $values['send_to_cc'];
820 $send_to_bcc = $values['send_to_bcc'];
821 $subject = $values['subject'];
822 $mailprio = $values['mailprio'];
823 $body = $values['body'];
824 $identity = (int) $values['identity'];
827 if ($use_javascript_addr_book) {
828 echo "\n". '<SCRIPT LANGUAGE=JavaScript><!--' . "\n" .
829 'function open_abook() { ' . "\n" .
830 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
831 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
832 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
833 ' nwin.opener = document.windows;' . "\n" .
835 '// --></SCRIPT>' . "\n\n";
838 echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
839 'ENCTYPE="multipart/form-data"';
840 do_hook("compose_form");
844 if ($action == 'draft') {
845 echo '<input type="hidden" name="delete_draft" value="' . $passed_id . "\">\n";
847 if (isset($delete_draft)) {
848 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
850 if (isset($session)) {
851 echo '<input type="hidden" name="session" value="' . $session . "\">\n";
854 if (isset($passed_id)) {
855 echo '<input type="hidden" name="passed_id" value="' . $passed_id . "\">\n";
858 if ($saved_draft == 'yes') {
859 echo '<BR><CENTER><B>'. _("Draft Saved").'</CENTER></B>';
861 if ($mail_sent == 'yes') {
862 echo '<BR><CENTER><B>'. _("Your Message has been sent").'</CENTER></B>';
864 echo '<TABLE ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
865 if ($compose_new_win == '1') {
866 echo '<TABLE ALIGN=CENTER BGCOLOR="'.$color[0].'" WIDTH="100%" BORDER=0>'."\n" .
867 ' <TR><TD></TD><TD ALIGN="RIGHT"><INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'></TD></TR>'."\n";
869 if ($location_of_buttons == 'top') {
870 showComposeButtonRow();
873 $idents = getPref($data_dir, $username, 'identities', 0);
875 echo ' <TR>' . "\n" .
876 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
877 _("From:") . '</TD>' . "\n" .
878 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
879 ' <select name=identity>' . "\n" .
880 ' <option value=default>' .
881 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
882 $em = getPref($data_dir, $username, 'email_address');
884 echo htmlspecialchars(' <' . $em . '>') . "\n";
886 for ($i = 1; $i < $idents; $i ++
) {
887 echo '<option value="' . $i . '"';
888 if (isset($identity) && $identity == $i) {
891 echo '>' . htmlspecialchars(getPref($data_dir, $username,
893 $em = getPref($data_dir, $username, 'email_address' . $i);
895 echo htmlspecialchars(' <' . $em . '>') . "\n";
899 echo '</select>' . "\n" .
903 echo ' <TR>' . "\n" .
904 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
905 _("To:") . '</TD>' . "\n" .
906 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
907 ' <INPUT TYPE=text NAME="send_to" VALUE="' .
908 htmlspecialchars($send_to) . '" SIZE=60><BR>' . "\n" .
912 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' .
913 _("CC:") . '</TD>' . "\n" .
914 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
915 ' <INPUT TYPE=text NAME="send_to_cc" SIZE=60 VALUE="' .
916 htmlspecialchars($send_to_cc) . '"><BR>' . "\n" .
920 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' .
921 _("BCC:") . '</TD>' . "\n" .
922 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
923 ' <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
924 htmlspecialchars($send_to_bcc) . '" SIZE=60><BR>' . "\n" .
928 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' .
929 _("Subject:") . '</TD>' . "\n" .
930 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n";
931 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
932 htmlspecialchars($subject) . '">' . "\n" .
936 if ($location_of_buttons == 'between') {
937 showComposeButtonRow();
940 if ($compose_new_win == '1') {
941 echo ' <TR>' . "\n" .
942 ' <TD BGCOLOR="' . $color[0] . '" COLSPAN=2 ALIGN=CENTER>' . "\n" .
943 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
944 $editor_size . '" WRAP="VIRTUAL">';
947 echo ' <TR>' . "\n" .
948 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
949 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
950 $editor_size . '" WRAP="VIRTUAL">';
952 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
954 if ($identity == 'default') {
959 $signature = getSig($data_dir, $username, $no);
962 if ($sig_first == '1') {
963 if ($default_charset == 'iso-2022-jp') {
964 echo "\n\n".($prefix_sig==true?
"-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
966 echo "\n\n".($prefix_sig==true?
"-- \n":'').htmlspecialchars($signature);
968 echo "\n\n".htmlspecialchars($body);
971 echo "\n\n".htmlspecialchars($body);
972 if ($default_charset == 'iso-2022-jp') {
973 echo "\n\n".($prefix_sig==true?
"-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
975 echo "\n\n".($prefix_sig==true?
"-- \n":'').htmlspecialchars($signature);
980 echo htmlspecialchars($body);
982 echo '</TEXTAREA><BR>' . "\n" .
986 if ($location_of_buttons == 'bottom') {
987 showComposeButtonRow();
989 echo ' <TR>' . "\n" .
990 ' <TD COLSPAN=2 ALIGN=RIGHT>' . "\n" .
991 ' <INPUT TYPE=SUBMIT NAME=send VALUE="' . _("Send") . '">' . "\n" .
992 ' <BR><BR>' . "\n" .
997 /* This code is for attachments */
998 if ((bool) ini_get('file_uploads')) {
999 echo ' <TR>' . "\n" .
1000 ' <TD COLSPAN=2>' . "\n" .
1001 ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.
1002 ' border="0" bgcolor="'.$color[9].'">' . "\n" .
1005 ' <table width="100%" cellpadding="3" cellspacing="0" align="center"'.
1006 ' border="0">' . "\n" .
1008 ' <TD VALIGN=MIDDLE ALIGN=RIGHT>' .
1009 _("Attach:") . '</TD>' . "\n" .
1010 ' <TD VALIGN=MIDDLE ALIGN=LEFT>' . "\n" .
1011 ' <INPUT NAME="attachfile" SIZE=48 TYPE="file">' . "\n" .
1012 ' <input type="submit" name="attach"' .
1013 ' value="' . _("Add") .'">' . "\n" .
1019 if ($composeMessage->entities
) {
1020 foreach ($composeMessage->entities
as $key => $attachment) {
1021 $attached_file = $attachment->att_local_name
;
1022 if ($attachment->att_local_name ||
$attachment->body_part
) {
1023 $attached_filename = decodeHeader($attachment->mime_header
->getParameter('name'));
1024 $type = $attachment->mime_header
->type0
.'/'.
1025 $attachment->mime_header
->type1
;
1026 $s_a[] = '<input type="checkbox" name="delete[]" value="' .
1027 $key . "\">\n" . $attached_filename . ' - ' . $type .
1028 ' ('.show_readable_size( filesize( $attached_file ) )
1034 foreach ($s_a as $s) {
1035 echo '<tr><td align=left colspan="2" bgcolor="' . $color[0] . '">'.$s.'</td></tr>';
1037 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
1038 _("Delete selected attachments") . "\">\n" .
1041 echo ' </table>' . "\n" .
1044 ' </TABLE>' . "\n" .
1047 } // End of file_uploads if-block
1048 /* End of attachment code */
1049 if ($compose_new_win == '1') {
1050 echo '</TABLE>'."\n";
1053 echo '</TABLE>' . "\n" .
1054 '<input type="hidden" name="username" value="'. $username . "\">\n" .
1055 '<input type=hidden name=action value="' . $action . "\">\n" .
1056 '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
1059 store the complete ComposeMessages array in a hidden input value
1060 so we can restore them in case of a session timeout.
1062 echo '<input type=hidden name=restoremessages value="' . urlencode(serialize($compose_messages)) . "\">\n";
1063 echo '<input type=hidden name=composesession value="' . $composesession . "\">\n";
1064 echo '<input type=hidden name=querystring value="' . $_SERVER['QUERY_STRING'] . "\">\n";
1066 if (!(bool) ini_get('file_uploads')) {
1067 /* File uploads are off, so we didn't show that part of the form.
1068 To avoid bogus bug reports, tell the user why. */
1069 echo 'Because PHP file uploads are turned off, you can not attach files ';
1070 echo "to this message. Please see your system administrator for details.\r\n";
1074 do_hook('compose_bottom');
1075 echo '</BODY></HTML>' . "\n";
1079 function showComposeButtonRow() {
1080 global $use_javascript_addr_book, $save_as_draft,
1081 $default_use_priority, $mailprio, $default_use_mdn,
1082 $request_mdn, $request_dr,
1083 $data_dir, $username;
1085 echo ' <TR>' . "\n" .
1086 ' <TD></TD>' . "\n" .
1088 if ($default_use_priority) {
1089 if(!isset($mailprio)) {
1092 echo ' ' . _("Priority") .': <select name="mailprio">'.
1093 '<option value="1"'.($mailprio=='1'?
' selected':'').'>'. _("High") .'</option>'.
1094 '<option value="3"'.($mailprio=='3'?
' selected':'').'>'. _("Normal") .'</option>'.
1095 '<option value="5"'.($mailprio=='5'?
' selected':'').'>'. _("Low").'</option>'.
1098 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1099 if ($default_use_mdn) {
1100 if ($mdn_user_support) {
1101 echo ' ' . _("Receipt") .': '.
1102 '<input type="checkbox" name="request_mdn" value=1'.
1103 ($request_mdn=='1'?
' checked':'') .'>'. _("On Read").
1104 ' <input type="checkbox" name="request_dr" value=1'.
1105 ($request_dr=='1'?
' checked':'') .'>'. _("On Delivery");
1109 echo ' </TD>' . "\n" .
1112 ' <TD></TD>' . "\n" .
1114 ' <INPUT TYPE=SUBMIT NAME="sigappend" VALUE="' . _("Signature") . '">' . "\n";
1115 if ($use_javascript_addr_book) {
1116 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"".
1117 " <input type=button value=\\\""._("Addresses").
1118 "\\\" onclick='javascript:open_abook();'>\");".
1119 " // --></SCRIPT><NOSCRIPT>\n".
1120 " <input type=submit name=\"html_addr_search\" value=\"".
1121 _("Addresses")."\">".
1124 echo ' <input type=submit name="html_addr_search" value="'.
1125 _("Addresses").'">' . "\n";
1128 if ($save_as_draft) {
1129 echo ' <input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
1132 echo ' <INPUT TYPE=submit NAME=send VALUE="'. _("Send") . '">' . "\n";
1133 do_hook('compose_button_row');
1135 echo ' </TD>' . "\n" .
1139 function checkInput ($show) {
1141 * I implemented the $show variable because the error messages
1142 * were getting sent before the page header. So, I check once
1143 * using $show=false, and then when i'm ready to display the error
1144 * message, show=true
1146 global $body, $send_to, $send_to_bcc, $subject, $color;
1148 if ($send_to == '' && $send_to_bcc == '') {
1150 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
1155 } /* function checkInput() */
1158 /* True if FAILURE */
1159 function saveAttachedFiles($session) {
1160 global $_FILES, $attachment_dir, $attachments, $username,
1161 $data_dir, $compose_messages;
1163 /* get out of here if no file was attached at all */
1164 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1168 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1169 $localfilename = GenerateRandomString(32, '', 7);
1170 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1171 while (file_exists($full_localfilename)) {
1172 $localfilename = GenerateRandomString(32, '', 7);
1173 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1176 if (!@rename
($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1177 if (function_exists("move_uploaded_file")) {
1178 if (!@move_uploaded_file
($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
1182 if (!@copy
($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1187 $message = $compose_messages[$session];
1188 $type = strtolower($_FILES['attachfile']['type']);
1189 $name = $_FILES['attachfile']['name'];
1190 $message->initAttachment($type, $name, $full_localfilename);
1191 $compose_messages[$session] = $message;
1194 function ClearAttachments($composeMessage) {
1195 if ($composeMessage->att_local_name
) {
1196 $attached_file = $composeMessage->att_local_name
;
1197 if (file_exists($attached_file)) {
1198 unlink($attached_file);
1201 for ($i=0, $entCount=count($composeMessage->entities
);$i< $entCount; ++
$i) {
1202 ClearAttachments($composeMessage->entities
[$i]);
1208 function getReplyCitation($orig_from) {
1209 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
1211 /* First, return an empty string when no citation style selected. */
1212 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
1216 /* Make sure our final value isn't an empty string. */
1217 if ($orig_from == '') {
1221 /* Otherwise, try to select the desired citation style. */
1222 switch ($reply_citation_style) {
1225 $end = ' ' . _("said") . ':';
1228 $start = '<' . _("quote") . ' ' . _("who") . '="';
1231 case 'user-defined':
1232 $start = $reply_citation_start .
1233 ($reply_citation_start == '' ?
'' : ' ');
1234 $end = $reply_citation_end;
1240 /* Build and return the citation string. */
1241 return ($start . $orig_from . $end . "\n");
1244 /* temporary function to make use of the deliver class.
1245 In the future the responsable backend should be automaticly loaded
1246 and conf.pl should show a list of available backends.
1247 The message also should be constructed by the message class.
1250 function deliverMessage($composeMessage, $draft=false) {
1251 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
1252 $username, $popuser, $usernamedata, $identity, $data_dir,
1253 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
1255 global $imapServerAddress, $imapPort, $sent_folder, $key;
1257 $rfc822_header = $composeMessage->rfc822_header
;
1259 $abook = addressbook_init(false, true);
1261 $rfc822_header->to
= $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
1262 $rfc822_header->cc
= $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
1263 $rfc822_header->bcc
= $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
1264 $rfc822_header->priority
= $mailprio;
1265 $rfc822_header->subject
= $subject;
1266 $special_encoding='';
1267 if (strtolower($default_charset) == 'iso-2022-jp') {
1268 if (mb_detect_encoding($body) == 'ASCII') {
1269 $special_encoding = '8bit';
1271 $body = mb_convert_encoding($body, 'JIS');
1272 $special_encoding = '7bit';
1275 $composeMessage->setBody($body);
1277 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
1278 $popuser = $usernamedata[1];
1279 $domain = $usernamedata[2];
1280 unset($usernamedata);
1282 $popuser = $username;
1285 if (isset($identity) && $identity != 'default') {
1286 $from_mail = getPref($data_dir, $username,'email_address' . $identity);
1287 $full_name = getPref($data_dir, $username,'full_name' . $identity);
1288 $reply_to = getPref($data_dir, $username,'reply_to' . $identity);
1290 $from_mail = getPref($data_dir, $username, 'email_address');
1291 $full_name = getPref($data_dir, $username, 'full_name');
1292 $reply_to = getPref($data_dir, $username,'reply_to');
1295 $from_mail = "$popuser@$domain";
1298 $rfc822_header->from
= $rfc822_header->parseAddress($from_mail,true);
1300 $from = $rfc822_header->from
[0];
1301 if (!$from->host
) $from->host
= $domain;
1302 $from_addr = '"'.$full_name .'" <'.$from->mailbox
.'@'.$from->host
.'>';
1303 $rfc822_header->from
= $rfc822_header->parseAddress($from_addr,true);
1306 $rfc822_header->reply_to
= $rfc822_header->parseAddress($reply_to,true);
1308 /* Receipt: On Read */
1309 if (isset($request_mdn) && $request_mdn) {
1310 $rfc822_header->dnt
= $rfc822_header->parseAddress($from_mail,true);
1312 /* Receipt: On Delivery */
1313 if (isset($request_dr) && $request_dr) {
1314 $rfc822_header->more_headers
['Return-Receipt-To'] = $from_mail;
1316 /* multipart messages */
1317 if (count($composeMessage->entities
)) {
1318 $message_body = new Message();
1319 $message_body->body_part
= $composeMessage->body_part
;
1320 $composeMessage->body_part
= '';
1321 $mime_header = new MessageHeader
;
1322 $mime_header->type0
= 'text';
1323 $mime_header->type1
= 'plain';
1324 if ($special_encoding) {
1325 $mime_header->encoding
= $special_encoding;
1327 $mime_header->encoding
= 'us-ascii';
1329 if ($default_charset) {
1330 $mime_header->parameters
['charset'] = $default_charset;
1332 $message_body->mime_header
= $mime_header;
1333 array_unshift($composeMessage->entities
, $message_body);
1334 $content_type = new ContentType('multipart/mixed');
1336 $content_type = new ContentType('text/plain');
1338 if ($default_charset) {
1339 $content_type->properties
['charset']=$default_charset;
1342 $rfc822_header->content_type
= $content_type;
1343 $composeMessage->rfc822_header
= $rfc822_header;
1345 if (!$useSendmail && !$draft) {
1346 require_once(SM_PATH
. 'class/deliver/Deliver_SMTP.class.php');
1347 $deliver = new Deliver_SMTP();
1348 global $smtpServerAddress, $smtpPort, $pop_before_smtp, $smtp_auth_mech;
1350 if ($smtp_auth_mech == 'none') {
1354 global $key, $onetimepad;
1356 $pass = OneTimePadDecrypt($key, $onetimepad);
1359 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ?
true : false;
1360 $stream = $deliver->initStream($composeMessage,$domain,0,
1361 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
1362 } elseif (!$draft) {
1363 require_once(SM_PATH
. 'class/deliver/Deliver_SendMail.class.php');
1364 global $sendmail_path;
1365 $deliver = new Deliver_SendMail();
1366 $stream = $deliver->initStream($composeMessage,$sendmail_path);
1368 global $draft_folder;
1369 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1370 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1372 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
1373 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1374 $imap_deliver = new Deliver_IMAP();
1375 $length = $imap_deliver->mail($composeMessage);
1376 sqimap_append ($imap_stream, $draft_folder, $length);
1377 $imap_deliver->mail($composeMessage, $imap_stream);
1378 sqimap_append_done ($imap_stream, $draft_folder);
1379 sqimap_logout($imap_stream);
1380 unset ($imap_deliver);
1383 $msg = '<br>Error: '._("Draft folder")." $draft_folder" . ' does not exist.';
1384 plain_error_message($msg, $color);
1390 $length = $deliver->mail($composeMessage, $stream);
1391 $succes = $deliver->finalizeStream($stream);
1394 $msg = $deliver->dlv_msg
. '<br>Server replied: '.$deliver->dlv_ret_nr
;
1395 plain_error_message($msg, $color);
1398 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1400 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
1401 sqimap_append ($imap_stream, $sent_folder, $length);
1402 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1403 $imap_deliver = new Deliver_IMAP();
1404 $imap_deliver->mail($composeMessage, $imap_stream);
1405 sqimap_append_done ($imap_stream, $sent_folder);
1406 unset ($imap_deliver);
1408 global $passed_id, $mailbox, $action;
1409 ClearAttachments($composeMessage);
1410 if ($action == 'reply' ||
$action == 'reply_all') {
1411 sqimap_mailbox_select ($imap_stream, $mailbox);
1412 sqimap_messages_flag ($imap_stream, $passed_id, $passed_id, 'Answered', true);
1414 sqimap_logout($imap_stream);