539d8623710bdc28a0cb8347b8d19d0ed86d58a4
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/smtp.php');
29 require_once(SM_PATH
. 'functions/plugin.php');
30 require_once(SM_PATH
. 'functions/display_messages.php');
31 require_once(SM_PATH
. 'class/deliver/Deliver.class.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');
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($_POST['attachments']) ) {
85 $attachments = &$_POST['attachments'];
87 elseif ( isset($_SESSION['attachments'])) {
88 $attachments = &$_SESSION['attachments'];
91 /* Forward message as attachment */
92 if ( isset($_GET['attachedmessages']) ) {
93 $attachedmessages = $_GET['attachedmessages'];
97 sqextractGlobalVar('draft');
98 sqextractGlobalVar('draft_id');
99 sqextractGlobalVar('ent_num');
100 sqextractGlobalVar('saved_draft');
101 sqextractGlobalVar('delete_draft');
103 $key = $_COOKIE['key'];
105 /* --------------------- Specific Functions ------------------------------ */
107 function replyAllString($header) {
108 global $include_self_reply_all, $username, $data_dir;
111 * 1) Remove the addresses we'll be sending the message 'to'
113 $url_replytoall_avoid_addrs = '';
114 if (isset($header->replyto
)) {
115 $excl_ar = $header->getAddr_a('replyto');
118 * 2) Remove our identities from the CC list (they still can be in the
119 * TO list) only if $include_self_reply_all is turned off
121 if (!$include_self_reply_all) {
122 $email_address = trim(getPref($data_dir, $username, 'email_address'));
123 $excl_ar[$email_address] = '';
125 $idents = getPref($data_dir, $username, 'identities');
126 if ($idents != '' && $idents > 1) {
127 for ($i = 1; $i < $idents; $i ++
) {
128 $cur_email_address = getPref($data_dir, $username,
129 'email_address' . $i);
130 $cur_email_address = strtolower($cur_email_address);
131 $excl_ar[$cur_email_address] = '';
137 * 3) get the addresses.
139 $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar);
142 * 4) generate the string.
144 $url_replytoallcc = '';
145 foreach( $url_replytoall_ar as $email => $personal) {
147 $url_replytoallcc .= ", \"$personal\" <$email>";
149 $url_replytoallcc .= ', '. $email;
152 $url_replytoallcc = substr($url_replytoallcc,2);
153 return $url_replytoallcc;
156 function getforwardHeader($orig_header) {
159 $display = array( _("Subject") => strlen(_("Subject")),
160 _("From") => strlen(_("From")),
161 _("Date") => strlen(_("Date")),
162 _("To") => strlen(_("To")),
163 _("Cc") => strlen(_("Cc")) );
164 $maxsize = max($display);
165 $indent = str_pad('',$maxsize+
2);
166 foreach($display as $key => $val) {
167 $display[$key] = $key .': '. str_pad('', $maxsize - $val);
169 $bodyTop = str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH
) .
170 "\n". $display[_("Subject")] .
171 decodeHeader($orig_header->subject
) . "\n" .
172 $display[_("From")] .
173 decodeHeader($orig_header->getAddr_s('from',"\n$indent")) . "\n" .
174 $display[_("Date")] .
175 getLongDateString( $orig_header->date
). "\n" .
177 decodeHeader($orig_header->getAddr_s('to',"\n$indent")) ."\n";
178 if ($orig_header->cc
!= array() && $orig_header->cc
!='') {
179 $bodyTop .= $display[_("Cc")] . decodeHeader($orig_header->getAddr_s('cc',"\n$indent")) . "\n";
181 $bodyTop .= str_pad('', $editor_size -2 , '-') .
185 /* ----------------------------------------------------------------------- */
187 /* OLD: should be adapted to composeMessage */
190 * If the session is expired during a post this restores the compose session
193 //$session_expired = false;
194 if (session_is_registered('session_expired_post')) {
195 global $session_expired_post, $session_expired;
197 * extra check for username so we don't display previous post data from
198 * another user during this session.
200 if ($session_expired_post['username'] != $username) {
201 sqsession_unregister('session_expired_post');
202 sqsession_unregister('session_expired');
204 foreach ($session_expired_post as $postvar => $val) {
214 $session_expired = true;
216 sqsession_unregister('session_expired_post');
217 sqsession_unregister('session_expired');
218 if (!isset($mailbox)) {
221 if ($compose_new_win == '1') {
222 compose_Header($color, $mailbox);
224 displayPageHeader($color, $mailbox);
226 showInputForm($session, false);
230 if (!isset($composesession)) {
232 sqsession_register(0,'composesession');
235 if (!isset($session) ||
(isset($newmessage) && $newmessage)) {
236 sqsession_unregister('composesession');
237 $session = "$composesession" +
1;
238 $composesession = $session;
239 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 $Result = sendMessage($composeMessage, true);
270 if (! sendMessage($composeMessage, true)) {
271 showInputForm($session);
274 $draft_message = _("Draft Email Saved");
275 /* If this is a resumed draft, then delete the original */
276 if(isset($delete_draft)) {
277 Header("Location: delete_message.php?mailbox=" . urlencode($draft_folder) .
278 "&message=$delete_draft&sort=$sort&startMessage=1&saved_draft=yes");
282 if ($compose_new_win == '1') {
283 Header("Location: compose.php?saved_draft=yes&session=$composesession");
287 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort".
288 "&startMessage=1¬e=".urlencode($draft_message));
296 if (isset($_FILES['attachfile']) &&
297 $_FILES['attachfile']['tmp_name'] &&
298 $_FILES['attachfile']['tmp_name'] != 'none') {
299 $AttachFailure = saveAttachedFiles($session);
301 if (checkInput(false) && !isset($AttachFailure)) {
302 $urlMailbox = urlencode (trim($mailbox));
303 if (! isset($passed_id)) {
307 * Set $default_charset to correspond with the user's selection
308 * of language interface.
312 * This is to change all newlines to \n
313 * We'll change them to \r\n later (in the sendMessage function)
315 $body = str_replace("\r\n", "\n", $body);
316 $body = str_replace("\r", "\n", $body);
319 * Rewrap $body so that no line is bigger than $editor_size
320 * This should only really kick in the sqWordWrap function
321 * if the browser doesn't support "VIRTUAL" as the wrap type.
323 $body = explode("\n", $body);
325 foreach ($body as $line) {
326 if( $line <> '-- ' ) {
327 $line = rtrim($line);
329 if (strlen($line) <= $editor_size +
1) {
330 $newBody .= $line . "\n";
332 sqWordWrap($line, $editor_size);
333 $newBody .= $line . "\n";
337 do_hook('compose_send');
338 $composeMessage=$compose_messages[$session];
339 $Result = sendMessage($composeMessage);
341 showInputForm($session);
344 if ( isset($delete_draft)) {
345 Header("Location: delete_message.php?mailbox=" . urlencode( $draft_folder ).
346 "&message=$delete_draft&sort=$sort&startMessage=1&mail_sent=yes");
349 if ($compose_new_win == '1') {
350 Header("Location: compose.php?mail_sent=yes");
353 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort".
358 *$imapConnection = sqimap_login($username, $key, $imapServerAddress,
361 if ($compose_new_win == '1') {
362 compose_Header($color, $mailbox);
365 displayPageHeader($color, $mailbox);
367 if (isset($AttachFailure)) {
368 plain_error_message(_("Could not move/copy file. File not attached"),
372 showInputForm($session);
373 /* sqimap_logout($imapConnection); */
375 } elseif (isset($html_addr_search_done)) {
376 if ($compose_new_win == '1') {
377 compose_Header($color, $mailbox);
380 displayPageHeader($color, $mailbox);
383 if (isset($send_to_search) && is_array($send_to_search)) {
384 foreach ($send_to_search as $k => $v) {
385 if (substr($k, 0, 1) == 'T') {
391 elseif (substr($k, 0, 1) == 'C') {
397 elseif (substr($k, 0, 1) == 'B') {
399 $send_to_bcc .= ', ';
405 showInputForm($session);
406 } elseif (isset($html_addr_search)) {
407 if (isset($_FILES['attachfile']) &&
408 $_FILES['attachfile']['tmp_name'] &&
409 $_FILES['attachfile']['tmp_name'] != 'none') {
410 if(saveAttachedFiles($session)) {
411 plain_error_message(_("Could not move/copy file. File not attached"), $color);
415 * I am using an include so as to elminiate an extra unnecessary
416 * click. If you can think of a better way, please implement it.
418 include_once('./addrbook_search_html.php');
419 } elseif (isset($attach)) {
420 if (saveAttachedFiles($session)) {
421 plain_error_message(_("Could not move/copy file. File not attached"), $color);
423 if ($compose_new_win == '1') {
424 compose_Header($color, $mailbox);
427 displayPageHeader($color, $mailbox);
429 showInputForm($session);
431 elseif (isset($sigappend)) {
432 $idents = getPref($data_dir, $username, 'identities', 0);
434 if ($identity == 'default') {
439 $signature = getSig($data_dir, $username, $no);
441 $body .= "\n\n".($prefix_sig==true?
"-- \n":'').$signature;
442 if ($compose_new_win == '1') {
443 compose_Header($color, $mailbox);
445 displayPageHeader($color, $mailbox);
447 showInputForm($session);
448 } elseif (isset($do_delete)) {
449 if ($compose_new_win == '1') {
450 compose_Header($color, $mailbox);
453 displayPageHeader($color, $mailbox);
456 if (isset($delete) && is_array($delete)) {
457 $composeMessage = $compose_messages[$session];
458 foreach($delete as $index) {
459 $attached_file = $composeMessage->entities
[$index]->att_local_name
;
460 unlink ($attached_file);
461 unset ($composeMessage->entities
[$index]);
463 $new_entities = array();
464 foreach ($composeMessage->entities
as $entity) {
465 $new_entities[] = $entity;
467 $composeMessage->entities
= $new_entities;
468 $compose_messages[$session] = $composeMessage;
469 sqsession_register($compose_messages, 'compose_messages');
470 // setPref($data_dir, $username, 'attachments', serialize($attachments));
472 showInputForm($session);
475 * This handles the default case as well as the error case
476 * (they had the same code) --> if (isset($smtpErrors))
479 if ($compose_new_win == '1') {
480 compose_Header($color, $mailbox);
482 displayPageHeader($color, $mailbox);
487 if (!isset($passed_ent_id)) {
490 if (!isset($passed_id)) {
493 if (!isset($mailbox)) {
496 if (!isset($action)) {
500 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
502 /* in case the origin is not read_body.php */
503 if (isset($send_to)) {
504 $values['send_to'] = $send_to;
506 if (isset($send_to_cc)) {
507 $values['send_to_cc'] = $send_to_cc;
509 if (isset($send_to_bcc)) {
510 $values['send_to_bcc'] = $send_to_bcc;
512 showInputForm($session, $values);
517 /**************** Only function definitions go below *************/
520 /* This function is used when not sending or adding attachments */
521 function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
522 global $editor_size, $default_use_priority, $body,
523 $use_signature, $composesession, $data_dir, $username,
524 $username, $key, $imapServerAddress, $imapPort, $compose_messages,
527 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
531 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
534 sqimap_mailbox_select($imapConnection, $mailbox);
535 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
538 if ($passed_ent_id) {
539 /* redefine the messsage in case of message/rfc822 */
540 $message = $message->getEntity($passed_ent_id);
541 /* message is an entity which contains the envelope and type0=message
542 * and type1=rfc822. The actual entities are childs from
543 * $message->entities[0]. That's where the encoding and is located
546 $entities = $message->entities
[0]->findDisplayEntity
547 (array(), $alt_order = array('text/plain'));
548 if (!count($entities)) {
549 $entities = $message->entities
[0]->findDisplayEntity
550 (array(), $alt_order = array('text/plain','html/plain'));
552 $orig_header = $message->rfc822_header
; /* here is the envelope located */
553 /* redefine the message for picking up the attachments */
554 $message = $message->entities
[0];
557 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
558 if (!count($entities)) {
559 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
561 $orig_header = $message->rfc822_header
;
564 $encoding = $message->header
->encoding
;
565 $type0 = $message->type0
;
566 $type1 = $message->type1
;
567 foreach ($entities as $ent) {
568 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
569 $body_part_entity = $message->getEntity($ent);
570 $bodypart = decodeBody($unencoded_bodypart,
571 $body_part_entity->header
->encoding
);
572 if ($type1 == 'html') {
573 $bodypart = strip_tags($bodypart);
577 if ($default_use_priority) {
578 $mailprio = substr($orig_header->priority
,0,1);
585 //ClearAttachments($session);
588 $idents = getPref($data_dir, $username, 'identities');
589 $from_o = $orig_header->from
;
590 if (is_object($from_o)) {
591 $orig_from = $from_o->getAddress();
595 if (!empty($idents) && $idents > 1) {
596 for ($i = 1; $i < $idents; $i++
) {
597 $enc_from_name = '"'.
601 '" <' . getPref($data_dir, $username,
602 'email_address' . $i) . '>';
603 if ($enc_from_name == $orig_from) {
612 $use_signature = FALSE;
613 $send_to = decodeHeader($orig_header->getAddr_s('to'));
614 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'));
615 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'));
616 $subject = decodeHeader($orig_header->subject
);
618 $body_ary = explode("\n", $body);
619 $cnt = count($body_ary) ;
621 for ($i=0; $i < $cnt; $i++
) {
622 if (!ereg("^[>\\s]*$", $body_ary[$i]) ||
!$body_ary[$i]) {
623 sqWordWrap($body_ary[$i], $editor_size );
624 $body .= $body_ary[$i] . "\n";
626 unset($body_ary[$i]);
629 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
631 case ('edit_as_new'):
632 $send_to = decodeHeader($orig_header->getAddr_s('to'));
633 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'));
634 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'));
635 $subject = decodeHeader($orig_header->subject
);
636 $mailprio = $orig_header->priority
;
638 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
643 $subject = decodeHeader($orig_header->subject
);
644 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
645 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
646 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
647 $subject = '[Fwd: ' . $subject . ']';
649 $body = getforwardHeader($orig_header) . $body;
651 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
653 case ('forward_as_attachment'):
654 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
658 $send_to_cc = replyAllString($orig_header);
660 $send_to = $orig_header->reply_to
;
661 if (is_object($send_to)) {
662 $send_to = decodeHeader($send_to->getAddr_s('reply_to'));
664 $send_to = decodeHeader($orig_header->getAddr_s('from'));
666 $subject = decodeHeader($orig_header->subject
);
667 $subject = str_replace('"', "'", $subject);
668 $subject = trim($subject);
669 if (substr(strtolower($subject), 0, 3) != 're:') {
670 $subject = 'Re: ' . $subject;
672 /* this corrects some wrapping/quoting problems on replies */
673 $rewrap_body = explode("\n", $body);
675 $body = getReplyCitation($orig_header->from
->personal
);
676 $cnt = count($rewrap_body);
677 for ($i=0;$i<$cnt;$i++
) {
678 // sqWordWrap($rewrap_body[$i], ($editor_size - 2));
679 if (preg_match("/^(>+)/", $rewrap_body[$i], $matches)) {
681 $body .= '>' . str_replace("\n", "\n$gt ", $rewrap_body[$i]) ."\n";
683 $body .= '> ' . $rewrap_body[$i] . "\n";
685 unset($rewrap_body[$i]);
687 $composeMessage->reply_rfc822_header
= $orig_header;
692 $compose_messages[$session] = $composeMessage;
693 sqsession_register($compose_messages, 'compose_messages');
695 sqimap_logout($imapConnection);
697 $ret = array( 'send_to' => $send_to,
698 'send_to_cc' => $send_to_cc,
699 'send_to_bcc' => $send_to_bcc,
700 'subject' => $subject,
701 'mailprio' => $mailprio,
703 'identity' => $identity );
706 } /* function newMail() */
708 function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
709 global $attachment_dir, $username, $data_dir, $squirrelmail_language;
710 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
711 if (!count($message->entities
) ||
712 ($message->type0
== 'message' && $message->type1
== 'rfc822')) {
713 if ( !in_array($message->entity_id
, $entities) && $message->entity_id
) {
714 switch ($message->type0
) {
716 if ($message->type1
== 'rfc822') {
717 $filename = decodeHeader($message->rfc822_header
->subject
.'.eml');
718 if ($filename == "") {
719 $filename = "untitled-".$message->entity_id
.'.eml';
722 $filename = decodeHeader($message->getFilename());
726 $filename = decodeHeader($message->getFilename());
729 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
730 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
731 $filename = $languages[$squirrelmail_language]['XTRA_CODE']('encode', $filename);
734 $localfilename = GenerateRandomString(32, '', 7);
735 $full_localfilename = "$hashed_attachment_dir/$localfilename";
736 while (file_exists($full_localfilename)) {
737 $localfilename = GenerateRandomString(32, '', 7);
738 $full_localfilename = "$hashed_attachment_dir/$localfilename";
740 $message->att_local_name
= $full_localfilename;
741 if (!$message->mime_header
) { /* temporary hack */
742 $message->mime_header
= $message->header
;
745 $composeMessage->addEntity($message);
747 /* Write Attachment to file */
748 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
749 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
750 $passed_id, $message->entity_id
),
751 $message->header
->encoding
));
755 for ($i=0, $entCount=count($message->entities
); $i<$entCount;$i++
) {
756 $composeMessage=getAttachments($message->entities
[$i], $composeMessage, $passed_id, $entities, $imapConnection);
759 // setPref($data_dir, $username, 'attachments', serialize($attachments));
760 return $composeMessage;
763 function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
764 $passed_ent_id='', $imapConnection) {
765 global $attachments, $attachment_dir, $username, $data_dir, $uid_support;
766 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
767 if (!$passed_ent_id) {
768 $body_a = sqimap_run_command($imapConnection,
769 'FETCH '.$passed_id.' RFC822',
770 TRUE, $response, $readmessage,
773 $body_a = sqimap_run_command($imapConnection,
774 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
775 TRUE, $response, $readmessage, $uid_support);
776 $message = $message->parent
;
778 if ($response = 'OK') {
779 $subject = encodeHeader($message->rfc822_header
->subject
);
780 array_shift($body_a);
781 $body = implode('', $body_a) . "\r\n";
783 $localfilename = GenerateRandomString(32, 'FILE', 7);
784 $full_localfilename = "$hashed_attachment_dir/$localfilename";
786 $fp = fopen( $full_localfilename, 'w');
789 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
790 $full_localfilename);
792 return $composeMessage;
795 function showInputForm ($session, $values=false) {
796 global $send_to, $send_to_cc, $body,
797 $passed_body, $color, $use_signature, $signature, $prefix_sig,
798 $editor_size, $attachments, $subject, $newmail,
799 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
800 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
801 $username, $data_dir, $identity, $draft_id, $delete_draft,
802 $mailprio, $default_use_mdn, $mdn_user_support, $compose_new_win,
803 $saved_draft, $mail_sent, $sig_first, $edit_as_new, $action,
804 $username, $compose_messages;
806 $composeMessage = $compose_messages[$session];
808 $subject = decodeHeader($subject, false);
810 $send_to = $values['send_to'];
811 $send_to_cc = $values['send_to_cc'];
812 $send_to_bcc = $values['send_to_bcc'];
813 $subject = $values['subject'];
814 $mailprio = $values['mailprio'];
815 $body = $values['body'];
816 $identity = $values['identity'];
819 if ($use_javascript_addr_book) {
820 echo "\n". '<SCRIPT LANGUAGE=JavaScript><!--' . "\n" .
821 'function open_abook() { ' . "\n" .
822 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
823 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
824 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
825 ' nwin.opener = document.windows;' . "\n" .
827 '// --></SCRIPT>' . "\n\n";
830 echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
831 'ENCTYPE="multipart/form-data"';
832 do_hook("compose_form");
837 if ($action == 'draft') {
838 echo '<input type="hidden" name="delete_draft" value="' . $passed_id . "\">\n";
840 if (isset($delete_draft)) {
841 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
843 if (isset($session)) {
844 echo '<input type="hidden" name="session" value="' . $session . "\">\n";
847 if (isset($passed_id)) {
848 echo '<input type="hidden" name="passed_id" value="' . $passed_id . "\">\n";
851 if ($saved_draft == 'yes') {
852 echo '<BR><CENTER><B>'. _("Draft Saved").'</CENTER></B>';
854 if ($mail_sent == 'yes') {
855 echo '<BR><CENTER><B>'. _("Your Message has been sent").'</CENTER></B>';
857 echo '<TABLE ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
858 if ($compose_new_win == '1') {
859 echo '<TABLE ALIGN=CENTER BGCOLOR="'.$color[0].'" WIDTH="100%" BORDER=0>'."\n" .
860 ' <TR><TD></TD><TD ALIGN="RIGHT"><INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'></TD></TR>'."\n";
862 if ($location_of_buttons == 'top') {
863 showComposeButtonRow();
866 $idents = getPref($data_dir, $username, 'identities', 0);
868 echo ' <TR>' . "\n" .
869 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
872 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
873 '<select name=identity>' . "\n" .
874 '<option value=default>' .
875 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
876 $em = getPref($data_dir, $username, 'email_address');
878 echo htmlspecialchars(' <' . $em . '>') . "\n";
880 for ($i = 1; $i < $idents; $i ++
) {
881 echo '<option value="' . $i . '"';
882 if (isset($identity) && $identity == $i) {
885 echo '>' . htmlspecialchars(getPref($data_dir, $username,
887 $em = getPref($data_dir, $username, 'email_address' . $i);
889 echo htmlspecialchars(' <' . $em . '>') . "\n";
893 echo '</select>' . "\n" .
897 echo ' <TR>' . "\n" .
898 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' . "\n" .
900 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
901 ' <INPUT TYPE=text NAME="send_to" VALUE="' .
902 htmlspecialchars($send_to) . '" SIZE=60><BR>' . "\n" .
906 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
908 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
909 ' <INPUT TYPE=text NAME="send_to_cc" SIZE=60 VALUE="' .
910 htmlspecialchars($send_to_cc) . '"><BR>' . "\n" .
914 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
916 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
917 ' <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
918 htmlspecialchars($send_to_bcc) . '" SIZE=60><BR>' . "\n" .
919 '</TD></TR>' . "\n" .
921 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
923 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n";
924 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
925 htmlspecialchars($subject) . '">';
926 echo '</td></tr>' . "\n\n";
928 if ($location_of_buttons == 'between') {
929 showComposeButtonRow();
931 if ($compose_new_win == '1') {
932 echo ' <TR>' . "\n" .
933 ' <TD BGCOLOR="' . $color[0] . '" COLSPAN=2 ALIGN=CENTER>' . "\n" .
934 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
935 $editor_size . '" WRAP="VIRTUAL">';
938 echo ' <TR>' . "\n" .
939 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
940 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
941 $editor_size . '" WRAP="VIRTUAL">';
943 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
944 if ($sig_first == '1') {
945 if ($charset == 'iso-2022-jp') {
946 echo "\n\n".($prefix_sig==true?
"-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
948 echo "\n\n".($prefix_sig==true?
"-- \n":'').htmlspecialchars($signature);
950 echo "\n\n".htmlspecialchars($body);
953 echo "\n\n".htmlspecialchars($body);
954 if ($charset == 'iso-2022-jp') {
955 echo "\n\n".($prefix_sig==true?
"-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
957 echo "\n\n".($prefix_sig==true?
"-- \n":'').htmlspecialchars($signature);
962 echo htmlspecialchars($body);
964 echo '</TEXTAREA><BR>' . "\n" .
968 if ($location_of_buttons == 'bottom') {
969 showComposeButtonRow();
971 echo ' <TR><TD COLSPAN=2 ALIGN=RIGHT>' .
972 ' <INPUT TYPE=SUBMIT NAME=send VALUE="' . _("Send") . '">' .
973 ' <BR><BR>' .
974 ' </TD></TR>' . "\n";
977 /* This code is for attachments */
978 echo '<table width="100%" cellpadding="0" cellspacing="4" align="center" border="0">' .
980 ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">' .
982 ' <table width="100%" cellpadding="3" cellspacing="0" align="center" border="0">' .
984 ' <TD VALIGN=MIDDLE ALIGN=RIGHT>' . "\n" .
987 ' <TD VALIGN=MIDDLE ALIGN=LEFT>' . "\n" .
988 ' <INPUT NAME="attachfile" SIZE=48 TYPE="file">' . "\n" .
989 ' <input type="submit" name="attach"' .
990 ' value="' . _("Add") .'">' . "\n" .
996 foreach ($composeMessage->entities
as $key => $attachment) {
997 $attached_file = $attachment->att_local_name
;
998 if ($attachment->att_local_name ||
$attachment->body_part
) {
999 $attached_filename = decodeHeader($attachment->mime_header
->getParameter('name'));
1000 $type = $attachment->mime_header
->type0
.'/'.
1001 $attachment->mime_header
->type1
;
1002 $s_a[] = '<input type="checkbox" name="delete[]" value="' .
1003 $key . "\">\n" . $attached_filename . ' - ' . $type .
1004 ' ('.show_readable_size( filesize( $attached_file ) )
1009 foreach ($s_a as $s) {
1010 echo '<tr><td align=left colspan="2" bgcolor="' . $color[0] . '">'.$s.'</td></tr>';
1012 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
1013 _("Delete selected attachments") . "\">\n" .
1016 echo ' </table></td></tr>' .
1020 /* End of attachment code */
1021 if ($compose_new_win == '1') {
1022 echo '</TABLE>'."\n";
1024 echo '</TABLE>' . "\n" .
1025 '<input type="hidden" name="username" value="'. $username . "\">\n" .
1026 '<input type=hidden name=action value=' . $action . ">\n" .
1027 '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
1030 do_hook('compose_bottom');
1031 echo '</BODY></HTML>' . "\n";
1035 function showComposeButtonRow() {
1036 global $use_javascript_addr_book, $save_as_draft,
1037 $default_use_priority, $mailprio, $default_use_mdn,
1038 $request_mdn, $request_dr,
1039 $data_dir, $username;
1041 echo " <TR><TD>\n</TD><TD>\n";
1042 if ($default_use_priority) {
1043 if(!isset($mailprio)) {
1046 echo _("Priority") .': <select name="mailprio">'.
1047 '<option value="1"'.($mailprio=='1'?
' selected':'').'>'. _("High") .'</option>'.
1048 '<option value="3"'.($mailprio=='3'?
' selected':'').'>'. _("Normal") .'</option>'.
1049 '<option value="5"'.($mailprio=='5'?
' selected':'').'>'. _("Low").'</option>'.
1052 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1053 if ($default_use_mdn) {
1054 if ($mdn_user_support) {
1055 echo "\n\t". _("Receipt") .': '.
1056 '<input type="checkbox" name="request_mdn" value=1'.
1057 ($request_mdn=='1'?
' checked':'') .'>'. _("On Read").
1058 ' <input type="checkbox" name="request_dr" value=1'.
1059 ($request_dr=='1'?
' checked':'') .'>'. _("On Delivery");
1063 echo " </td></tr>\n <TR><td>\n </td><td>\n" .
1064 "\n <INPUT TYPE=SUBMIT NAME=\"sigappend\" VALUE=\"". _("Signature") . "\">\n";
1065 if ($use_javascript_addr_book) {
1066 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"".
1067 " <input type=button value=\\\""._("Addresses").
1068 "\\\" onclick='javascript:open_abook();'>\");".
1069 " // --></SCRIPT><NOSCRIPT>\n".
1070 " <input type=submit name=\"html_addr_search\" value=\"".
1071 _("Addresses")."\">".
1074 echo " <input type=submit name=\"html_addr_search\" value=\"".
1075 _("Addresses")."\">";
1078 if ($save_as_draft) {
1079 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
1082 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
1083 do_hook('compose_button_row');
1085 echo " </TD></TR>\n\n";
1088 function checkInput ($show) {
1090 * I implemented the $show variable because the error messages
1091 * were getting sent before the page header. So, I check once
1092 * using $show=false, and then when i'm ready to display the error
1093 * message, show=true
1095 global $body, $send_to, $send_to_bcc, $subject, $color;
1097 if ($send_to == '' && $send_to_bcc == '') {
1099 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
1104 } /* function checkInput() */
1107 /* True if FAILURE */
1108 function saveAttachedFiles($session) {
1109 global $_FILES, $attachment_dir, $attachments, $username,
1110 $data_dir, $compose_messages;
1112 /* get out of here if no file was attached at all */
1113 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1117 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1118 $localfilename = GenerateRandomString(32, '', 7);
1119 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1120 while (file_exists($full_localfilename)) {
1121 $localfilename = GenerateRandomString(32, '', 7);
1122 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1125 if (!@rename
($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1126 if (function_exists("move_uploaded_file")) {
1127 if (!@move_uploaded_file
($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
1131 if (!@copy
($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1136 $message = $compose_messages[$session];
1137 $type = strtolower($_FILES['attachfile']['type']);
1138 $name = $_FILES['attachfile']['name'];
1139 $message->initAttachment($type, $name, $full_localfilename);
1140 $compose_messages[$session] = $message;
1143 function ClearAttachments($composeMessage) {
1144 if ($composeMessage->att_local_name
) {
1145 $attached_file = $composeMessage->att_local_name
;
1146 if (file_exists($attached_file)) {
1147 unlink($attached_file);
1150 for ($i=0, $entCount=count($composeMessage->entities
);$i< $entCount; ++
$i) {
1151 ClearAttachments($composeMessage->entities
[$i]);
1157 function getReplyCitation($orig_from) {
1158 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
1160 /* First, return an empty string when no citation style selected. */
1161 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
1165 /* Make sure our final value isn't an empty string. */
1166 if ($orig_from == '') {
1170 /* Otherwise, try to select the desired citation style. */
1171 switch ($reply_citation_style) {
1174 $end = ' ' . _("said") . ':';
1177 $start = '<' . _("quote") . ' ' . _("who") . '="';
1180 case 'user-defined':
1181 $start = $reply_citation_start .
1182 ($reply_citation_start == '' ?
'' : ' ');
1183 $end = $reply_citation_end;
1189 /* Build and return the citation string. */
1190 return ($start . $orig_from . $end . "\n");
1193 /* temporary function to make use of the deliver class.
1194 In the future the responsable backend should be automaticly loaded
1195 and conf.pl should show a list of available backends.
1196 The message also should be constructed by the message class.
1199 function sendMessage($composeMessage, $draft=false) {
1200 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
1201 $username, $popuser, $usernamedata, $identity, $data_dir,
1202 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
1204 global $imapServerAddress, $imapPort, $sent_folder, $key;
1206 $rfc822_header = $composeMessage->rfc822_header
;
1207 $rfc822_header->to
= $rfc822_header->parseAddress($send_to,true, array(), '', $domain);
1208 $rfc822_header->cc
= $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain);
1209 $rfc822_header->bcc
= $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain);
1210 $rfc822_header->priority
= $mailprio;
1211 $rfc822_header->subject
= $subject;
1212 $special_encoding='';
1213 if (strtolower($default_charset) == 'iso-2022-jp') {
1214 if (mb_detect_encoding($body) == 'ASCII') {
1215 $special_encoding = '8bit';
1217 $body = mb_convert_encoding($body, 'JIS');
1218 $special_encoding = '7bit';
1221 $composeMessage->setBody($body);
1223 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
1224 $popuser = $usernamedata[1];
1225 $domain = $usernamedata[2];
1226 unset($usernamedata);
1228 $popuser = $username;
1231 if (isset($identity) && $identity != 'default') {
1232 $from_mail = getPref($data_dir, $username,'email_address' . $identity);
1233 $full_name = getPref($data_dir, $username,'full_name' . $identity);
1234 $reply_to = getPref($data_dir, $username,'reply_to' . $identity);
1236 $from_mail = getPref($data_dir, $username, 'email_address');
1237 $full_name = getPref($data_dir, $username, 'full_name');
1238 $reply_to = getPref($data_dir, $username,'reply_to');
1241 $from_mail = "$popuser@$domain";
1244 $rfc822_header->from
= $rfc822_header->parseAddress($from_mail,true);
1246 $from = $rfc822_header->from
[0];
1247 if (!$from->host
) $from->host
= $domain;
1248 $from_addr = $full_name .' <'.$from->mailbox
.'@'.$from->host
.'>';
1249 $rfc822_header->from
= $rfc822_header->parseAddress($from_addr,true);
1252 $rfc822_header->reply_to
= $rfc822_header->parseAddress($reply_to,true);
1254 /* Receipt: On Read */
1255 if (isset($request_mdn) && $request_mdn) {
1256 $rfc822_header->dnt
= $rfc822_header->parseAddress($from_mail,true);
1258 /* Receipt: On Delivery */
1259 if (isset($request_dr) && $request_dr) {
1260 $rfc822_header->more_headers
['Return-Receipt-To'] = $from_mail;
1262 /* multipart messages */
1263 if (count($composeMessage->entities
)) {
1264 $message_body = new Message();
1265 $message_body->body_part
= $composeMessage->body_part
;
1266 $composeMessage->body_part
= '';
1267 $mime_header = new MessageHeader
;
1268 $mime_header->type0
= 'text';
1269 $mime_header->type1
= 'plain';
1270 if ($special_encoding) {
1271 $mime_header->encoding
= $special_encoding;
1273 $mime_header->encoding
= 'us-ascii';
1275 if ($default_charset) {
1276 $mime_header->parameters
['charset'] = $default_charset;
1278 $message_body->mime_header
= $mime_header;
1279 array_unshift($composeMessage->entities
, $message_body);
1280 $content_type = new ContentType('multipart/mixed');
1282 $content_type = new ContentType('text/plain');
1284 if ($default_charset) {
1285 $content_type->properties
['charset']=$default_charset;
1288 $rfc822_header->content_type
= $content_type;
1289 $composeMessage->rfc822_header
= $rfc822_header;
1291 if (!$useSendmail && !$draft) {
1292 require_once(SM_PATH
. 'class/deliver/Deliver_SMTP.class.php');
1293 $deliver = new Deliver_SMTP();
1294 global $smtpServerAddress, $smtpPort, $use_authenticated_smtp, $pop_before_smtp;
1295 if ($use_authenticated_smtp) {
1296 global $key, $onetimepad;
1298 $pass = OneTimePadDecrypt($key, $onetimepad);
1303 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ?
true : false;
1304 $stream = $deliver->initStream($composeMessage,$domain,0,
1305 $smtpServerAddress, $smtpPort, $authPop);
1306 } elseif (!$draft) {
1307 require_once(SM_PATH
. 'class/deliver/Deliver_SendMail.class.php');
1308 global $sendmail_path;
1309 $deliver = new Deliver_SendMail();
1310 $stream = $deliver->initStream($composeMessage,$sendmail_path);
1312 global $draft_folder;
1313 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1314 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1316 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
1317 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1318 $imap_deliver = new Deliver_IMAP();
1319 $length = $imap_deliver->mail($composeMessage);
1320 sqimap_append ($imap_stream, $draft_folder, $length);
1321 $imap_deliver->mail($composeMessage, $imap_stream);
1322 sqimap_append_done ($imap_stream);
1323 sqimap_logout($imap_stream);
1324 unset ($imap_deliver);
1331 $length = $deliver->mail($composeMessage, $stream);
1332 $succes = $deliver->finalizeStream($stream);
1335 $msg = $deliver->dlv_msg
. '<br>Server replied: '.$deliver->dlv_ret_nr
;
1336 plain_error_message($msg, $color);
1339 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1341 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
1342 sqimap_append ($imap_stream, $sent_folder, $length);
1343 require_once(SM_PATH
. 'class/deliver/Deliver_IMAP.class.php');
1344 $imap_deliver = new Deliver_IMAP();
1345 $imap_deliver->mail($composeMessage, $imap_stream);
1346 sqimap_append_done ($imap_stream);
1347 unset ($imap_deliver);
1349 global $passed_id, $mailbox, $action;
1350 ClearAttachments($composeMessage);
1351 if ($action == 'reply' ||
$action == 'reply_all') {
1352 sqimap_mailbox_select ($imap_stream, $mailbox);
1353 sqimap_messages_flag ($imap_stream, $passed_id, $passed_id, 'Answered', true);
1355 sqimap_logout($imap_stream);