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 $i = count($body_ary) - 1;
78 while (isset($body_ary[$i]) && ereg("^[>\s]*$", $body_ary[$i])) {
83 for ($i=0; $i < count($body_ary); $i++
) {
86 if (ereg('^[\s>]+', $body_ary[$i]))
88 $body_ary[$i] = '>' . $body_ary[$i];
92 $body_ary[$i] = '> ' . $body_ary[$i];
95 sqWordWrap($body_ary[$i], $editor_size - 1);
96 $body .= $body_ary[$i] . "\n";
101 $bodyTop = "-------- " . _("Original Message") . " --------\n";
102 $bodyTop .= _("Subject") . ": " . $orig_header->subject
. "\n";
103 $bodyTop .= _("From") . ": " . $orig_header->from
. "\n";
104 $bodyTop .= _("To") . ": " . $orig_header->to
[0] . "\n";
105 if (count($orig_header->to
) > 1) {
106 for ($x=1; $x < count($orig_header->to
); $x++
) {
107 $bodyTop .= " " . $orig_header->to
[$x] . "\n";
111 $body = $bodyTop . $body;
114 $body = ereg_replace('\\\\', '\\\\', $body);
116 sqimap_mailbox_close($imapConnection);
121 $send_to = sqimap_find_email($send_to);
124 /** This formats a CC string if they hit "reply all" **/
125 if ($send_to_cc != "") {
126 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
127 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
128 $sendcc = explode(",", $send_to_cc);
131 for ($i = 0; $i < count($sendcc); $i++
) {
132 $sendcc[$i] = trim($sendcc[$i]);
133 if ($sendcc[$i] == "")
136 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
137 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
138 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
140 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
141 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
142 (trim($sendcc[$i]) != "")) {
143 $send_to_cc .= trim($sendcc[$i]) . ", ";
146 $send_to_cc = trim($send_to_cc);
147 if (substr($send_to_cc, -1) == ",") {
148 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
151 } // function newMail()
153 function showInputForm () {
154 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
155 $passed_body, $color, $use_signature, $signature, $editor_size,
156 $attachments, $subject, $newmail, $use_javascript_addr_book,
157 $send_to_bcc, $reply_id, $mailbox, $from_htmladdr_search,
158 $location_of_buttons;
160 $subject = sqStripSlashes(decodeHeader($subject));
161 $reply_subj = decodeHeader($reply_subj);
162 $forward_subj = decodeHeader($forward_subj);
163 $body = sqStripSlashes($body);
165 if ($use_javascript_addr_book) {
166 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
167 echo "function open_abook() { \n";
168 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
169 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
170 echo " if((!nwin.opener) && (document.windows != null))\n";
171 echo " nwin.opener = document.windows;\n";
173 echo "// --></SCRIPT>\n\n";
176 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\">\n";
177 //echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST>\n";
179 echo "<input type=hidden name=reply_id value=$reply_id>\n";
181 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
182 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
184 if ($location_of_buttons == 'top') showComposeButtonRow();
187 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
189 echo " </TD><TD BGCOLOR=\"$color[4]\">\n";
190 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
191 htmlspecialchars($send_to));
195 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
197 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
198 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
199 htmlspecialchars($send_to_cc));
203 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
205 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
206 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
207 htmlspecialchars($send_to_bcc));
211 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
213 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
215 $reply_subj = str_replace("\"", "'", $reply_subj);
216 $reply_subj = sqStripSlashes($reply_subj);
217 $reply_subj = trim($reply_subj);
218 if (substr(strtolower($reply_subj), 0, 3) != "re:")
219 $reply_subj = "Re: $reply_subj";
220 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
221 htmlspecialchars($reply_subj));
222 } else if ($forward_subj) {
223 $forward_subj = str_replace("\"", "'", $forward_subj);
224 $forward_subj = sqStripSlashes($forward_subj);
225 $forward_subj = trim($forward_subj);
226 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
227 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
228 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
229 $forward_subj = "[Fwd: $forward_subj]";
230 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
231 htmlspecialchars($forward_subj));
233 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
234 htmlspecialchars($subject));
236 echo "</td></tr>\n\n";
238 if ($location_of_buttons == 'between') showComposeButtonRow();
241 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
242 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
243 echo htmlspecialchars($body);
244 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
245 echo "\n\n-- \n" . htmlspecialchars($signature);
247 echo "</TEXTAREA><BR>\n";
251 if ($location_of_buttons == 'bottom')
252 showComposeButtonRow();
254 echo " <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
257 // This code is for attachments
259 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
260 echo " <SMALL><BR></SMALL>"._("Attach:");
261 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
262 // echo " <INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\"\n";
263 // echo " value=\"10000\">\n";
264 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
265 echo " <input type=\"submit\" name=\"attach\"";
266 echo " value=\"" . _("Add") ."\">\n";
269 if (isset($attachments) && count($attachments)>0) {
270 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
272 echo "</td><td align=left bgcolor=\"$color[0]\">";
273 while (list($localname, $remotename) = each($attachments)) {
274 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
275 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
278 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
281 // End of attachment code
285 do_hook("compose_bottom");
288 function showComposeButtonRow() {
289 global $use_javascript_addr_book;
290 echo " <TR><td>\n </td><td>\n";
291 if ($use_javascript_addr_book) {
292 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
293 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
294 echo " // --></SCRIPT><NOSCRIPT>\n";
295 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
296 echo " </NOSCRIPT>\n";
298 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
300 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
302 do_hook("compose_button_row");
308 function showSentForm () {
309 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
310 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
314 function checkInput ($show) {
315 /** I implemented the $show variable because the error messages
316 were getting sent before the page header. So, I check once
317 using $show=false, and then when i'm ready to display the
318 error message, show=true **/
319 global $body, $send_to, $subject, $color;
321 if ($send_to == "") {
323 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
327 } // function checkInput()
331 function saveAttachedFiles() {
332 global $HTTP_POST_FILES, $attachment_dir, $attachments;
335 $localfilename = GenerateRandomString(32, '', 7);
337 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
338 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
344 // Write information about the file
345 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
346 fputs ($fp, $HTTP_POST_FILES['attachfile']['type']."\n".$HTTP_POST_FILES['attachfile']['name']."\n");
349 $attachments[$localfilename] = $HTTP_POST_FILES['attachfile']['name'];
353 if (($mailbox == "") ||
($mailbox == "None"))
357 if ($HTTP_POST_FILES['attachfile']['tmp_name'])
358 $AttachFailure = saveAttachedFiles();
359 if (checkInput(false) && ! $AttachFailure) {
360 $urlMailbox = urlencode ($mailbox);
361 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
362 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
364 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
365 displayPageHeader($color, $mailbox);
368 plain_error_message(_("Could not move/copy file. File not attached"), $color);
373 //sqimap_logout($imapConnection);
375 } else if ($html_addr_search_done) {
377 displayPageHeader($color, $mailbox);
379 $send_to = sqStripSlashes($send_to);
380 $send_to_cc = sqStripSlashes($send_to_cc);
381 $send_to_bcc = sqStripSlashes($send_to_bcc);
383 for ($i=0; $i < count($send_to_search); $i++
) {
386 $send_to .= $send_to_search[$i];
389 for ($i=0; $i < count($send_to_cc_search); $i++
) {
392 $send_to_cc .= $send_to_cc_search[$i];
396 } else if ($html_addr_search) {
397 // I am using an include so as to elminiate an extra unnecessary click. If you
398 // can think of a better way, please implement it.
399 include ("./addrbook_search_html.php");
400 } else if (isset($attach)) {
401 if (saveAttachedFiles())
402 plain_error_message(_("Could not move/copy file. File not attached"), $color);
404 } else if (isset($do_delete)) {
406 displayPageHeader($color, $mailbox);
408 while (list($lkey, $localname) = each($delete)) {
409 unset ($attachments[$localname]);
410 unlink ($attachment_dir.$localname);
411 unlink ($attachment_dir.$localname.".info");
416 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
417 displayPageHeader($color, $mailbox);
422 sqimap_logout($imapConnection);