5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
8 ** This code sends a mail.
10 ** There are 3 modes of operation:
12 ** - Add an attachment
18 if (!isset($config_php))
19 include("../config/config.php");
20 if (!isset($strings_php))
21 include("../functions/strings.php");
22 if (!isset($page_header_php))
23 include("../functions/page_header.php");
24 if (!isset($imap_php))
25 include("../functions/imap.php");
26 if (!isset($date_php))
27 include("../functions/date.php");
28 if (!isset($mime_php))
29 include("../functions/mime.php");
30 if (!isset($smtp_php))
31 include("../functions/smtp.php");
32 if (!isset($display_messages_php))
33 include("../functions/display_messages.php");
34 if (!isset($auth_php))
35 include ("../functions/auth.php");
36 if (!isset($plugin_php))
37 include ("../functions/plugin.php");
39 include("../src/load_prefs.php");
41 // This function is used when not sending or adding attachments
43 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
44 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size;
46 $send_to = sqStripSlashes(decodeHeader($send_to));
47 $send_to_cc = sqStripSlashes(decodeHeader($send_to_cc));
48 $send_to_bcc = sqStripSlashes(decodeHeader($send_to_bcc));
57 sqimap_mailbox_select($imapConnection, $mailbox);
58 $message = sqimap_get_message($imapConnection, $id, $mailbox);
59 $orig_header = $message->header
;
61 $message = getEntity($message, $ent_num);
63 if ($message->header
->type0
== "text" ||
$message->header
->type1
== "message") {
65 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header
->encoding
);
67 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header
->encoding
);
72 if ($message->header
->type1
== "html")
73 $body = strip_tags($body);
75 $body_ary = explode("\n", $body);
77 for ($i=0; $i < count($body_ary); $i++
) {
78 sqWordWrap($body_ary[$i], $editor_size - 1);
79 $body .= $body_ary[$i];
82 $body_ary = explode("\n", $body);
84 for ($i=0; $i < count($body_ary); $i++
) {
85 if ($i==0 && $forward_id) {
86 $tmp = "-------- " . _("Original Message") . " --------\n";
87 $tmp .= _("Subject") . ": " . $orig_header->subject
. "\n";
88 $tmp .= _("From") . ": " . $orig_header->from
. "\n";
89 $tmp .= _("To") . ": " . $orig_header->to
[0] . "\n";
90 if (count($orig_header->to
) > 1) {
91 for ($x=1; $x < count($orig_header->to
); $x++
) {
92 $tmp .= " " . $orig_header->to
[$x] . "\n";
95 $tmp .= "\n" . $body_ary[$i];
100 $body = "$body$tmp\n";
102 $body = "$body> $tmp\n";
105 sqimap_mailbox_close($imapConnection);
110 $send_to = sqimap_find_email($send_to);
113 /** This formats a CC string if they hit "reply all" **/
114 if ($send_to_cc != "") {
115 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
116 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
117 $sendcc = explode(",", $send_to_cc);
120 for ($i = 0; $i < count($sendcc); $i++
) {
121 $sendcc[$i] = trim($sendcc[$i]);
122 if ($sendcc[$i] == "")
125 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
126 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
127 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
129 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
130 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
131 (trim($sendcc[$i]) != "")) {
132 $send_to_cc .= trim($sendcc[$i]) . ", ";
135 $send_to_cc = trim($send_to_cc);
136 if (substr($send_to_cc, -1) == ",") {
137 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
140 } // function newMail()
142 function showInputForm () {
143 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
144 $passed_body, $color, $use_signature, $signature, $editor_size,
145 $attachments, $subject, $newmail, $use_javascript_addr_book,
146 $send_to_bcc, $reply_id, $mailbox, $from_htmladdr_search;
148 $subject = sqStripSlashes(decodeHeader($subject));
149 $reply_subj = decodeHeader($reply_subj);
150 $forward_subj = decodeHeader($forward_subj);
151 $body = sqStripSlashes($body);
153 if ($use_javascript_addr_book) {
154 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
155 echo "function open_abook() { \n";
156 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
157 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
158 echo " if((!nwin.opener) && (document.windows != null))\n";
159 echo " nwin.opener = document.windows;\n";
161 echo "// --></SCRIPT>\n\n";
164 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\">\n";
165 //echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST>\n";
167 echo "<input type=hidden name=reply_id value=$reply_id>\n";
169 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
170 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
172 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
174 echo " </TD><TD BGCOLOR=\"$color[4]\">\n";
175 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
176 htmlspecialchars($send_to));
180 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
182 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
183 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
184 htmlspecialchars($send_to_cc));
188 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
190 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
191 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
192 htmlspecialchars($send_to_bcc));
196 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
198 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
200 $reply_subj = str_replace("\"", "'", $reply_subj);
201 $reply_subj = sqStripSlashes($reply_subj);
202 $reply_subj = trim($reply_subj);
203 if (substr(strtolower($reply_subj), 0, 3) != "re:")
204 $reply_subj = "Re: $reply_subj";
205 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
206 htmlspecialchars($reply_subj));
207 } else if ($forward_subj) {
208 $forward_subj = str_replace("\"", "'", $forward_subj);
209 $forward_subj = sqStripSlashes($forward_subj);
210 $forward_subj = trim($forward_subj);
211 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
212 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
213 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
214 $forward_subj = "[Fwd: $forward_subj]";
215 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
216 htmlspecialchars($forward_subj));
218 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
219 htmlspecialchars($subject));
221 echo "</td></tr>\n\n";
223 echo " <TR><td>\n </td><td>\n";
224 if ($use_javascript_addr_book) {
225 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
226 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
227 echo " // --></SCRIPT><NOSCRIPT>\n";
228 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
229 echo " </NOSCRIPT>\n";
231 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
233 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
235 do_hook("compose_button_row");
242 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
243 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
244 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
245 echo (htmlspecialchars($body)) . "\n\n-- \n" . htmlspecialchars($signature);
247 echo (htmlspecialchars($body));
249 echo "</TEXTAREA><BR>\n";
252 echo " <TR><TD COLSPAN=2 ALIGN=CENTER><INPUT TYPE=SUBMIT NAME=send VALUE=\"";
254 echo "\"></TD></TR>\n";
256 // This code is for attachments
258 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
259 echo " <SMALL><BR></SMALL>"._("Attach:");
260 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
261 // echo " <INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\"\n";
262 // echo " value=\"10000\">\n";
263 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
264 echo " <input type=\"submit\" name=\"attach\"";
265 echo " value=\"" . _("Add") ."\">\n";
268 if (isset($attachments) && count($attachments)>0) {
269 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
271 echo "</td><td align=left bgcolor=\"$color[0]\">";
272 while (list($localname, $remotename) = each($attachments)) {
273 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
274 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
277 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
280 // End of attachment code
284 do_hook("compose_bottom");
287 function showSentForm () {
288 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
289 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
293 function checkInput ($show) {
294 /** I implemented the $show variable because the error messages
295 were getting sent before the page header. So, I check once
296 using $show=false, and then when i'm ready to display the
297 error message, show=true **/
298 global $body, $send_to, $subject, $color;
300 if ($send_to == "") {
302 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
306 } // function checkInput()
309 if (($mailbox == "") ||
($mailbox == "None"))
313 if (checkInput(false)) {
314 $urlMailbox = urlencode ($mailbox);
315 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
316 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
318 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
319 displayPageHeader($color, $mailbox);
323 sqimap_logout($imapConnection);
325 } else if ($html_addr_search_done) {
327 displayPageHeader($color, $mailbox);
329 $send_to = sqStripSlashes($send_to);
330 $send_to_cc = sqStripSlashes($send_to_cc);
331 $send_to_bcc = sqStripSlashes($send_to_bcc);
333 for ($i=0; $i < count($send_to_search); $i++
) {
336 $send_to .= $send_to_search[$i];
339 for ($i=0; $i < count($send_to_cc_search); $i++
) {
342 $send_to_cc .= $send_to_cc_search[$i];
346 } else if ($html_addr_search) {
347 // I am using an include so as to elminiate an extra unnecessary click. If you
348 // can think of a better way, please implement it.
349 include ("addrbook_search_html.php");
350 } else if (isset($attach)) {
352 displayPageHeader($color, $mailbox);
354 $localfilename = md5($HTTP_POST_FILES['attachfile']['tmp_name'].", ".$HTTP_POST_FILES['attachfile']['name'].", $REMOTE_IP, $REMOTE_PORT, $UNIQUE_ID, and everything else that may add entropy");
355 // $localfilename = $localfilename; // ??
357 // Put the file in a better place
358 // This shouldn't be here... Ondrej Sury <ondrej@sury.cz>
359 //$tmp=explode('/',$attachfile);
360 //$attachfile=$tmp[count($tmp)-1];
361 //$attachfile=ereg_replace('\.{2,}','',$attachfile);
363 //error_reporting(0); // Rename will produce error output if it fails
364 //if (!rename($attachfile, $attachment_dir.$localfilename)) {
365 // if (!copy($attachfile, $attachment_dir.$localfilename)) {
366 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
367 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
368 plain_error_message(_("Could not move/copy file. File not attached"), $color);
372 // If it still exists, PHP will remove the original file
375 // Write information about the file
376 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
377 fputs ($fp, $HTTP_POST_FILES['attachfile']['type']."\n".$HTTP_POST_FILES['attachfile']['name']."\n");
380 $attachments[$localfilename] = $HTTP_POST_FILES['attachfile']['name'];
384 } else if (isset($do_delete)) {
386 displayPageHeader($color, $mailbox);
388 while (list($lkey, $localname) = each($delete)) {
389 array_splice ($attachments, $lkey, 1);
390 unlink ($attachment_dir.$localname);
391 unlink ($attachment_dir.$localname.".info");
396 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
397 displayPageHeader($color, $mailbox);
402 sqimap_logout($imapConnection);