078acf6662930429dd9493f6672ed8c884a1a622
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
20 if (!isset($strings_php))
21 include("../functions/strings.php");
22 if (!isset($config_php))
23 include("../config/config.php");
24 if (!isset($page_header_php))
25 include("../functions/page_header.php");
26 if (!isset($imap_php))
27 include("../functions/imap.php");
28 if (!isset($date_php))
29 include("../functions/date.php");
30 if (!isset($mime_php))
31 include("../functions/mime.php");
32 if (!isset($smtp_php))
33 include("../functions/smtp.php");
34 if (!isset($display_messages_php))
35 include("../functions/display_messages.php");
36 if (!isset($auth_php))
37 include ("../functions/auth.php");
38 if (!isset($plugin_php))
39 include ("../functions/plugin.php");
41 include("../src/load_prefs.php");
43 if (!isset($attachments))
44 $attachments = array();
46 // This function is used when not sending or adding attachments
48 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
49 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size;
51 $send_to = decodeHeader($send_to);
52 $send_to_cc = decodeHeader($send_to_cc);
53 $send_to_bcc = decodeHeader($send_to_bcc);
62 sqimap_mailbox_select($imapConnection, $mailbox);
63 $message = sqimap_get_message($imapConnection, $id, $mailbox);
64 $orig_header = $message->header
;
66 $message = getEntity($message, $ent_num);
68 if ($message->header
->type0
== "text" ||
$message->header
->type1
== "message") {
70 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header
->encoding
);
72 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header
->encoding
);
77 if ($message->header
->type1
== "html")
78 $body = strip_tags($body);
81 $body_ary = explode("\n", $body);
82 $i = count($body_ary) - 1;
83 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
88 for ($i=0; isset($body_ary[$i]); $i++
) {
91 if (ereg('^[\\s>]+', $body_ary[$i]))
93 $body_ary[$i] = '>' . $body_ary[$i];
97 $body_ary[$i] = '> ' . $body_ary[$i];
100 sqWordWrap($body_ary[$i], $editor_size - 1);
101 $body .= $body_ary[$i] . "\n";
102 unset($body_ary[$i]);
106 $bodyTop = "-------- " . _("Original Message") . " --------\n";
107 $bodyTop .= _("Subject") . ": " . $orig_header->subject
. "\n";
108 $bodyTop .= _("From") . ": " . $orig_header->from
. "\n";
109 $bodyTop .= _("To") . ": " . $orig_header->to
[0] . "\n";
110 if (count($orig_header->to
) > 1) {
111 for ($x=1; $x < count($orig_header->to
); $x++
) {
112 $bodyTop .= " " . $orig_header->to
[$x] . "\n";
116 $body = $bodyTop . $body;
123 $send_to = sqimap_find_email($send_to);
126 /** This formats a CC string if they hit "reply all" **/
127 if ($send_to_cc != "") {
128 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
129 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
130 $sendcc = explode(",", $send_to_cc);
133 for ($i = 0; $i < count($sendcc); $i++
) {
134 $sendcc[$i] = trim($sendcc[$i]);
135 if ($sendcc[$i] == "")
138 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
139 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
140 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
142 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
143 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
144 (trim($sendcc[$i]) != "")) {
145 $send_to_cc .= trim($sendcc[$i]) . ", ";
148 $send_to_cc = trim($send_to_cc);
149 if (substr($send_to_cc, -1) == ",") {
150 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
153 } // function newMail()
155 function getAttachments($message) {
156 global $mailbox, $attachments, $attachment_dir, $imapConnection,
157 $ent_num, $forward_id;
160 sqimap_mailbox_select($imapConnection, $mailbox);
161 $message = sqimap_get_message($imapConnection, $forward_id,
165 if (count($message->entities
) == 0) {
166 if ($message->header
->entity_id
!= $ent_num) {
167 $filename = decodeHeader($message->header
->filename
);
170 $filename = "untitled-".$message->header
->entity_id
;
172 $localfilename = GenerateRandomString(32, '', 7);
173 while (isset($attachments[$localfilename]))
174 $localfilename = GenerateRandomString(32, '', 7);
177 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
178 fputs ($fp, strtolower($message->header
->type0
)."/".
179 strtolower($message->header
->type1
)."\n".$filename."\n");
182 // Write Attachment to file
183 $fp = fopen ($attachment_dir.$localfilename, "w");
184 fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
185 $forward_id, $message->header
->entity_id
),
186 $message->header
->encoding
));
189 $attachments[$localfilename] = $filename;
192 for ($i = 0; $i < count($message->entities
); $i++
) {
193 getAttachments($message->entities
[$i]);
199 function showInputForm () {
200 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
201 $passed_body, $color, $use_signature, $signature, $prefix_sig,
202 $editor_size, $attachments, $subject, $newmail,
203 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
204 $from_htmladdr_search, $location_of_buttons;
206 $subject = decodeHeader($subject);
207 $reply_subj = decodeHeader($reply_subj);
208 $forward_subj = decodeHeader($forward_subj);
210 if ($use_javascript_addr_book) {
211 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
212 echo "function open_abook() { \n";
213 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
214 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
215 echo " if((!nwin.opener) && (document.windows != null))\n";
216 echo " nwin.opener = document.windows;\n";
218 echo "// --></SCRIPT>\n\n";
221 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
222 do_hook("compose_form");
225 echo "<input type=hidden name=reply_id value=$reply_id>\n";
227 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
228 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
230 if ($location_of_buttons == 'top') showComposeButtonRow();
233 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
235 echo " </TD><TD BGCOLOR=\"$color[4]\">\n";
236 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
237 htmlspecialchars($send_to));
241 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
243 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
244 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
245 htmlspecialchars($send_to_cc));
249 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
251 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
252 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
253 htmlspecialchars($send_to_bcc));
257 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
259 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
261 $reply_subj = str_replace("\"", "'", $reply_subj);
262 $reply_subj = trim($reply_subj);
263 if (substr(strtolower($reply_subj), 0, 3) != "re:")
264 $reply_subj = "Re: $reply_subj";
265 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
266 htmlspecialchars($reply_subj));
267 } else if ($forward_subj) {
268 $forward_subj = str_replace("\"", "'", $forward_subj);
269 $forward_subj = trim($forward_subj);
270 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
271 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
272 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
273 $forward_subj = "[Fwd: $forward_subj]";
274 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
275 htmlspecialchars($forward_subj));
277 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
278 htmlspecialchars($subject));
280 echo "</td></tr>\n\n";
282 if ($location_of_buttons == 'between') showComposeButtonRow();
285 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
286 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
287 echo htmlspecialchars($body);
288 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
289 if ( $prefix_sig == true )
290 echo "\n\n-- \n" . htmlspecialchars($signature);
292 echo "\n\n" . htmlspecialchars($signature);
294 echo "</TEXTAREA><BR>\n";
298 if ($location_of_buttons == 'bottom')
299 showComposeButtonRow();
301 echo " <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
304 // This code is for attachments
306 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
307 echo " <SMALL><BR></SMALL>"._("Attach:");
308 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
309 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
310 echo " <input type=\"submit\" name=\"attach\"";
311 echo " value=\"" . _("Add") ."\">\n";
314 if (count($attachments) > 0) {
315 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
317 echo "</td><td align=left bgcolor=\"$color[0]\">";
318 while (list($localname, $remotename) = each($attachments)) {
319 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
320 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
323 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
326 // End of attachment code
330 do_hook("compose_bottom");
333 function showComposeButtonRow() {
334 global $use_javascript_addr_book;
335 echo " <TR><td>\n </td><td>\n";
336 if ($use_javascript_addr_book) {
337 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
338 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
339 echo " // --></SCRIPT><NOSCRIPT>\n";
340 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
341 echo " </NOSCRIPT>\n";
343 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
345 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
347 do_hook("compose_button_row");
353 function showSentForm () {
354 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
355 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
359 function checkInput ($show) {
360 /** I implemented the $show variable because the error messages
361 were getting sent before the page header. So, I check once
362 using $show=false, and then when i'm ready to display the
363 error message, show=true **/
364 global $body, $send_to, $subject, $color;
366 if ($send_to == "") {
368 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
372 } // function checkInput()
376 function saveAttachedFiles() {
377 global $HTTP_POST_FILES, $attachment_dir, $attachments;
380 $localfilename = GenerateRandomString(32, '', 7);
381 while (isset($attachments[$localfilename]))
382 $localfilename = GenerateRandomString(32, '', 7);
384 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
385 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
390 if (!isset($failed) ||
!$failed) {
391 // Write information about the file
392 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
393 fputs ($fp, $HTTP_POST_FILES['attachfile']['type']."\n".$HTTP_POST_FILES['attachfile']['name']."\n");
396 $attachments[$localfilename] = $HTTP_POST_FILES['attachfile']['name'];
400 function SqConvertRussianCharsets(){
402 // This function is here because Russian Apache is a bastard when it comes to
403 // attachments. The solution is to turn off attachment recoding for multipart
404 // forms and do it manually.
405 // See graf@relhum.org for support.
407 global $CHARSET, $SOURCE_CHARSET, $send_to, $send_to_cc, $send_to_bcc, $subject, $body;
408 $charset_ary = array("koi8-r" => "k",
409 "windows-1251" => "w",
411 "ISO-8859-5" => "i");
412 $body = convert_cyr_string($body, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
413 $send_to = convert_cyr_string($send_to, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
414 $send_to_cc = convert_cyr_string($send_to_cc, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
415 $send_to_bcc = convert_cyr_string($send_to_bcc, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
416 $subject = convert_cyr_string($subject, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
417 } // end SqConvertRussianCharsets()
419 // Russian Apache sets $CHARSET. See if this is Russian Apache.
420 // If so, check if the source charset (koi8-r) is different from the
421 // one submitted by the browser. If so, recode the parts of the form
422 // to the needed format so SM can proceed and not mangle the cyrillic
424 // See graf@relhum.org for support.
426 if (isset($CHARSET) && $CHARSET != $SOURCE_CHARSET) SqConvertRussianCharsets();
428 if (!isset($mailbox) ||
$mailbox == "" ||
($mailbox == "None"))
432 if (isset($HTTP_POST_FILES['attachfile']) &&
433 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
434 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
435 $AttachFailure = saveAttachedFiles();
436 if (checkInput(false) && !isset($AttachFailure)) {
437 $urlMailbox = urlencode (trim($mailbox));
438 if (! isset($reply_id))
440 // Set $default_charset to correspond with the user's selection
441 // of language interface.
443 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
444 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
446 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
447 displayPageHeader($color, $mailbox);
450 plain_error_message(_("Could not move/copy file. File not attached"), $color);
455 //sqimap_logout($imapConnection);
457 } else if (isset($html_addr_search_done)) {
459 displayPageHeader($color, $mailbox);
461 for ($i=0; $i < count($send_to_search); $i++
) {
464 $send_to .= $send_to_search[$i];
467 for ($i=0; $i < count($send_to_cc_search); $i++
) {
470 $send_to_cc .= $send_to_cc_search[$i];
473 for ($i=0; $i < count($send_to_bcc_search); $i++
) {
475 $send_to_bcc .= ", ";
476 $send_to_bcc .= $send_to_bcc_search[$i];
480 } else if (isset($html_addr_search)) {
481 // I am using an include so as to elminiate an extra unnecessary click. If you
482 // can think of a better way, please implement it.
483 include ("./addrbook_search_html.php");
484 } else if (isset($attach)) {
485 if (saveAttachedFiles())
486 plain_error_message(_("Could not move/copy file. File not attached"), $color);
487 displayPageHeader($color, $mailbox);
489 } else if (isset($do_delete)) {
491 displayPageHeader($color, $mailbox);
493 while (list($lkey, $localname) = each($delete)) {
494 unset ($attachments[$localname]);
495 unlink ($attachment_dir.$localname);
496 unlink ($attachment_dir.$localname.".info");
501 // This handles the default case as well as the error case
502 // (they had the same code) --> if (isset($smtpErrors))
503 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
505 displayPageHeader($color, $mailbox);
508 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
513 sqimap_logout($imapConnection);