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 require_once ('../src/draft_actions.php');
39 if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id)) {
44 $draft_message = _("Draft Email Saved");
45 /* If this is a resumed draft, then delete the original */
46 if(isset($delete_draft)) {
47 Header("Location: delete_message.php?mailbox=$draft_folder".
48 "&message=$delete_draft&sort=$sort&startMessage=1");
52 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort".
53 "&startMessage=1¬e=$draft_message");
60 if (isset($HTTP_POST_FILES['attachfile']) &&
61 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
62 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none') {
63 $AttachFailure = saveAttachedFiles();
65 if (checkInput(false) && !isset($AttachFailure)) {
66 $urlMailbox = urlencode (trim($mailbox));
67 if (! isset($reply_id)) {
71 * Set $default_charset to correspond with the user's selection
72 * of language interface.
77 * This is to change all newlines to \n
78 * We'll change them to \r\n later (in the sendMessage function)
80 $body = str_replace("\r\n", "\n", $body);
81 $body = str_replace("\r", "\n", $body);
84 * Rewrap $body so that no line is bigger than $editor_size
85 * This should only really kick in the sqWordWrap function
86 * if the browser doesn't support "HARD" as the wrap type
87 * Or, in Opera's case, something goes wrong.
89 $body = explode("\n", $body);
91 foreach ($body as $line) {
92 if( $line <> '-- ' ) {
95 if (strlen($line) <= $editor_size +
1) {
96 $newBody .= $line . "\n";
99 sqWordWrap($line, $editor_size) . "\n";
105 do_hook("compose_send");
107 if (! isset($mailprio)) {
108 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
109 $subject, $body, $reply_id);
112 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
113 $subject, $body, $reply_id, $mailprio);
119 if ( isset($delete_draft)) {
120 Header("Location: delete_message.php?mailbox=$draft_folder".
121 "&message=$delete_draft&sort=$sort&startMessage=1");
125 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort".
130 *$imapConnection = sqimap_login($username, $key, $imapServerAddress,
133 displayPageHeader($color, $mailbox);
135 if (isset($AttachFailure)) {
136 plain_error_message(_("Could not move/copy file. File not attached"),
142 /* sqimap_logout($imapConnection); */
145 elseif (isset($html_addr_search_done)) {
146 displayPageHeader($color, $mailbox);
148 if (isset($send_to_search) && is_array($send_to_search)) {
149 foreach ($send_to_search as $k => $v) {
150 if (substr($k, 0, 1) == 'T') {
156 elseif (substr($k, 0, 1) == 'C') {
162 elseif (substr($k, 0, 1) == 'B') {
164 $send_to_bcc .= ', ';
172 elseif (isset($html_addr_search)) {
173 if (isset($HTTP_POST_FILES['attachfile']) &&
174 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
175 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none') {
176 if (saveAttachedFiles()) {
177 plain_error_message(_("Could not move/copy file. File not attached"), $color);
181 * I am using an include so as to elminiate an extra unnecessary
182 * click. If you can think of a better way, please implement it.
184 include_once('./addrbook_search_html.php');
186 elseif (isset($attach)) {
187 if (saveAttachedFiles()) {
188 plain_error_message(_("Could not move/copy file. File not attached"), $color);
190 displayPageHeader($color, $mailbox);
193 elseif (isset($do_delete)) {
194 displayPageHeader($color, $mailbox);
196 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
197 if (isset($delete) && is_array($delete)) {
198 foreach($delete as $index) {
199 $attached_file = $hashed_attachment_dir . '/'
200 . $attachments[$index]['localfilename'];
201 unlink ($attached_file);
202 unset ($attachments[$index]);
210 * This handles the default case as well as the error case
211 * (they had the same code) --> if (isset($smtpErrors))
213 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
215 displayPageHeader($color, $mailbox);
221 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num) {
225 if (isset($draft_id) && $draft_id && isset($ent_num) && $ent_num) {
231 sqimap_logout($imapConnection);
237 /**************** Only function definitions go below *************/
240 /* This function is used when not sending or adding attachments */
241 function newMail () {
242 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
243 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size,
244 $draft_id, $use_signature;
246 $send_to = decodeHeader($send_to);
247 $send_to_cc = decodeHeader($send_to_cc);
248 $send_to_bcc = decodeHeader($send_to_bcc);
252 } elseif ($reply_id) {
258 $use_signature = FALSE;
262 sqimap_mailbox_select($imapConnection, $mailbox);
263 $message = sqimap_get_message($imapConnection, $id, $mailbox);
264 $orig_header = $message->header
;
266 $message = getEntity($message, $ent_num);
268 if ($message->header
->type0
== 'text' ||
269 $message->header
->type1
== 'message') {
272 mime_fetch_body($imapConnection, $id, $ent_num),
273 $message->header
->encoding
);
276 mime_fetch_body($imapConnection, $id, 1),
277 $message->header
->encoding
);
284 if ($message->header
->type1
== 'html') {
285 $body = strip_tags($body);
289 $body_ary = explode("\n", $body);
290 $i = count($body_ary) - 1;
291 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
292 unset($body_ary[$i]);
296 for ($i=0; isset($body_ary[$i]); $i++
) {
298 if (ereg('^[ >]+', $body_ary[$i])) {
299 $body_ary[$i] = '>' . $body_ary[$i];
302 $body_ary[$i] = '> ' . $body_ary[$i];
306 sqWordWrap($body_ary[$i], $editor_size - 1);
308 $body .= $body_ary[$i] . "\n";
309 unset($body_ary[$i]);
312 $bodyTop = '-------- ' . _("Original Message") . " --------\n" .
313 _("Subject") . ': ' . $orig_header->subject
. "\n" .
314 _("From") . ': ' . $orig_header->from
. "\n" .
316 getLongDateString( $orig_header->date
). "\n" .
317 _("To") . ': ' . $orig_header->to
[0] . "\n";
318 if (count($orig_header->to
) > 1) {
319 for ($x=1; $x < count($orig_header->to
); $x++
) {
320 $bodyTop .= ' ' . $orig_header->to
[$x] . "\n";
324 $body = $bodyTop . $body;
327 $orig_from = decodeHeader($orig_header->from
);
328 $body = getReplyCitation($orig_from) . $body;
335 $send_to = sqimap_find_email($send_to);
338 /* This formats a CC string if they hit "reply all" */
339 if ($send_to_cc != '') {
340 $send_to_cc = ereg_replace('"[^"]*"', '', $send_to_cc);
341 $send_to_cc = ereg_replace(';', ',', $send_to_cc);
342 $sendcc = explode(',', $send_to_cc);
345 for ($i = 0; $i < count($sendcc); $i++
) {
346 $sendcc[$i] = trim($sendcc[$i]);
347 if ($sendcc[$i] == '') {
351 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
352 $whofrom = sqimap_find_displayable_name($msg['HEADER']['FROM']);
353 $whoreplyto = sqimap_find_email($msg['HEADER']['REPLYTO']);
355 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
356 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
357 (trim($sendcc[$i]) != '')) {
358 $send_to_cc .= trim($sendcc[$i]) . ', ';
361 $send_to_cc = trim($send_to_cc);
362 if (substr($send_to_cc, -1) == ',') {
363 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
366 } /* function newMail() */
369 function getAttachments($message) {
370 global $mailbox, $attachments, $attachment_dir, $imapConnection,
371 $ent_num, $forward_id, $draft_id, $username;
373 if (isset($draft_id)) {
381 sqimap_mailbox_select($imapConnection, $mailbox);
382 $message = sqimap_get_message($imapConnection, $id, $mailbox);
385 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
386 if (count($message->entities
) == 0) {
387 if ($message->header
->entity_id
!= $ent_num) {
388 $filename = decodeHeader($message->header
->filename
);
390 if ($filename == "") {
391 $filename = "untitled-".$message->header
->entity_id
;
394 $localfilename = GenerateRandomString(32, '', 7);
395 $full_localfilename = "$hashed_attachment_dir/$localfilename";
396 while (file_exists($full_localfilename)) {
397 $localfilename = GenerateRandomString(32, '', 7);
398 $full_localfilename = "$hashed_attachment_dir/$localfilename";
401 $newAttachment = array();
402 $newAttachment['localfilename'] = $localfilename;
403 $newAttachment['remotefilename'] = $filename;
404 $newAttachment['type'] = strtolower($message->header
->type0
.
405 '/' . $message->header
->type1
);
407 /* Write Attachment to file */
408 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'w');
409 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
410 $id, $message->header
->entity_id
),
411 $message->header
->encoding
));
414 $attachments[] = $newAttachment;
418 for ($i = 0; $i < count($message->entities
); $i++
) {
419 getAttachments($message->entities
[$i]);
425 function showInputForm () {
426 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
427 $passed_body, $color, $use_signature, $signature, $prefix_sig,
428 $editor_size, $attachments, $subject, $newmail,
429 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
430 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
431 $username, $data_dir, $identity, $draft_id, $delete_draft,
434 $subject = decodeHeader($subject);
435 $reply_subj = decodeHeader($reply_subj);
436 $forward_subj = decodeHeader($forward_subj);
438 if ($use_javascript_addr_book) {
439 echo "\n". '<SCRIPT LANGUAGE=JavaScript><!--' . "\n" .
440 'function open_abook() { ' . "\n" .
441 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
442 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
443 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
444 ' nwin.opener = document.windows;' . "\n" .
446 '// --></SCRIPT>' . "\n\n";
449 echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
450 'ENCTYPE="multipart/form-data"';
451 do_hook("compose_form");
454 if (isset($draft_id)) {
455 echo '<input type="hidden" name="delete_draft" value="' . $draft_id . "\">\n";
457 if (isset($delete_draft)) {
458 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
461 echo '<TABLE WIDTH="100%" ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
463 if ($location_of_buttons == 'top') {
464 showComposeButtonRow();
467 $idents = getPref($data_dir, $username, 'identities');
468 if ($idents != '' && $idents > 1) {
469 echo ' <TR>' . "\n" .
470 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
473 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
474 '<select name=identity>' . "\n" .
475 '<option value=default>' .
476 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
477 $em = getPref($data_dir, $username, 'email_address');
479 echo htmlspecialchars(' <' . $em . '>') . "\n";
481 for ($i = 1; $i < $idents; $i ++
) {
482 echo '<option value="' . $i . '"';
483 if (isset($identity) && $identity == $i) {
486 echo '>' . htmlspecialchars(getPref($data_dir, $username,
488 $em = getPref($data_dir, $username, 'email_address' . $i);
490 echo htmlspecialchars(' <' . $em . '>') . "\n";
493 echo '</select>' . "\n" .
497 echo ' <TR>' . "\n" .
498 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' . "\n" .
500 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
501 ' <INPUT TYPE=text NAME="send_to" VALUE="' .
502 htmlspecialchars($send_to) . '" SIZE=60><BR>' . "\n" .
506 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
508 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
509 ' <INPUT TYPE=text NAME="send_to_cc" SIZE=60 VALUE="' .
510 htmlspecialchars($send_to_cc) . '"><BR>' . "\n" .
514 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
516 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
517 ' <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
518 htmlspecialchars($send_to_bcc) . '" SIZE=60><BR>' . "\n" .
519 '</TD></TR>' . "\n" .
521 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
523 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n";
525 $reply_subj = str_replace('"', "'", $reply_subj);
526 $reply_subj = trim($reply_subj);
527 if (substr(strtolower($reply_subj), 0, 3) != 're:') {
528 $reply_subj = 'Re: ' . $reply_subj;
530 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
531 htmlspecialchars($reply_subj) . '">';
533 elseif ($forward_subj) {
534 $forward_subj = trim($forward_subj);
535 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
536 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
537 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:')) {
538 $forward_subj = '[Fwd: ' . $forward_subj . ']';
540 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
541 htmlspecialchars($forward_subj) . '">';
544 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
545 htmlspecialchars($subject) . '">';
547 echo '</td></tr>' . "\n\n";
549 if ($location_of_buttons == 'between') {
550 showComposeButtonRow();
553 echo ' <TR>' . "\n" .
554 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
555 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
556 $editor_size . '" WRAP=HARD>' . htmlspecialchars($body);
557 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
558 if ( $prefix_sig == true ) {
559 echo "\n\n-- \n" . htmlspecialchars($signature);
561 echo "\n\n" . htmlspecialchars($signature);
564 echo '</TEXTAREA><BR>' . "\n" .
568 if ($location_of_buttons == 'bottom') {
569 showComposeButtonRow();
571 echo ' <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT ' .
572 'NAME=send VALUE="' . _("Send") . '"></TD></TR>' . "\n";
575 /* This code is for attachments */
576 echo ' <TR>' . "\n" .
577 ' <TD VALIGN=MIDDLE ALIGN=RIGHT>' . "\n" .
580 ' <TD VALIGN=MIDDLE ALIGN=LEFT>' . "\n" .
581 ' <INPUT NAME="attachfile" SIZE=48 TYPE="file">' . "\n" .
582 ' <input type="submit" name="attach"' .
583 ' value="' . _("Add") .'">' . "\n" .
586 if (count($attachments)) {
587 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
588 echo '<tr><td bgcolor="' . $color[0] . '" align=right>' . "\n" .
590 '</td><td align=left bgcolor="' . $color[0] . '">';
591 foreach ($attachments as $key => $info) {
592 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
593 echo '<input type="checkbox" name="delete[]" value="' . $key . "\">\n" .
594 $info['remotefilename'] . ' - ' . $info['type'] . ' (' .
595 show_readable_size(filesize($attached_file)) . ")<br>\n";
598 echo '<input type="submit" name="do_delete" value="' .
599 _("Delete selected attachments") . "\">\n" .
602 /* End of attachment code */
604 echo '</TABLE>' . "\n";
606 echo '<input type=hidden name=reply_id value=' . $reply_id . ">\n";
608 echo '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
611 do_hook("compose_bottom");
612 echo '</BODY></HTML>' . "\n";
616 function showComposeButtonRow()
618 global $use_javascript_addr_book, $save_as_draft,
619 $default_use_priority, $mailprio;
621 echo " <TR><td>\n </td><td>\n";
622 if ($use_javascript_addr_book) {
623 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
624 echo " <input type=button value=\\\""._("Addresses").
625 "\\\" onclick='javascript:open_abook();'>\");";
626 echo " // --></SCRIPT><NOSCRIPT>\n";
627 echo " <input type=submit name=\"html_addr_search\" value=\"".
628 _("Addresses")."\">";
629 echo " </NOSCRIPT>\n";
632 echo " <input type=submit name=\"html_addr_search\" value=\"".
633 _("Addresses")."\">";
635 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
637 if ($save_as_draft) {
638 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
640 if ($default_use_priority) {
641 if(!isset($mailprio)) {
644 echo "\n\t". _("Priority") .':<select name="mailprio">'.
645 "\n\t\t<option value=1".($mailprio=='1'?
' selected':'').'>'. _("High") .'</option>'.
646 "\n\t\t<option value=3".($mailprio=='3'?
' selected':'').'>'. _("Normal") .'</option>'.
647 "\n\t\t<option value=5".($mailprio=='5'?
' selected':'').'>'. _("Low").'</option>'.
651 do_hook('compose_button_row');
657 function checkInput ($show)
660 * I implemented the $show variable because the error messages
661 * were getting sent before the page header. So, I check once
662 * using $show=false, and then when i'm ready to display the error
665 global $body, $send_to, $subject, $color;
667 if ($send_to == "") {
669 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
674 } /* function checkInput() */
677 /* True if FAILURE */
678 function saveAttachedFiles() {
679 global $HTTP_POST_FILES, $attachment_dir, $attachments, $username;
681 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
682 $localfilename = GenerateRandomString(32, '', 7);
683 $full_localfilename = "$hashed_attachment_dir/$localfilename";
684 while (file_exists($full_localfilename)) {
685 $localfilename = GenerateRandomString(32, '', 7);
686 $full_localfilename = "$hashed_attachment_dir/$localfilename";
689 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
690 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
695 $newAttachment['localfilename'] = $localfilename;
696 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
697 $newAttachment['type'] = strtolower($HTTP_POST_FILES['attachfile']['type']);
699 if ($newAttachment['type'] == "") {
700 $newAttachment['type'] = 'application/octet-stream';
703 $attachments[] = $newAttachment;
707 function ClearAttachments()
709 global $username, $attachments, $attachment_dir;
710 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
712 foreach ($attachments as $info) {
713 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
714 if (file_exists($attached_file)) {
715 unlink($attached_file);
719 $attachments = array();
722 function getReplyCitation($orig_from)
724 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
726 /* First, return an empty string when no citation style selected. */
727 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
731 /* Decode the users name. */
732 $parpos = strpos($orig_from, '(');
733 if ($parpos === false) {
734 $orig_from = trim(substr($orig_from, 0, strpos($orig_from, '<')));
735 $orig_from = str_replace('"', '', $orig_from);
736 $orig_from = str_replace("'", '', $orig_from);
739 $end_parpos = strrpos($orig_from, ')');
740 $end_parpos -= ($end_parpos === false ?
$end_parpos : $parpos +
1);
741 $orig_from = trim(substr($orig_from, $parpos +
1, $end_parpos));
744 /* Make sure our final value isn't an empty string. */
745 if ($orig_from == '') {
749 /* Otherwise, try to select the desired citation style. */
750 switch ($reply_citation_style) {
753 $end = ' ' . _("said") . ':';
756 $start = '<' . _("quote") . ' ' . _("who") . '="';
760 $start = $reply_citation_start;
761 $end = $reply_citation_end;
767 /* Build and return the citation string. */
768 return ($start . $orig_from . $end . "\n");