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);
78 for ($i=0; $i < count($body_ary); $i++
) {
81 if (preg_match('/^[\s>]+/', $body_ary[$i]))
83 $body_ary[$i] = '>' . $body_ary[$i];
87 $body_ary[$i] = '> ' . $body_ary[$i];
90 sqWordWrap($body_ary[$i], $editor_size - 1);
91 $body .= $body_ary[$i] . "\n";
96 $bodyTop = "-------- " . _("Original Message") . " --------\n";
97 $bodyTop .= _("Subject") . ": " . $orig_header->subject
. "\n";
98 $bodyTop .= _("From") . ": " . $orig_header->from
. "\n";
99 $bodyTop .= _("To") . ": " . $orig_header->to
[0] . "\n";
100 if (count($orig_header->to
) > 1) {
101 for ($x=1; $x < count($orig_header->to
); $x++
) {
102 $bodyTop .= " " . $orig_header->to
[$x] . "\n";
106 $body = $bodyTop . $body;
109 sqimap_mailbox_close($imapConnection);
114 $send_to = sqimap_find_email($send_to);
117 /** This formats a CC string if they hit "reply all" **/
118 if ($send_to_cc != "") {
119 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
120 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
121 $sendcc = explode(",", $send_to_cc);
124 for ($i = 0; $i < count($sendcc); $i++
) {
125 $sendcc[$i] = trim($sendcc[$i]);
126 if ($sendcc[$i] == "")
129 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
130 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
131 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
133 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
134 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
135 (trim($sendcc[$i]) != "")) {
136 $send_to_cc .= trim($sendcc[$i]) . ", ";
139 $send_to_cc = trim($send_to_cc);
140 if (substr($send_to_cc, -1) == ",") {
141 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
144 } // function newMail()
146 function showInputForm () {
147 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
148 $passed_body, $color, $use_signature, $signature, $editor_size,
149 $attachments, $subject, $newmail, $use_javascript_addr_book,
150 $send_to_bcc, $reply_id, $mailbox, $from_htmladdr_search;
152 $subject = sqStripSlashes(decodeHeader($subject));
153 $reply_subj = decodeHeader($reply_subj);
154 $forward_subj = decodeHeader($forward_subj);
155 $body = sqStripSlashes($body);
157 if ($use_javascript_addr_book) {
158 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
159 echo "function open_abook() { \n";
160 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
161 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
162 echo " if((!nwin.opener) && (document.windows != null))\n";
163 echo " nwin.opener = document.windows;\n";
165 echo "// --></SCRIPT>\n\n";
168 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\">\n";
169 //echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST>\n";
171 echo "<input type=hidden name=reply_id value=$reply_id>\n";
173 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
174 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
176 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
178 echo " </TD><TD BGCOLOR=\"$color[4]\">\n";
179 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
180 htmlspecialchars($send_to));
184 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
186 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
187 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
188 htmlspecialchars($send_to_cc));
192 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
194 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
195 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
196 htmlspecialchars($send_to_bcc));
200 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
202 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
204 $reply_subj = str_replace("\"", "'", $reply_subj);
205 $reply_subj = sqStripSlashes($reply_subj);
206 $reply_subj = trim($reply_subj);
207 if (substr(strtolower($reply_subj), 0, 3) != "re:")
208 $reply_subj = "Re: $reply_subj";
209 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
210 htmlspecialchars($reply_subj));
211 } else if ($forward_subj) {
212 $forward_subj = str_replace("\"", "'", $forward_subj);
213 $forward_subj = sqStripSlashes($forward_subj);
214 $forward_subj = trim($forward_subj);
215 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
216 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
217 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
218 $forward_subj = "[Fwd: $forward_subj]";
219 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
220 htmlspecialchars($forward_subj));
222 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
223 htmlspecialchars($subject));
225 echo "</td></tr>\n\n";
227 echo " <TR><td>\n </td><td>\n";
228 if ($use_javascript_addr_book) {
229 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
230 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
231 echo " // --></SCRIPT><NOSCRIPT>\n";
232 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
233 echo " </NOSCRIPT>\n";
235 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
237 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
239 do_hook("compose_button_row");
246 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
247 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
248 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
249 echo (htmlspecialchars($body)) . "\n\n-- \n" . htmlspecialchars($signature);
251 echo (htmlspecialchars($body));
253 echo "</TEXTAREA><BR>\n";
256 echo " <TR><TD COLSPAN=2 ALIGN=CENTER><INPUT TYPE=SUBMIT NAME=send VALUE=\"";
258 echo "\"></TD></TR>\n";
260 // This code is for attachments
262 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
263 echo " <SMALL><BR></SMALL>"._("Attach:");
264 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
265 // echo " <INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\"\n";
266 // echo " value=\"10000\">\n";
267 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
268 echo " <input type=\"submit\" name=\"attach\"";
269 echo " value=\"" . _("Add") ."\">\n";
272 if (isset($attachments) && count($attachments)>0) {
273 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
275 echo "</td><td align=left bgcolor=\"$color[0]\">";
276 while (list($localname, $remotename) = each($attachments)) {
277 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
278 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
281 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
284 // End of attachment code
288 do_hook("compose_bottom");
291 function showSentForm () {
292 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
293 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
297 function checkInput ($show) {
298 /** I implemented the $show variable because the error messages
299 were getting sent before the page header. So, I check once
300 using $show=false, and then when i'm ready to display the
301 error message, show=true **/
302 global $body, $send_to, $subject, $color;
304 if ($send_to == "") {
306 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
310 } // function checkInput()
313 if (($mailbox == "") ||
($mailbox == "None"))
317 if (checkInput(false)) {
318 $urlMailbox = urlencode ($mailbox);
319 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
320 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
322 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
323 displayPageHeader($color, $mailbox);
327 //sqimap_logout($imapConnection);
329 } else if ($html_addr_search_done) {
331 displayPageHeader($color, $mailbox);
333 $send_to = sqStripSlashes($send_to);
334 $send_to_cc = sqStripSlashes($send_to_cc);
335 $send_to_bcc = sqStripSlashes($send_to_bcc);
337 for ($i=0; $i < count($send_to_search); $i++
) {
340 $send_to .= $send_to_search[$i];
343 for ($i=0; $i < count($send_to_cc_search); $i++
) {
346 $send_to_cc .= $send_to_cc_search[$i];
350 } else if ($html_addr_search) {
351 // I am using an include so as to elminiate an extra unnecessary click. If you
352 // can think of a better way, please implement it.
353 include ("addrbook_search_html.php");
354 } else if (isset($attach)) {
356 displayPageHeader($color, $mailbox);
358 $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");
359 // $localfilename = $localfilename; // ??
361 // Put the file in a better place
362 // This shouldn't be here... Ondrej Sury <ondrej@sury.cz>
363 //$tmp=explode('/',$attachfile);
364 //$attachfile=$tmp[count($tmp)-1];
365 //$attachfile=ereg_replace('\.{2,}','',$attachfile);
367 //error_reporting(0); // Rename will produce error output if it fails
368 //if (!rename($attachfile, $attachment_dir.$localfilename)) {
369 // if (!copy($attachfile, $attachment_dir.$localfilename)) {
370 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
371 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
372 plain_error_message(_("Could not move/copy file. File not attached"), $color);
376 // If it still exists, PHP will remove the original file
379 // Write information about the file
380 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
381 fputs ($fp, $HTTP_POST_FILES['attachfile']['type']."\n".$HTTP_POST_FILES['attachfile']['name']."\n");
384 $attachments[$localfilename] = $HTTP_POST_FILES['attachfile']['name'];
388 } else if (isset($do_delete)) {
390 displayPageHeader($color, $mailbox);
392 while (list($lkey, $localname) = each($delete)) {
393 array_splice ($attachments, $lkey, 1);
394 unlink ($attachment_dir.$localname);
395 unlink ($attachment_dir.$localname.".info");
400 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
401 displayPageHeader($color, $mailbox);
406 sqimap_logout($imapConnection);