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 require_once('../src/validate.php');
21 require_once('../functions/imap.php');
22 require_once('../functions/date.php');
23 require_once('../functions/mime.php');
24 require_once('../functions/smtp.php');
25 require_once('../functions/display_messages.php');
26 require_once('../functions/plugin.php');
28 if (!isset($attachments)) {
29 $attachments = array();
30 session_register('attachments');
33 if (!isset($mailbox) ||
$mailbox == '' ||
($mailbox == 'None')) {
38 include_once ('../src/draft_actions.php');
39 if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id, $MDN)) {
43 $draft_message = _("Draft Email Saved");
44 /* If this is a resumed draft, then delete the original */
45 if(isset($delete_draft)) {
46 Header("Location: delete_message.php?mailbox=$draft_folder".
47 "&message=$delete_draft&sort=$sort&startMessage=1");
50 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort".
51 "&startMessage=1¬e=$draft_message");
58 if (isset($HTTP_POST_FILES['attachfile']) &&
59 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
60 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none') {
61 $AttachFailure = saveAttachedFiles();
63 if (checkInput(false) && !isset($AttachFailure)) {
64 $urlMailbox = urlencode (trim($mailbox));
65 if (! isset($reply_id)) {
69 * Set $default_charset to correspond with the user's selection
70 * of language interface.
75 * This is to change all newlines to \n
76 * We'll change them to \r\n later (in the sendMessage function)
78 $body = str_replace("\r\n", "\n", $body);
79 $body = str_replace("\r", "\n", $body);
82 * Rewrap $body so that no line is bigger than $editor_size
83 * This should only really kick in the sqWordWrap function
84 * if the browser doesn't support "HARD" as the wrap type
85 * Or, in Opera's case, something goes wrong.
87 $body = explode("\n", $body);
89 foreach ($body as $line) {
90 if( $line <> '-- ' ) {
93 if (strlen($line) <= $editor_size +
1) {
94 $newBody .= $line . "\n";
96 sqWordWrap($line, $editor_size) . "\n";
102 do_hook("compose_send");
104 $MDN = False; // we are not sending a mdn response
105 if (! isset($mailprio)) {
106 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
107 $subject, $body, $reply_id, $MDN);
109 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
110 $subject, $body, $reply_id, $MDN, $mailprio);
116 if ( isset($delete_draft)) {
117 Header("Location: delete_message.php?mailbox=$draft_folder".
118 "&message=$delete_draft&sort=$sort&startMessage=1");
122 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort".
126 *$imapConnection = sqimap_login($username, $key, $imapServerAddress,
129 displayPageHeader($color, $mailbox);
131 if (isset($AttachFailure)) {
132 plain_error_message(_("Could not move/copy file. File not attached"),
138 /* sqimap_logout($imapConnection); */
141 elseif (isset($html_addr_search_done)) {
142 displayPageHeader($color, $mailbox);
144 if (isset($send_to_search) && is_array($send_to_search)) {
145 foreach ($send_to_search as $k => $v) {
146 if (substr($k, 0, 1) == 'T') {
152 elseif (substr($k, 0, 1) == 'C') {
158 elseif (substr($k, 0, 1) == 'B') {
160 $send_to_bcc .= ', ';
168 elseif (isset($html_addr_search)) {
169 if (isset($HTTP_POST_FILES['attachfile']) &&
170 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
171 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none') {
172 if (saveAttachedFiles()) {
173 plain_error_message(_("Could not move/copy file. File not attached"), $color);
177 * I am using an include so as to elminiate an extra unnecessary
178 * click. If you can think of a better way, please implement it.
180 include_once('./addrbook_search_html.php');
182 elseif (isset($attach)) {
183 if (saveAttachedFiles()) {
184 plain_error_message(_("Could not move/copy file. File not attached"), $color);
186 displayPageHeader($color, $mailbox);
189 elseif (isset($do_delete)) {
190 displayPageHeader($color, $mailbox);
192 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
193 if (isset($delete) && is_array($delete)) {
194 foreach($delete as $index) {
195 $attached_file = $hashed_attachment_dir . '/'
196 . $attachments[$index]['localfilename'];
197 unlink ($attached_file);
198 unset ($attachments[$index]);
205 * This handles the default case as well as the error case
206 * (they had the same code) --> if (isset($smtpErrors))
208 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
210 displayPageHeader($color, $mailbox);
216 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num) {
220 if (isset($draft_id) && $draft_id && isset($ent_num) && $ent_num) {
226 sqimap_logout($imapConnection);
232 /**************** Only function definitions go below *************/
235 /* This function is used when not sending or adding attachments */
236 function newMail () {
237 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
238 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size,
239 $draft_id, $use_signature;
241 $send_to = decodeHeader($send_to, false);
242 $send_to_cc = decodeHeader($send_to_cc, false);
243 $send_to_bcc = decodeHeader($send_to_bcc, false);
247 } elseif ($reply_id) {
253 $use_signature = FALSE;
257 sqimap_mailbox_select($imapConnection, $mailbox);
258 $message = sqimap_get_message($imapConnection, $id, $mailbox);
259 $orig_header = $message->header
;
261 $message = getEntity($message, $ent_num);
263 if ($message->header
->type0
== 'text' ||
264 $message->header
->type1
== 'message') {
267 mime_fetch_body($imapConnection, $id, $ent_num),
268 $message->header
->encoding
);
271 mime_fetch_body($imapConnection, $id, 1),
272 $message->header
->encoding
);
278 if ($message->header
->type1
== 'html') {
279 $body = strip_tags($body);
283 $body_ary = explode("\n", $body);
284 $i = count($body_ary) - 1;
285 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
286 unset($body_ary[$i]);
290 for ($i=0; isset($body_ary[$i]); $i++
) {
292 if (ereg('^[ >]+', $body_ary[$i])) {
293 $body_ary[$i] = '>' . $body_ary[$i];
295 $body_ary[$i] = '> ' . $body_ary[$i];
299 sqWordWrap($body_ary[$i], $editor_size - 1);
301 $body .= $body_ary[$i] . "\n";
302 unset($body_ary[$i]);
305 $bodyTop = '-------- ' . _("Original Message") . " --------\n" .
306 _("Subject") . ': ' . $orig_header->subject
. "\n" .
307 _("From") . ': ' . $orig_header->from
. "\n" .
309 getLongDateString( $orig_header->date
). "\n" .
310 _("To") . ': ' . $orig_header->to
[0] . "\n";
311 if (count($orig_header->to
) > 1) {
312 for ($x=1; $x < count($orig_header->to
); $x++
) {
313 $bodyTop .= ' ' . $orig_header->to
[$x] . "\n";
317 $body = $bodyTop . $body;
320 $orig_from = decodeHeader($orig_header->from
, false);
321 $body = getReplyCitation($orig_from) . $body;
328 $send_to = sqimap_find_email($send_to);
331 /* This formats a CC string if they hit "reply all" */
332 if ($send_to_cc != '') {
333 $send_to_cc = ereg_replace('"[^"]*"', '', $send_to_cc);
334 $send_to_cc = str_replace(';', ',', $send_to_cc);
335 $sendcc = explode(',', $send_to_cc);
338 for ($i = 0; $i < count($sendcc); $i++
) {
339 $sendcc[$i] = trim($sendcc[$i]);
340 if ($sendcc[$i] == '') {
344 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
345 $whofrom = sqimap_find_displayable_name($msg['HEADER']['FROM']);
346 $whoreplyto = sqimap_find_email($msg['HEADER']['REPLYTO']);
348 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
349 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
350 (trim($sendcc[$i]) != '')) {
351 $send_to_cc .= trim($sendcc[$i]) . ', ';
354 $send_to_cc = trim($send_to_cc);
355 if (substr($send_to_cc, -1) == ',') {
356 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
359 } /* function newMail() */
362 function getAttachments($message) {
363 global $mailbox, $attachments, $attachment_dir, $imapConnection,
364 $ent_num, $forward_id, $draft_id, $username;
366 if (isset($draft_id)) {
373 sqimap_mailbox_select($imapConnection, $mailbox);
374 $message = sqimap_get_message($imapConnection, $id, $mailbox);
377 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
378 if (count($message->entities
) == 0) {
379 if ($message->header
->entity_id
!= $ent_num) {
380 $filename = decodeHeader($message->header
->filename
);
382 if ($filename == "") {
383 $filename = "untitled-".$message->header
->entity_id
;
386 $localfilename = GenerateRandomString(32, '', 7);
387 $full_localfilename = "$hashed_attachment_dir/$localfilename";
388 while (file_exists($full_localfilename)) {
389 $localfilename = GenerateRandomString(32, '', 7);
390 $full_localfilename = "$hashed_attachment_dir/$localfilename";
393 $newAttachment = array();
394 $newAttachment['localfilename'] = $localfilename;
395 $newAttachment['remotefilename'] = $filename;
396 $newAttachment['type'] = strtolower($message->header
->type0
.
397 '/' . $message->header
->type1
);
399 /* Write Attachment to file */
400 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'w');
401 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
402 $id, $message->header
->entity_id
),
403 $message->header
->encoding
));
406 $attachments[] = $newAttachment;
409 for ($i = 0; $i < count($message->entities
); $i++
) {
410 getAttachments($message->entities
[$i]);
416 function showInputForm () {
417 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
418 $passed_body, $color, $use_signature, $signature, $prefix_sig,
419 $editor_size, $attachments, $subject, $newmail,
420 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
421 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
422 $username, $data_dir, $identity, $draft_id, $delete_draft,
423 $mailprio, $default_use_mdn, $mdn_user_support;
425 $subject = decodeHeader($subject, false);
426 $reply_subj = decodeHeader($reply_subj, false);
427 $forward_subj = decodeHeader($forward_subj, false);
429 if ($use_javascript_addr_book) {
430 echo "\n". '<SCRIPT LANGUAGE=JavaScript><!--' . "\n" .
431 'function open_abook() { ' . "\n" .
432 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
433 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
434 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
435 ' nwin.opener = document.windows;' . "\n" .
437 '// --></SCRIPT>' . "\n\n";
440 echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
441 'ENCTYPE="multipart/form-data"';
442 do_hook("compose_form");
447 if (isset($draft_id)) {
448 echo '<input type="hidden" name="delete_draft" value="' . $draft_id . "\">\n";
450 if (isset($delete_draft)) {
451 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
454 echo '<TABLE WIDTH="100%" ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
456 if ($location_of_buttons == 'top') {
457 showComposeButtonRow();
460 $idents = getPref($data_dir, $username, 'identities', 0);
462 echo ' <TR>' . "\n" .
463 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
466 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
467 '<select name=identity>' . "\n" .
468 '<option value=default>' .
469 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
470 $em = getPref($data_dir, $username, 'email_address');
472 echo htmlspecialchars(' <' . $em . '>') . "\n";
474 for ($i = 1; $i < $idents; $i ++
) {
475 echo '<option value="' . $i . '"';
476 if (isset($identity) && $identity == $i) {
479 echo '>' . htmlspecialchars(getPref($data_dir, $username,
481 $em = getPref($data_dir, $username, 'email_address' . $i);
483 echo htmlspecialchars(' <' . $em . '>') . "\n";
487 echo '</select>' . "\n" .
491 echo ' <TR>' . "\n" .
492 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' . "\n" .
494 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
495 ' <INPUT TYPE=text NAME="send_to" VALUE="' .
496 htmlspecialchars($send_to) . '" SIZE=60><BR>' . "\n" .
500 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
502 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
503 ' <INPUT TYPE=text NAME="send_to_cc" SIZE=60 VALUE="' .
504 htmlspecialchars($send_to_cc) . '"><BR>' . "\n" .
508 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
510 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
511 ' <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
512 htmlspecialchars($send_to_bcc) . '" SIZE=60><BR>' . "\n" .
513 '</TD></TR>' . "\n" .
515 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
517 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n";
519 $reply_subj = str_replace('"', "'", $reply_subj);
520 $reply_subj = trim($reply_subj);
521 if (substr(strtolower($reply_subj), 0, 3) != 're:') {
522 $reply_subj = 'Re: ' . $reply_subj;
524 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
525 htmlspecialchars($reply_subj) . '">';
527 elseif ($forward_subj) {
528 $forward_subj = trim($forward_subj);
529 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
530 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
531 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:')) {
532 $forward_subj = '[Fwd: ' . $forward_subj . ']';
534 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
535 htmlspecialchars($forward_subj) . '">';
537 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
538 htmlspecialchars($subject) . '">';
540 echo '</td></tr>' . "\n\n";
542 if ($location_of_buttons == 'between') {
543 showComposeButtonRow();
546 echo ' <TR>' . "\n" .
547 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
548 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
549 $editor_size . '" WRAP=HARD>' . htmlspecialchars($body);
550 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
551 echo "\n\n".($prefix_sig==true?
"-- \n":'').htmlspecialchars($signature);
553 echo '</TEXTAREA><BR>' . "\n" .
557 if ($location_of_buttons == 'bottom') {
558 showComposeButtonRow();
560 echo ' <TR><TD COLSPAN=2 ALIGN=LEFT>';
561 echo ' <INPUT TYPE=SUBMIT NAME=send VALUE="' . _("Send") . '"></TD></TR>' . "\n";
564 /* This code is for attachments */
565 echo ' <TR>' . "\n" .
566 ' <TD VALIGN=MIDDLE ALIGN=RIGHT>' . "\n" .
569 ' <TD VALIGN=MIDDLE ALIGN=LEFT>' . "\n" .
570 ' <INPUT NAME="attachfile" SIZE=48 TYPE="file">' . "\n" .
571 ' <input type="submit" name="attach"' .
572 ' value="' . _("Add") .'">' . "\n" .
576 if (count($attachments)) {
577 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
578 echo '<tr><td bgcolor="' . $color[0] . '" align=right>' . "\n" .
580 '</td><td align=left bgcolor="' . $color[0] . '">';
581 foreach ($attachments as $key => $info) {
582 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
583 echo '<input type="checkbox" name="delete[]" value="' . $key . "\">\n" .
584 $info['remotefilename'] . ' - ' . $info['type'] . ' (' .
585 show_readable_size(filesize($attached_file)) . ")<br>\n";
588 echo '<input type="submit" name="do_delete" value="' .
589 _("Delete selected attachments") . "\">\n" .
592 /* End of attachment code */
594 echo '</TABLE>' . "\n";
596 echo '<input type=hidden name=reply_id value=' . $reply_id . ">\n";
598 echo '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
601 do_hook('compose_bottom');
602 echo '</BODY></HTML>' . "\n";
606 function showComposeButtonRow() {
607 global $use_javascript_addr_book, $save_as_draft,
608 $default_use_priority, $mailprio, $default_use_mdn,
609 $data_dir, $username;
611 echo " <TR><TD>\n</TD><TD>\n";
612 if ($default_use_priority) {
613 if(!isset($mailprio)) {
616 echo _("Priority") .': <select name="mailprio">'.
617 '<option value="1"'.($mailprio=='1'?
' selected':'').'>'. _("High") .'</option>'.
618 '<option value="3"'.($mailprio=='3'?
' selected':'').'>'. _("Normal") .'</option>'.
619 '<option value="5"'.($mailprio=='5'?
' selected':'').'>'. _("Low").'</option>'.
622 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
623 if ($default_use_mdn) {
624 if ($mdn_user_support) {
625 echo "\n\t". _("Receipt") .': '.
626 '<input type="checkbox" name="request_mdn" value=1>'. _("On read").
627 ' <input type="checkbox" name="request_dr" value=1>'. _("On Delivery");
631 echo " <TR><td>\n </td><td>\n";
632 if ($use_javascript_addr_book) {
633 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"".
634 " <input type=button value=\\\""._("Addresses").
635 "\\\" onclick='javascript:open_abook();'>\");".
636 " // --></SCRIPT><NOSCRIPT>\n".
637 " <input type=submit name=\"html_addr_search\" value=\"".
638 _("Addresses")."\">".
641 echo " <input type=submit name=\"html_addr_search\" value=\"".
642 _("Addresses")."\">";
644 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
646 if ($save_as_draft) {
647 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
650 do_hook('compose_button_row');
652 echo " </TD></TR>\n\n";
655 function checkInput ($show) {
657 * I implemented the $show variable because the error messages
658 * were getting sent before the page header. So, I check once
659 * using $show=false, and then when i'm ready to display the error
662 global $body, $send_to, $subject, $color;
664 if ($send_to == "") {
666 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
671 } /* function checkInput() */
674 /* True if FAILURE */
675 function saveAttachedFiles() {
676 global $HTTP_POST_FILES, $attachment_dir, $attachments, $username;
678 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
679 $localfilename = GenerateRandomString(32, '', 7);
680 $full_localfilename = "$hashed_attachment_dir/$localfilename";
681 while (file_exists($full_localfilename)) {
682 $localfilename = GenerateRandomString(32, '', 7);
683 $full_localfilename = "$hashed_attachment_dir/$localfilename";
686 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
687 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
692 $newAttachment['localfilename'] = $localfilename;
693 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
694 $newAttachment['type'] = strtolower($HTTP_POST_FILES['attachfile']['type']);
696 if ($newAttachment['type'] == "") {
697 $newAttachment['type'] = 'application/octet-stream';
700 $attachments[] = $newAttachment;
706 function ClearAttachments()
708 global $username, $attachments, $attachment_dir;
709 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
711 foreach ($attachments as $info) {
712 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
713 if (file_exists($attached_file)) {
714 unlink($attached_file);
718 $attachments = array();
721 function getReplyCitation($orig_from)
723 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
725 /* First, return an empty string when no citation style selected. */
726 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
730 /* Decode the users name. */
731 $parpos = strpos($orig_from, '(');
732 if ($parpos === false) {
733 $orig_from = trim(substr($orig_from, 0, strpos($orig_from, '<')));
734 $orig_from = str_replace('"', '', $orig_from);
735 $orig_from = str_replace("'", '', $orig_from);
737 $end_parpos = strrpos($orig_from, ')');
738 $end_parpos -= ($end_parpos === false ?
$end_parpos : $parpos +
1);
739 $orig_from = trim(substr($orig_from, $parpos +
1, $end_parpos));
742 /* Make sure our final value isn't an empty string. */
743 if ($orig_from == '') {
747 /* Otherwise, try to select the desired citation style. */
748 switch ($reply_citation_style) {
751 $end = ' ' . _("said") . ':';
754 $start = '<' . _("quote") . ' ' . _("who") . '="';
758 $start = $reply_citation_start;
759 $end = $reply_citation_end;
765 /* Build and return the citation string. */
766 return ($start . $orig_from . $end . "\n");