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);
120 $send_to = sqimap_find_email($send_to);
123 /** This formats a CC string if they hit "reply all" **/
124 if ($send_to_cc != "") {
125 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
126 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
127 $sendcc = explode(",", $send_to_cc);
130 for ($i = 0; $i < count($sendcc); $i++
) {
131 $sendcc[$i] = trim($sendcc[$i]);
132 if ($sendcc[$i] == "")
135 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
136 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
137 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
139 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
140 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
141 (trim($sendcc[$i]) != "")) {
142 $send_to_cc .= trim($sendcc[$i]) . ", ";
145 $send_to_cc = trim($send_to_cc);
146 if (substr($send_to_cc, -1) == ",") {
147 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
150 } // function newMail()
152 function getAttachments($message) {
153 global $mailbox, $attachments, $attachment_dir, $imapConnection,
154 $ent_num, $forward_id;
157 sqimap_mailbox_select($imapConnection, $mailbox);
158 $message = sqimap_get_message($imapConnection, $forward_id, $mailbox); }
160 if (!$message->entities
) {
161 if ($message->header
->entity_id
!= $ent_num) {
162 $filename = decodeHeader($message->header
->filename
);
165 $filename = "untitled-".$message->header
->entity_id
;
167 $localfilename = md5($filename.", $REMOTE_IP, REMOTE_PORT, $UNIQUE_ID, extra-stuff here");
170 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
171 fputs ($fp, strtolower($message->header
->type0
)."/".strtolower($message->header
->type1
)."\n".$filename."\n");
174 // Write Attachment to file
175 $fp = fopen ($attachment_dir.$localfilename, "w");
176 fputs ($fp, decodeBody(mime_fetch_body($imapConnection, $forward_id, $message->header
->entity_id
), $message->header
->encoding
));
178 // Don't know why these lines were included
179 // fgets($imapConnection, 256);
180 // fgets($imapConnection, 256);
184 $attachments[$localfilename] = $filename;
188 for ($i = 0; $i < count($message->entities
); $i++
) {
189 getAttachments($message->entities
[$i]);
195 function showInputForm () {
196 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
197 $passed_body, $color, $use_signature, $signature, $editor_size,
198 $attachments, $subject, $newmail, $use_javascript_addr_book,
199 $send_to_bcc, $reply_id, $mailbox, $from_htmladdr_search,
200 $location_of_buttons;
202 $subject = sqStripSlashes(decodeHeader($subject));
203 $reply_subj = decodeHeader($reply_subj);
204 $forward_subj = decodeHeader($forward_subj);
205 $body = sqStripSlashes($body);
207 if ($use_javascript_addr_book) {
208 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
209 echo "function open_abook() { \n";
210 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
211 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
212 echo " if((!nwin.opener) && (document.windows != null))\n";
213 echo " nwin.opener = document.windows;\n";
215 echo "// --></SCRIPT>\n\n";
218 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\">\n";
219 //echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST>\n";
221 echo "<input type=hidden name=reply_id value=$reply_id>\n";
223 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
224 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
226 if ($location_of_buttons == 'top') showComposeButtonRow();
229 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
231 echo " </TD><TD BGCOLOR=\"$color[4]\">\n";
232 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
233 htmlspecialchars($send_to));
237 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
239 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
240 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
241 htmlspecialchars($send_to_cc));
245 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
247 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
248 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
249 htmlspecialchars($send_to_bcc));
253 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
255 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
257 $reply_subj = str_replace("\"", "'", $reply_subj);
258 $reply_subj = sqStripSlashes($reply_subj);
259 $reply_subj = trim($reply_subj);
260 if (substr(strtolower($reply_subj), 0, 3) != "re:")
261 $reply_subj = "Re: $reply_subj";
262 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
263 htmlspecialchars($reply_subj));
264 } else if ($forward_subj) {
265 $forward_subj = str_replace("\"", "'", $forward_subj);
266 $forward_subj = sqStripSlashes($forward_subj);
267 $forward_subj = trim($forward_subj);
268 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
269 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
270 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
271 $forward_subj = "[Fwd: $forward_subj]";
272 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
273 htmlspecialchars($forward_subj));
275 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
276 htmlspecialchars($subject));
278 echo "</td></tr>\n\n";
280 if ($location_of_buttons == 'between') showComposeButtonRow();
283 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
284 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
285 echo htmlspecialchars($body);
286 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
287 echo "\n\n-- \n" . htmlspecialchars($signature);
289 echo "</TEXTAREA><BR>\n";
293 if ($location_of_buttons == 'bottom')
294 showComposeButtonRow();
296 echo " <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
299 // This code is for attachments
301 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
302 echo " <SMALL><BR></SMALL>"._("Attach:");
303 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
304 // echo " <INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\"\n";
305 // echo " value=\"10000\">\n";
306 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
307 echo " <input type=\"submit\" name=\"attach\"";
308 echo " value=\"" . _("Add") ."\">\n";
311 if (isset($attachments) && count($attachments)>0) {
312 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
314 echo "</td><td align=left bgcolor=\"$color[0]\">";
315 while (list($localname, $remotename) = each($attachments)) {
316 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
317 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
320 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
323 // End of attachment code
327 do_hook("compose_bottom");
330 function showComposeButtonRow() {
331 global $use_javascript_addr_book;
332 echo " <TR><td>\n </td><td>\n";
333 if ($use_javascript_addr_book) {
334 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
335 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
336 echo " // --></SCRIPT><NOSCRIPT>\n";
337 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
338 echo " </NOSCRIPT>\n";
340 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
342 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
344 do_hook("compose_button_row");
350 function showSentForm () {
351 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
352 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
356 function checkInput ($show) {
357 /** I implemented the $show variable because the error messages
358 were getting sent before the page header. So, I check once
359 using $show=false, and then when i'm ready to display the
360 error message, show=true **/
361 global $body, $send_to, $subject, $color;
363 if ($send_to == "") {
365 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
369 } // function checkInput()
373 function saveAttachedFiles() {
374 global $HTTP_POST_FILES, $attachment_dir, $attachments;
377 $localfilename = GenerateRandomString(32, '', 7);
379 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
380 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
386 // Write information about the file
387 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
388 fputs ($fp, $HTTP_POST_FILES['attachfile']['type']."\n".$HTTP_POST_FILES['attachfile']['name']."\n");
391 $attachments[$localfilename] = $HTTP_POST_FILES['attachfile']['name'];
395 if (($mailbox == "") ||
($mailbox == "None"))
399 if ($HTTP_POST_FILES['attachfile']['tmp_name'] &&
400 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
401 $AttachFailure = saveAttachedFiles();
402 if (checkInput(false) && ! $AttachFailure) {
403 $urlMailbox = urlencode ($mailbox);
404 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
405 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
407 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
408 displayPageHeader($color, $mailbox);
411 plain_error_message(_("Could not move/copy file. File not attached"), $color);
416 //sqimap_logout($imapConnection);
418 } else if ($html_addr_search_done) {
420 displayPageHeader($color, $mailbox);
422 $send_to = sqStripSlashes($send_to);
423 $send_to_cc = sqStripSlashes($send_to_cc);
424 $send_to_bcc = sqStripSlashes($send_to_bcc);
426 for ($i=0; $i < count($send_to_search); $i++
) {
429 $send_to .= $send_to_search[$i];
432 for ($i=0; $i < count($send_to_cc_search); $i++
) {
435 $send_to_cc .= $send_to_cc_search[$i];
439 } else if ($html_addr_search) {
440 // I am using an include so as to elminiate an extra unnecessary click. If you
441 // can think of a better way, please implement it.
442 include ("./addrbook_search_html.php");
443 } else if (isset($attach)) {
444 if (saveAttachedFiles())
445 plain_error_message(_("Could not move/copy file. File not attached"), $color);
446 displayPageHeader($color, $mailbox);
448 } else if (isset($do_delete)) {
450 displayPageHeader($color, $mailbox);
452 while (list($lkey, $localname) = each($delete)) {
453 unset ($attachments[$localname]);
454 unlink ($attachment_dir.$localname);
455 unlink ($attachment_dir.$localname.".info");
459 } else if ($smtpErrors) {
460 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
461 displayPageHeader($color, $mailbox);
464 if ($forward_id && $ent_num) getAttachments(0);
468 sqimap_logout($imapConnection);
470 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
471 displayPageHeader($color, $mailbox);
475 if ($forward_id && $ent_num) getAttachments(0);
479 sqimap_logout($imapConnection);