39d3d33bb7a97cf479966ee0ab470204666bda94
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);
76 $body_ary = explode("\n", $body);
77 while (ereg("^[>\s]*$", $body_ary[count($body_ary) - 1])) {
78 unset($body_ary[count($body_ary) - 1]);
81 for ($i=0; $i < count($body_ary); $i++
) {
84 if (ereg('^[\s>]+', $body_ary[$i]))
86 $body_ary[$i] = '>' . $body_ary[$i];
90 $body_ary[$i] = '> ' . $body_ary[$i];
93 sqWordWrap($body_ary[$i], $editor_size - 1);
94 $body .= $body_ary[$i] . "\n";
99 $bodyTop = "-------- " . _("Original Message") . " --------\n";
100 $bodyTop .= _("Subject") . ": " . $orig_header->subject
. "\n";
101 $bodyTop .= _("From") . ": " . $orig_header->from
. "\n";
102 $bodyTop .= _("To") . ": " . $orig_header->to
[0] . "\n";
103 if (count($orig_header->to
) > 1) {
104 for ($x=1; $x < count($orig_header->to
); $x++
) {
105 $bodyTop .= " " . $orig_header->to
[$x] . "\n";
109 $body = $bodyTop . $body;
112 $body = ereg_replace('\\\\', '\\\\', $body);
114 sqimap_mailbox_close($imapConnection);
119 $send_to = sqimap_find_email($send_to);
122 /** This formats a CC string if they hit "reply all" **/
123 if ($send_to_cc != "") {
124 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
125 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
126 $sendcc = explode(",", $send_to_cc);
129 for ($i = 0; $i < count($sendcc); $i++
) {
130 $sendcc[$i] = trim($sendcc[$i]);
131 if ($sendcc[$i] == "")
134 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
135 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
136 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
138 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
139 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
140 (trim($sendcc[$i]) != "")) {
141 $send_to_cc .= trim($sendcc[$i]) . ", ";
144 $send_to_cc = trim($send_to_cc);
145 if (substr($send_to_cc, -1) == ",") {
146 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
149 } // function newMail()
151 function showInputForm () {
152 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
153 $passed_body, $color, $use_signature, $signature, $editor_size,
154 $attachments, $subject, $newmail, $use_javascript_addr_book,
155 $send_to_bcc, $reply_id, $mailbox, $from_htmladdr_search,
156 $location_of_buttons;
158 $subject = sqStripSlashes(decodeHeader($subject));
159 $reply_subj = decodeHeader($reply_subj);
160 $forward_subj = decodeHeader($forward_subj);
161 $body = sqStripSlashes($body);
163 if ($use_javascript_addr_book) {
164 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
165 echo "function open_abook() { \n";
166 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
167 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
168 echo " if((!nwin.opener) && (document.windows != null))\n";
169 echo " nwin.opener = document.windows;\n";
171 echo "// --></SCRIPT>\n\n";
174 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\">\n";
175 //echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST>\n";
177 echo "<input type=hidden name=reply_id value=$reply_id>\n";
179 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
180 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
182 if ($location_of_buttons == 'top') showComposeButtonRow();
185 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
187 echo " </TD><TD BGCOLOR=\"$color[4]\">\n";
188 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
189 htmlspecialchars($send_to));
193 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
195 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
196 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
197 htmlspecialchars($send_to_cc));
201 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
203 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
204 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
205 htmlspecialchars($send_to_bcc));
209 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
211 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
213 $reply_subj = str_replace("\"", "'", $reply_subj);
214 $reply_subj = sqStripSlashes($reply_subj);
215 $reply_subj = trim($reply_subj);
216 if (substr(strtolower($reply_subj), 0, 3) != "re:")
217 $reply_subj = "Re: $reply_subj";
218 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
219 htmlspecialchars($reply_subj));
220 } else if ($forward_subj) {
221 $forward_subj = str_replace("\"", "'", $forward_subj);
222 $forward_subj = sqStripSlashes($forward_subj);
223 $forward_subj = trim($forward_subj);
224 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
225 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
226 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
227 $forward_subj = "[Fwd: $forward_subj]";
228 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
229 htmlspecialchars($forward_subj));
231 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
232 htmlspecialchars($subject));
234 echo "</td></tr>\n\n";
236 if ($location_of_buttons == 'between') showComposeButtonRow();
239 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
240 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
241 echo htmlspecialchars($body);
242 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
243 echo "\n\n-- \n" . htmlspecialchars($signature);
245 echo "</TEXTAREA><BR>\n";
249 if ($location_of_buttons == 'bottom')
250 showComposeButtonRow();
252 echo " <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
255 // This code is for attachments
257 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
258 echo " <SMALL><BR></SMALL>"._("Attach:");
259 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
260 // echo " <INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\"\n";
261 // echo " value=\"10000\">\n";
262 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
263 echo " <input type=\"submit\" name=\"attach\"";
264 echo " value=\"" . _("Add") ."\">\n";
267 if (isset($attachments) && count($attachments)>0) {
268 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
270 echo "</td><td align=left bgcolor=\"$color[0]\">";
271 while (list($localname, $remotename) = each($attachments)) {
272 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
273 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
276 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
279 // End of attachment code
283 do_hook("compose_bottom");
286 function showComposeButtonRow() {
287 echo " <TR><td>\n </td><td>\n";
288 if ($use_javascript_addr_book) {
289 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
290 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
291 echo " // --></SCRIPT><NOSCRIPT>\n";
292 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
293 echo " </NOSCRIPT>\n";
295 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
297 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
299 do_hook("compose_button_row");
305 function showSentForm () {
306 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
307 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
311 function checkInput ($show) {
312 /** I implemented the $show variable because the error messages
313 were getting sent before the page header. So, I check once
314 using $show=false, and then when i'm ready to display the
315 error message, show=true **/
316 global $body, $send_to, $subject, $color;
318 if ($send_to == "") {
320 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
324 } // function checkInput()
327 if (($mailbox == "") ||
($mailbox == "None"))
331 if (checkInput(false)) {
332 $urlMailbox = urlencode ($mailbox);
333 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
334 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
336 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
337 displayPageHeader($color, $mailbox);
341 //sqimap_logout($imapConnection);
343 } else if ($html_addr_search_done) {
345 displayPageHeader($color, $mailbox);
347 $send_to = sqStripSlashes($send_to);
348 $send_to_cc = sqStripSlashes($send_to_cc);
349 $send_to_bcc = sqStripSlashes($send_to_bcc);
351 for ($i=0; $i < count($send_to_search); $i++
) {
354 $send_to .= $send_to_search[$i];
357 for ($i=0; $i < count($send_to_cc_search); $i++
) {
360 $send_to_cc .= $send_to_cc_search[$i];
364 } else if ($html_addr_search) {
365 // I am using an include so as to elminiate an extra unnecessary click. If you
366 // can think of a better way, please implement it.
367 include ("addrbook_search_html.php");
368 } else if (isset($attach)) {
370 displayPageHeader($color, $mailbox);
372 $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");
373 // $localfilename = $localfilename; // ??
375 // Put the file in a better place
376 // This shouldn't be here... Ondrej Sury <ondrej@sury.cz>
377 //$tmp=explode('/',$attachfile);
378 //$attachfile=$tmp[count($tmp)-1];
379 //$attachfile=ereg_replace('\.{2,}','',$attachfile);
381 //error_reporting(0); // Rename will produce error output if it fails
382 //if (!rename($attachfile, $attachment_dir.$localfilename)) {
383 // if (!copy($attachfile, $attachment_dir.$localfilename)) {
384 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
385 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
386 plain_error_message(_("Could not move/copy file. File not attached"), $color);
390 // If it still exists, PHP will remove the original file
393 // Write information about the file
394 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
395 fputs ($fp, $HTTP_POST_FILES['attachfile']['type']."\n".$HTTP_POST_FILES['attachfile']['name']."\n");
398 $attachments[$localfilename] = $HTTP_POST_FILES['attachfile']['name'];
402 } else if (isset($do_delete)) {
404 displayPageHeader($color, $mailbox);
406 while (list($lkey, $localname) = each($delete)) {
407 array_splice ($attachments, $lkey, 1);
408 unlink ($attachment_dir.$localname);
409 unlink ($attachment_dir.$localname.".info");
414 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
415 displayPageHeader($color, $mailbox);
420 sqimap_logout($imapConnection);