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 /*****************************************************************/
21 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
22 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
23 /*** + Base level indent should begin at left margin, as ***/
24 /*** the require_once below looks. ***/
25 /*** + All identation should consist of four space blocks ***/
26 /*** + Tab characters are evil. ***/
27 /*** + all comments should use "slash-star ... star-slash" ***/
28 /*** style -- no pound characters, no slash-slash style ***/
29 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
30 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
31 /*** + Please use ' instead of ", when possible. Note " ***/
32 /*** should always be used in _( ) function calls. ***/
33 /*** Thank you for your help making the SM code more readable. ***/
34 /*****************************************************************/
36 require_once('../src/validate.php');
37 require_once('../functions/imap.php');
38 require_once('../functions/date.php');
39 require_once('../functions/mime.php');
40 require_once('../functions/smtp.php');
41 require_once('../functions/display_messages.php');
42 require_once('../functions/plugin.php');
44 if (!isset($attachments)) {
45 $attachments = array();
46 session_register('attachments');
49 /* This function is used when not sending or adding attachments */
51 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
52 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size,
53 $draft_id, $use_signature;
55 $send_to = decodeHeader($send_to);
56 $send_to_cc = decodeHeader($send_to_cc);
57 $send_to_bcc = decodeHeader($send_to_bcc);
61 } elseif ($reply_id) {
67 $use_signature = FALSE;
71 sqimap_mailbox_select($imapConnection, $mailbox);
72 $message = sqimap_get_message($imapConnection, $id, $mailbox);
73 $orig_header = $message->header
;
75 $message = getEntity($message, $ent_num);
77 if ($message->header
->type0
== 'text' ||
78 $message->header
->type1
== 'message') {
81 mime_fetch_body($imapConnection, $id, $ent_num),
82 $message->header
->encoding
);
85 mime_fetch_body($imapConnection, $id, 1),
86 $message->header
->encoding
);
93 if ($message->header
->type1
== 'html') {
94 $body = strip_tags($body);
98 $body_ary = explode("\n", $body);
99 $i = count($body_ary) - 1;
100 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
101 unset($body_ary[$i]);
105 for ($i=0; isset($body_ary[$i]); $i++
) {
107 if (ereg('^[ >]+', $body_ary[$i])) {
108 $body_ary[$i] = '>' . $body_ary[$i];
111 $body_ary[$i] = '> ' . $body_ary[$i];
115 sqWordWrap($body_ary[$i], $editor_size - 1);
117 $body .= $body_ary[$i] . "\n";
118 unset($body_ary[$i]);
121 $bodyTop = '-------- ' . _("Original Message") . " --------\n" .
122 _("Subject") . ': ' . $orig_header->subject
. "\n" .
123 _("From") . ': ' . $orig_header->from
. "\n" .
125 getLongDateString( $orig_header->date
). "\n" .
126 _("To") . ': ' . $orig_header->to
[0] . "\n";
127 if (count($orig_header->to
) > 1) {
128 for ($x=1; $x < count($orig_header->to
); $x++
) {
129 $bodyTop .= ' ' . $orig_header->to
[$x] . "\n";
133 $body = $bodyTop . $body;
136 $orig_from = decodeHeader($orig_header->from
);
137 $body = getReplyCitation($orig_from) . $body;
144 $send_to = sqimap_find_email($send_to);
147 /* This formats a CC string if they hit "reply all" */
148 if ($send_to_cc != '') {
149 $send_to_cc = ereg_replace('"[^"]*"', '', $send_to_cc);
150 $send_to_cc = ereg_replace(';', ',', $send_to_cc);
151 $sendcc = explode(',', $send_to_cc);
154 for ($i = 0; $i < count($sendcc); $i++
) {
155 $sendcc[$i] = trim($sendcc[$i]);
156 if ($sendcc[$i] == '') {
160 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
161 $whofrom = sqimap_find_displayable_name($msg['HEADER']['FROM']);
162 $whoreplyto = sqimap_find_email($msg['HEADER']['REPLYTO']);
164 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
165 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
166 (trim($sendcc[$i]) != '')) {
167 $send_to_cc .= trim($sendcc[$i]) . ', ';
170 $send_to_cc = trim($send_to_cc);
171 if (substr($send_to_cc, -1) == ',') {
172 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
175 } /* function newMail() */
178 function getAttachments($message) {
179 global $mailbox, $attachments, $attachment_dir, $imapConnection,
180 $ent_num, $forward_id, $draft_id, $username;
182 if (isset($draft_id)) {
190 sqimap_mailbox_select($imapConnection, $mailbox);
191 $message = sqimap_get_message($imapConnection, $id, $mailbox);
194 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
195 if (count($message->entities
) == 0) {
196 if ($message->header
->entity_id
!= $ent_num) {
197 $filename = decodeHeader($message->header
->filename
);
199 if ($filename == "") {
200 $filename = "untitled-".$message->header
->entity_id
;
203 $localfilename = GenerateRandomString(32, '', 7);
204 $full_localfilename = "$hashed_attachment_dir/$localfilename";
205 while (file_exists($full_localfilename)) {
206 $localfilename = GenerateRandomString(32, '', 7);
207 $full_localfilename = "$hashed_attachment_dir/$localfilename";
210 $newAttachment = array();
211 $newAttachment['localfilename'] = $localfilename;
212 $newAttachment['remotefilename'] = $filename;
213 $newAttachment['type'] = strtolower($message->header
->type0
.
214 '/' . $message->header
->type1
);
216 /* Write Attachment to file */
217 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'w');
218 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
219 $id, $message->header
->entity_id
),
220 $message->header
->encoding
));
223 $attachments[] = $newAttachment;
227 for ($i = 0; $i < count($message->entities
); $i++
) {
228 getAttachments($message->entities
[$i]);
234 function showInputForm () {
235 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
236 $passed_body, $color, $use_signature, $signature, $prefix_sig,
237 $editor_size, $attachments, $subject, $newmail,
238 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
239 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
240 $username, $data_dir, $identity, $draft_id, $delete_draft,
243 $subject = decodeHeader($subject);
244 $reply_subj = decodeHeader($reply_subj);
245 $forward_subj = decodeHeader($forward_subj);
247 if ($use_javascript_addr_book) {
248 echo "\n". '<SCRIPT LANGUAGE=JavaScript><!--' . "\n" .
249 'function open_abook() { ' . "\n" .
250 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
251 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
252 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
253 ' nwin.opener = document.windows;' . "\n" .
255 '// --></SCRIPT>' . "\n\n";
258 echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
259 'ENCTYPE="multipart/form-data"';
260 do_hook("compose_form");
263 if (isset($draft_id)) {
264 echo '<input type="hidden" name="delete_draft" value="' . $draft_id . "\">\n";
266 if (isset($delete_draft)) {
267 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
270 echo '<TABLE WIDTH="100%" ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
272 if ($location_of_buttons == 'top') {
273 showComposeButtonRow();
276 $idents = getPref($data_dir, $username, 'identities');
277 if ($idents != '' && $idents > 1) {
278 echo ' <TR>' . "\n" .
279 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
282 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
283 '<select name=identity>' . "\n" .
284 '<option value=default>' .
285 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
286 $em = getPref($data_dir, $username, 'email_address');
288 echo htmlspecialchars(' <' . $em . '>') . "\n";
290 for ($i = 1; $i < $idents; $i ++
) {
291 echo '<option value="' . $i . '"';
292 if (isset($identity) && $identity == $i) {
295 echo '>' . htmlspecialchars(getPref($data_dir, $username,
297 $em = getPref($data_dir, $username, 'email_address' . $i);
299 echo htmlspecialchars(' <' . $em . '>') . "\n";
302 echo '</select>' . "\n" .
306 echo ' <TR>' . "\n" .
307 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' . "\n" .
309 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
310 ' <INPUT TYPE=text NAME="send_to" VALUE="' .
311 htmlspecialchars($send_to) . '" SIZE=60><BR>' . "\n" .
315 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
317 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
318 ' <INPUT TYPE=text NAME="send_to_cc" SIZE=60 VALUE="' .
319 htmlspecialchars($send_to_cc) . '"><BR>' . "\n" .
323 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
325 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
326 ' <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
327 htmlspecialchars($send_to_bcc) . '" SIZE=60><BR>' . "\n" .
328 '</TD></TR>' . "\n" .
330 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
332 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n";
334 $reply_subj = str_replace('"', "'", $reply_subj);
335 $reply_subj = trim($reply_subj);
336 if (substr(strtolower($reply_subj), 0, 3) != 're:') {
337 $reply_subj = 'Re: ' . $reply_subj;
339 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
340 htmlspecialchars($reply_subj) . '">';
342 elseif ($forward_subj) {
343 $forward_subj = trim($forward_subj);
344 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
345 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
346 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:')) {
347 $forward_subj = '[Fwd: ' . $forward_subj . ']';
349 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
350 htmlspecialchars($forward_subj) . '">';
353 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
354 htmlspecialchars($subject) . '">';
356 echo '</td></tr>' . "\n\n";
358 if ($location_of_buttons == 'between') {
359 showComposeButtonRow();
362 echo ' <TR>' . "\n" .
363 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
364 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
365 $editor_size . '" WRAP=HARD>' . htmlspecialchars($body);
366 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
367 if ( $prefix_sig == true ) {
368 echo "\n\n-- \n" . htmlspecialchars($signature);
370 echo "\n\n" . htmlspecialchars($signature);
373 echo '</TEXTAREA><BR>' . "\n" .
377 if ($location_of_buttons == 'bottom') {
378 showComposeButtonRow();
380 echo ' <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT ' .
381 'NAME=send VALUE="' . _("Send") . '"></TD></TR>' . "\n";
384 /* This code is for attachments */
385 echo ' <TR>' . "\n" .
386 ' <TD VALIGN=MIDDLE ALIGN=RIGHT>' . "\n" .
389 ' <TD VALIGN=MIDDLE ALIGN=LEFT>' . "\n" .
390 ' <INPUT NAME="attachfile" SIZE=48 TYPE="file">' . "\n" .
391 ' <input type="submit" name="attach"' .
392 ' value="' . _("Add") .'">' . "\n" .
395 if (count($attachments)) {
396 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
397 echo '<tr><td bgcolor="' . $color[0] . '" align=right>' . "\n" .
399 '</td><td align=left bgcolor="' . $color[0] . '">';
400 foreach ($attachments as $key => $info) {
401 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
402 echo '<input type="checkbox" name="delete[]" value="' . $key . "\">\n" .
403 $info['remotefilename'] . ' - ' . $info['type'] . ' (' .
404 show_readable_size(filesize($attached_file)) . ")<br>\n";
407 echo '<input type="submit" name="do_delete" value="' .
408 _("Delete selected attachments") . "\">\n" .
411 /* End of attachment code */
413 echo '</TABLE>' . "\n";
415 echo '<input type=hidden name=reply_id value=' . $reply_id . ">\n";
417 echo '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
420 do_hook("compose_bottom");
421 echo '</BODY></HTML>' . "\n";
425 function showComposeButtonRow()
427 global $use_javascript_addr_book, $save_as_draft,
428 $default_use_priority, $mailprio;
430 echo " <TR><td>\n </td><td>\n";
431 if ($use_javascript_addr_book) {
432 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
433 echo " <input type=button value=\\\""._("Addresses").
434 "\\\" onclick='javascript:open_abook();'>\");";
435 echo " // --></SCRIPT><NOSCRIPT>\n";
436 echo " <input type=submit name=\"html_addr_search\" value=\"".
437 _("Addresses")."\">";
438 echo " </NOSCRIPT>\n";
441 echo " <input type=submit name=\"html_addr_search\" value=\"".
442 _("Addresses")."\">";
444 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
446 if ($save_as_draft) {
447 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
449 if ($default_use_priority) {
450 if(!isset($mailprio)) {
453 echo "\n\t". _("Priority") .':<select name="mailprio">'.
454 "\n\t\t<option value=1".($mailprio=='1'?
' selected':'').'>'. _("High") .'</option>'.
455 "\n\t\t<option value=3".($mailprio=='3'?
' selected':'').'>'. _("Normal") .'</option>'.
456 "\n\t\t<option value=5".($mailprio=='5'?
' selected':'').'>'. _("Low").'</option>'.
460 do_hook('compose_button_row');
466 function checkInput ($show)
469 * I implemented the $show variable because the error messages
470 * were getting sent before the page header. So, I check once
471 * using $show=false, and then when i'm ready to display the error
474 global $body, $send_to, $subject, $color;
476 if ($send_to == "") {
478 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
483 } /* function checkInput() */
487 function saveAttachedFiles() {
488 global $HTTP_POST_FILES, $attachment_dir, $attachments, $username;
490 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
491 $localfilename = GenerateRandomString(32, '', 7);
492 $full_localfilename = "$hashed_attachment_dir/$localfilename";
493 while (file_exists($full_localfilename)) {
494 $localfilename = GenerateRandomString(32, '', 7);
495 $full_localfilename = "$hashed_attachment_dir/$localfilename";
498 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
499 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
504 $newAttachment['localfilename'] = $localfilename;
505 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
506 $newAttachment['type'] = strtolower($HTTP_POST_FILES['attachfile']['type']);
508 if ($newAttachment['type'] == "") {
509 $newAttachment['type'] = 'application/octet-stream';
512 $attachments[] = $newAttachment;
515 if (!isset($mailbox) ||
$mailbox == '' ||
($mailbox == 'None')) {
520 require_once ('../src/draft_actions.php');
521 if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id)) {
526 $draft_message = _("Draft Email Saved");
527 /* If this is a resumed draft, then delete the original */
528 if(isset($delete_draft)) {
529 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
533 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort&startMessage=1¬e=$draft_message");
540 if (isset($HTTP_POST_FILES['attachfile']) &&
541 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
542 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none') {
543 $AttachFailure = saveAttachedFiles();
545 if (checkInput(false) && !isset($AttachFailure)) {
546 $urlMailbox = urlencode (trim($mailbox));
547 if (! isset($reply_id)) {
551 * Set $default_charset to correspond with the user's selection
552 * of language interface.
557 * This is to change all newlines to \n
558 * We'll change them to \r\n later (in the sendMessage function)
560 $body = str_replace("\r\n", "\n", $body);
561 $body = str_replace("\r", "\n", $body);
564 * Rewrap $body so that no line is bigger than $editor_size
565 * This should only really kick in the sqWordWrap function
566 * if the browser doesn't support "HARD" as the wrap type
567 * Or, in Opera's case, something goes wrong.
569 $body = explode("\n", $body);
571 foreach ($body as $line) {
572 if( $line <> '-- ' ) {
573 $line = rtrim($line);
575 if (strlen($line) <= $editor_size +
1) {
576 $newBody .= $line . "\n";
579 sqWordWrap($line, $editor_size) . "\n";
585 do_hook("compose_send");
587 if (! isset($mailprio)) {
588 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
589 $subject, $body, $reply_id);
592 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
593 $subject, $body, $reply_id, $mailprio);
599 if ( isset($delete_draft)) {
600 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
604 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
607 /* $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); */
608 displayPageHeader($color, $mailbox);
610 if (isset($AttachFailure)) {
611 plain_error_message(_("Could not move/copy file. File not attached"), $color);
617 /* sqimap_logout($imapConnection); */
620 elseif (isset($html_addr_search_done)) {
621 displayPageHeader($color, $mailbox);
623 if (isset($send_to_search) && is_array($send_to_search)) {
624 foreach ($send_to_search as $k => $v) {
625 if (substr($k, 0, 1) == 'T') {
631 elseif (substr($k, 0, 1) == 'C') {
637 elseif (substr($k, 0, 1) == 'B') {
639 $send_to_bcc .= ', ';
647 elseif (isset($html_addr_search)) {
648 if (isset($HTTP_POST_FILES['attachfile']) &&
649 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
650 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none') {
651 if (saveAttachedFiles()) {
652 plain_error_message(_("Could not move/copy file. File not attached"), $color);
656 * I am using an include so as to elminiate an extra unnecessary
657 * click. If you can think of a better way, please implement it.
659 include_once('./addrbook_search_html.php');
661 elseif (isset($attach)) {
662 if (saveAttachedFiles()) {
663 plain_error_message(_("Could not move/copy file. File not attached"), $color);
665 displayPageHeader($color, $mailbox);
668 elseif (isset($do_delete)) {
669 displayPageHeader($color, $mailbox);
671 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
672 if (isset($delete) && is_array($delete)) {
673 foreach($delete as $index) {
674 $attached_file = $hashed_attachment_dir . '/'
675 . $attachments[$index]['localfilename'];
676 unlink ($attached_file);
677 unset ($attachments[$index]);
685 * This handles the default case as well as the error case
686 * (they had the same code) --> if (isset($smtpErrors))
688 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
690 displayPageHeader($color, $mailbox);
696 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num) {
700 if (isset($draft_id) && $draft_id && isset($ent_num) && $ent_num) {
706 sqimap_logout($imapConnection);
709 function ClearAttachments()
711 global $username, $attachments, $attachment_dir;
712 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
714 foreach ($attachments as $info) {
715 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
716 if (file_exists($attached_file)) {
717 unlink($attached_file);
721 $attachments = array();
724 function getReplyCitation($orig_from)
726 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
728 /* First, return an empty string when no citation style selected. */
729 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
733 /* Decode the users name. */
734 $parpos = strpos($orig_from, '(');
735 if ($parpos === false) {
736 $orig_from = trim(substr($orig_from, 0, strpos($orig_from, '<')));
737 $orig_from = str_replace('"', '', $orig_from);
738 $orig_from = str_replace("'", '', $orig_from);
741 $end_parpos = strrpos($orig_from, ')');
742 $end_parpos -= ($end_parpos === false ?
$end_parpos : $parpos +
1);
743 $orig_from = trim(substr($orig_from, $parpos +
1, $end_parpos));
746 /* Make sure our final value isn't an empty string. */
747 if ($orig_from == '') {
751 /* Otherwise, try to select the desired citation style. */
752 switch ($reply_citation_style) {
755 $end = ' ' . _("said") . ':';
758 $start = '<' . _("quote") . ' ' . _("who") . '="';
762 $start = $reply_citation_start;
763 $end = $reply_citation_end;
769 /* Build and return the citation string. */
770 return ($start . $orig_from . $end . "\n");