bc3ab0609dec480ad3a2297c50df8230a008d24e
6 * Copyright (c) 1999-2001 The Squirrelmail Development 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);
66 $use_signature = FALSE;
70 sqimap_mailbox_select($imapConnection, $mailbox);
71 $message = sqimap_get_message($imapConnection, $id, $mailbox);
72 $orig_header = $message->header
;
74 $message = getEntity($message, $ent_num);
76 if ($message->header
->type0
== 'text' ||
$message->header
->type1
== 'message') {
78 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header
->encoding
);
80 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header
->encoding
);
85 if ($message->header
->type1
== "html")
86 $body = strip_tags($body);
89 $body_ary = explode("\n", $body);
90 $i = count($body_ary) - 1;
91 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
96 for ($i=0; isset($body_ary[$i]); $i++
) {
99 if (ereg('^[ >]+', $body_ary[$i]))
101 $body_ary[$i] = '>' . $body_ary[$i];
105 $body_ary[$i] = '> ' . $body_ary[$i];
109 sqWordWrap($body_ary[$i], $editor_size - 1);
111 $body .= $body_ary[$i] . "\n";
112 unset($body_ary[$i]);
116 $bodyTop = "-------- " . _("Original Message") . " --------\n";
117 $bodyTop .= _("Subject") . ": " . $orig_header->subject
. "\n";
118 $bodyTop .= _("From") . ": " . $orig_header->from
. "\n";
119 $bodyTop .= _("To") . ": " . $orig_header->to
[0] . "\n";
120 if (count($orig_header->to
) > 1) {
121 for ($x=1; $x < count($orig_header->to
); $x++
) {
122 $bodyTop .= " " . $orig_header->to
[$x] . "\n";
126 $body = $bodyTop . $body;
127 } else if ($reply_id) {
128 $orig_from = decodeHeader($orig_header->from
);
129 $orig_from = trim(substr($orig_from,0,strpos($orig_from,'<')));
130 $orig_from = str_replace('"','',$orig_from);
131 $orig_from = str_replace("'",'',$orig_from);
132 $body = getReplyCitation($orig_from) . $body;
139 $send_to = sqimap_find_email($send_to);
142 /** This formats a CC string if they hit "reply all" **/
143 if ($send_to_cc != "") {
144 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
145 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
146 $sendcc = explode(",", $send_to_cc);
149 for ($i = 0; $i < count($sendcc); $i++
) {
150 $sendcc[$i] = trim($sendcc[$i]);
151 if ($sendcc[$i] == "")
154 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
155 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
156 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
158 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
159 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
160 (trim($sendcc[$i]) != "")) {
161 $send_to_cc .= trim($sendcc[$i]) . ", ";
164 $send_to_cc = trim($send_to_cc);
165 if (substr($send_to_cc, -1) == ",") {
166 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
169 } // function newMail()
171 function getAttachments($message) {
172 global $mailbox, $attachments, $attachment_dir, $imapConnection,
173 $ent_num, $forward_id, $draft_id, $username;
175 if (isset($draft_id))
181 sqimap_mailbox_select($imapConnection, $mailbox);
182 $message = sqimap_get_message($imapConnection, $id,
186 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
187 if (count($message->entities
) == 0) {
188 if ($message->header
->entity_id
!= $ent_num) {
189 $filename = decodeHeader($message->header
->filename
);
192 $filename = "untitled-".$message->header
->entity_id
;
194 $localfilename = GenerateRandomString(32, '', 7);
195 $full_localfilename = "$hashed_attachment_dir/$localfilename";
196 while (file_exists($full_localfilename)) {
197 $localfilename = GenerateRandomString(32, '', 7);
198 $full_localfilename = "$hashed_attachment_dir/$localfilename";
201 $newAttachment = array();
202 $newAttachment['localfilename'] = $localfilename;
203 $newAttachment['remotefilename'] = $filename;
204 $newAttachment['type'] = strtolower($message->header
->type0
.
205 '/' . $message->header
->type1
);
207 // Write Attachment to file
208 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'w');
209 fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
210 $id, $message->header
->entity_id
),
211 $message->header
->encoding
));
214 $attachments[] = $newAttachment;
217 for ($i = 0; $i < count($message->entities
); $i++
) {
218 getAttachments($message->entities
[$i]);
224 function showInputForm () {
225 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
226 $passed_body, $color, $use_signature, $signature, $prefix_sig,
227 $editor_size, $attachments, $subject, $newmail,
228 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
229 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
230 $username, $data_dir, $identity, $draft_id, $delete_draft,
233 $subject = decodeHeader($subject);
234 $reply_subj = decodeHeader($reply_subj);
235 $forward_subj = decodeHeader($forward_subj);
237 if ($use_javascript_addr_book) {
238 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
239 echo "function open_abook() { \n";
240 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
241 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
242 echo " if((!nwin.opener) && (document.windows != null))\n";
243 echo " nwin.opener = document.windows;\n";
245 echo "// --></SCRIPT>\n\n";
248 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
249 do_hook("compose_form");
252 if (isset($draft_id)) {
253 echo "<input type=\"hidden\" name=\"delete_draft\" value=\"$draft_id\">\n";
255 if (isset($delete_draft)) {
256 echo "<input type=\"hidden\" name=\"delete_draft\" value=\"$delete_draft\">\n";
259 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
261 if ($location_of_buttons == 'top') showComposeButtonRow();
263 $idents = getPref($data_dir, $username, 'identities');
264 if ($idents != '' && $idents > 1) {
266 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
268 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
269 echo "<select name=identity>\n";
270 echo "<option value=default>" .
271 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
272 $em = getPref($data_dir, $username, 'email_address');
274 echo htmlspecialchars(' <' . $em . '>') . "\n";
275 for ($i = 1; $i < $idents; $i ++
) {
276 echo '<option value="' . $i . '"';
277 if (isset($identity) && $identity == $i)
280 echo htmlspecialchars(getPref($data_dir, $username, 'full_name' .
282 $em = getPref($data_dir, $username, 'email_address' . $i);
284 echo htmlspecialchars(' <' . $em . '>') . "\n";
291 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
293 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
294 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
295 htmlspecialchars($send_to));
299 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
301 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
302 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
303 htmlspecialchars($send_to_cc));
307 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
309 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
310 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
311 htmlspecialchars($send_to_bcc));
315 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
317 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
319 $reply_subj = str_replace("\"", "'", $reply_subj);
320 $reply_subj = trim($reply_subj);
321 if (substr(strtolower($reply_subj), 0, 3) != "re:")
322 $reply_subj = "Re: $reply_subj";
323 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
324 htmlspecialchars($reply_subj));
325 } else if ($forward_subj) {
326 $forward_subj = trim($forward_subj);
327 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
328 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
329 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:'))
330 $forward_subj = "[Fwd: $forward_subj]";
331 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
332 htmlspecialchars($forward_subj));
334 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
335 htmlspecialchars($subject));
337 echo "</td></tr>\n\n";
339 if ($location_of_buttons == 'between') showComposeButtonRow();
342 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
343 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
344 echo htmlspecialchars($body);
345 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
346 if ( $prefix_sig == true )
347 echo "\n\n-- \n" . htmlspecialchars($signature);
349 echo "\n\n" . htmlspecialchars($signature);
351 echo "</TEXTAREA><BR>\n";
355 if ($location_of_buttons == 'bottom')
356 showComposeButtonRow();
358 echo " <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
361 // This code is for attachments
363 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=MIDDLE ALIGN=RIGHT>\n";
365 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
366 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
367 echo " <input type=\"submit\" name=\"attach\"";
368 echo " value=\"" . _("Add") ."\">\n";
371 if (count($attachments)) {
372 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
373 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
375 echo "</td><td align=left bgcolor=\"$color[0]\">";
376 foreach ($attachments as $key => $info) {
377 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
378 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$key\">\n";
379 echo $info['remotefilename'] . " - " . $info['type'] . " (";
380 echo show_readable_size(filesize($attached_file)) . ")<br>\n";
383 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
386 // End of attachment code
390 echo "<input type=hidden name=reply_id value=$reply_id>\n";
392 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
394 do_hook("compose_bottom");
395 echo "</BODY></HTML>\n";
398 function showComposeButtonRow() {
399 global $use_javascript_addr_book, $save_as_draft,
400 $default_use_priority, $mailprio;
402 echo " <TR><td>\n </td><td>\n";
403 if ($use_javascript_addr_book) {
404 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
405 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
406 echo " // --></SCRIPT><NOSCRIPT>\n";
407 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
408 echo " </NOSCRIPT>\n";
410 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
412 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
414 if ($save_as_draft) {
415 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
417 if ($default_use_priority) {
418 if(!isset($mailprio)) {
421 echo "\n\t". _("Priority") .":<select name=\"mailprio\">".
422 "\n\t\t<option value=1".($mailprio=="1"?
" selected":"").">". _("High") ."</option>".
423 "\n\t\t<option value=3".($mailprio=="3"?
" selected":"").">". _("Normal") ."</option>".
424 "\n\t\t<option value=5".($mailprio=="5"?
" selected":"").">". _("Low")."</option>".
428 do_hook("compose_button_row");
434 function checkInput ($show) {
435 /** I implemented the $show variable because the error messages
436 were getting sent before the page header. So, I check once
437 using $show=false, and then when i'm ready to display the
438 error message, show=true **/
439 global $body, $send_to, $subject, $color;
441 if ($send_to == "") {
443 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
447 } // function checkInput()
451 function saveAttachedFiles() {
452 global $HTTP_POST_FILES, $attachment_dir, $attachments, $username;
454 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
455 $localfilename = GenerateRandomString(32, '', 7);
456 $full_localfilename = "$hashed_attachment_dir/$localfilename";
457 while (file_exists($full_localfilename)) {
458 $localfilename = GenerateRandomString(32, '', 7);
459 $full_localfilename = "$hashed_attachment_dir/$localfilename";
462 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
463 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
468 $newAttachment['localfilename'] = $localfilename;
469 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
470 $newAttachment['type'] =
471 strtolower($HTTP_POST_FILES['attachfile']['type']);
473 if ($newAttachment['type'] == "")
474 $newAttachment['type'] = 'application/octet-stream';
476 $attachments[] = $newAttachment;
479 if (!isset($mailbox) ||
$mailbox == '' ||
($mailbox == 'None'))
483 require_once ('../src/draft_actions.php');
484 if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id)) {
488 $draft_message = _("Draft Email Saved");
489 /* If this is a resumed draft, then delete the original */
490 if(isset($delete_draft)) {
491 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
494 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort&startMessage=1¬e=$draft_message");
501 if (isset($HTTP_POST_FILES['attachfile']) &&
502 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
503 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
504 $AttachFailure = saveAttachedFiles();
505 if (checkInput(false) && !isset($AttachFailure)) {
506 $urlMailbox = urlencode (trim($mailbox));
507 if (! isset($reply_id))
509 // Set $default_charset to correspond with the user's selection
510 // of language interface.
513 // This is to change all newlines to \n
514 // We'll change them to \r\n later (in the sendMessage function)
515 $body = str_replace("\r\n", "\n", $body);
516 $body = str_replace("\r", "\n", $body);
518 // Rewrap $body so that no line is bigger than $editor_size
519 // This should only really kick in the sqWordWrap function
520 // if the browser doesn't support "HARD" as the wrap type
521 // Or, in Opera's case, something goes wrong.
522 $body = explode("\n", $body);
524 foreach ($body as $line) {
526 $line = rtrim($line);
527 if (strlen($line) <= $editor_size +
1)
528 $newBody .= $line . "\n";
530 sqWordWrap($line, $editor_size) . "\n";
536 do_hook("compose_send");
538 if (! isset($mailprio))
539 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
540 $subject, $body, $reply_id);
542 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
543 $subject, $body, $reply_id, $mailprio);
548 if ( isset($delete_draft)) {
549 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
553 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
555 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
556 displayPageHeader($color, $mailbox);
558 if (isset($AttachFailure))
559 plain_error_message(_("Could not move/copy file. File not attached"), $color);
564 //sqimap_logout($imapConnection);
566 } else if (isset($html_addr_search_done)) {
567 displayPageHeader($color, $mailbox);
569 if (isset($send_to_search) && is_array($send_to_search)) {
570 foreach ($send_to_search as $k => $v) {
571 if (substr($k, 0, 1) == 'T') {
576 elseif (substr($k, 0, 1) == 'C') {
581 elseif (substr($k, 0, 1) == 'B') {
583 $send_to_bcc .= ', ';
590 } else if (isset($html_addr_search)) {
591 if (isset($HTTP_POST_FILES['attachfile']) &&
592 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
593 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
595 if (saveAttachedFiles())
596 plain_error_message(_("Could not move/copy file. File not attached"), $color);
598 // I am using an include so as to elminiate an extra unnecessary click. If you
599 // can think of a better way, please implement it.
600 include_once('./addrbook_search_html.php');
601 } else if (isset($attach)) {
602 if (saveAttachedFiles())
603 plain_error_message(_("Could not move/copy file. File not attached"), $color);
604 displayPageHeader($color, $mailbox);
606 } else if (isset($do_delete)) {
607 displayPageHeader($color, $mailbox);
609 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
610 if (isset($delete) && is_array($delete)) {
611 foreach($delete as $index) {
612 $attached_file = $hashed_attachment_dir . '/'
613 . $attachments[$index]['localfilename'];
614 unlink ($attached_file);
615 unset ($attachments[$index]);
621 // This handles the default case as well as the error case
622 // (they had the same code) --> if (isset($smtpErrors))
623 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
625 displayPageHeader($color, $mailbox);
631 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
634 if (isset($draft_id) && $draft_id && isset($ent_num) && $ent_num)
639 sqimap_logout($imapConnection);
642 function ClearAttachments() {
643 global $username, $attachments, $attachment_dir;
644 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
646 foreach ($attachments as $info) {
647 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
648 if (file_exists($attached_file)) {
649 unlink($attached_file);
653 $attachments = array();
656 function getReplyCitation($orig_from) {
657 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
659 /* First, return an empty string when no citation style selected. */
660 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
664 /* Otherwise, try to select the desired citation style. */
665 switch ($reply_citation_style) {
668 $end = ' ' . _("said") . ':';
671 $start = '<' . _("quote") . ' ' . _("who") . '="';
675 $start = $reply_citation_start;
676 $end = $reply_citation_end;
678 default: return ('');
681 /* Build and return the citation string. */
682 return ($start . $orig_from . $end . "\n");