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))
45 $attachments = array();
48 // This function is used when not sending or adding attachments
50 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
51 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size;
53 $send_to = decodeHeader($send_to);
54 $send_to_cc = decodeHeader($send_to_cc);
55 $send_to_bcc = decodeHeader($send_to_bcc);
64 sqimap_mailbox_select($imapConnection, $mailbox);
65 $message = sqimap_get_message($imapConnection, $id, $mailbox);
66 $orig_header = $message->header
;
68 $message = getEntity($message, $ent_num);
70 if ($message->header
->type0
== "text" ||
$message->header
->type1
== "message") {
72 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header
->encoding
);
74 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header
->encoding
);
79 if ($message->header
->type1
== "html")
80 $body = strip_tags($body);
83 $body_ary = explode("\n", $body);
84 $i = count($body_ary) - 1;
85 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
90 for ($i=0; isset($body_ary[$i]); $i++
) {
93 if (ereg('^[\\s>]+', $body_ary[$i]))
95 $body_ary[$i] = '>' . $body_ary[$i];
99 $body_ary[$i] = '> ' . $body_ary[$i];
102 sqWordWrap($body_ary[$i], $editor_size - 1);
103 $body .= $body_ary[$i] . "\n";
104 unset($body_ary[$i]);
108 $bodyTop = "-------- " . _("Original Message") . " --------\n";
109 $bodyTop .= _("Subject") . ": " . $orig_header->subject
. "\n";
110 $bodyTop .= _("From") . ": " . $orig_header->from
. "\n";
111 $bodyTop .= _("To") . ": " . $orig_header->to
[0] . "\n";
112 if (count($orig_header->to
) > 1) {
113 for ($x=1; $x < count($orig_header->to
); $x++
) {
114 $bodyTop .= " " . $orig_header->to
[$x] . "\n";
118 $body = $bodyTop . $body;
125 $send_to = sqimap_find_email($send_to);
128 /** This formats a CC string if they hit "reply all" **/
129 if ($send_to_cc != "") {
130 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
131 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
132 $sendcc = explode(",", $send_to_cc);
135 for ($i = 0; $i < count($sendcc); $i++
) {
136 $sendcc[$i] = trim($sendcc[$i]);
137 if ($sendcc[$i] == "")
140 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
141 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
142 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
144 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
145 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
146 (trim($sendcc[$i]) != "")) {
147 $send_to_cc .= trim($sendcc[$i]) . ", ";
150 $send_to_cc = trim($send_to_cc);
151 if (substr($send_to_cc, -1) == ",") {
152 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
155 } // function newMail()
157 function getAttachments($message) {
158 global $mailbox, $attachments, $attachment_dir, $imapConnection,
159 $ent_num, $forward_id;
162 sqimap_mailbox_select($imapConnection, $mailbox);
163 $message = sqimap_get_message($imapConnection, $forward_id,
167 if (count($message->entities
) == 0) {
168 if ($message->header
->entity_id
!= $ent_num) {
169 $filename = decodeHeader($message->header
->filename
);
172 $filename = "untitled-".$message->header
->entity_id
;
174 $localfilename = GenerateRandomString(32, '', 7);
175 while (isset($attachments[$localfilename]))
176 $localfilename = GenerateRandomString(32, '', 7);
179 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
180 fputs ($fp, strtolower($message->header
->type0
)."/".
181 strtolower($message->header
->type1
)."\n".$filename."\n");
184 // Write Attachment to file
185 $fp = fopen ($attachment_dir.$localfilename, "w");
186 fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
187 $forward_id, $message->header
->entity_id
),
188 $message->header
->encoding
));
191 $attachments[$localfilename] = $filename;
194 for ($i = 0; $i < count($message->entities
); $i++
) {
195 getAttachments($message->entities
[$i]);
201 function showInputForm () {
202 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
203 $passed_body, $color, $use_signature, $signature, $prefix_sig,
204 $editor_size, $attachments, $subject, $newmail,
205 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
206 $from_htmladdr_search, $location_of_buttons;
208 $subject = decodeHeader($subject);
209 $reply_subj = decodeHeader($reply_subj);
210 $forward_subj = decodeHeader($forward_subj);
212 if ($use_javascript_addr_book) {
213 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
214 echo "function open_abook() { \n";
215 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
216 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
217 echo " if((!nwin.opener) && (document.windows != null))\n";
218 echo " nwin.opener = document.windows;\n";
220 echo "// --></SCRIPT>\n\n";
223 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
224 do_hook("compose_form");
227 echo "<input type=hidden name=reply_id value=$reply_id>\n";
229 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
230 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
232 if ($location_of_buttons == 'top') showComposeButtonRow();
235 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
237 echo " </TD><TD BGCOLOR=\"$color[4]\">\n";
238 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
239 htmlspecialchars($send_to));
243 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
245 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
246 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
247 htmlspecialchars($send_to_cc));
251 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
253 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
254 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
255 htmlspecialchars($send_to_bcc));
259 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
261 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
263 $reply_subj = str_replace("\"", "'", $reply_subj);
264 $reply_subj = trim($reply_subj);
265 if (substr(strtolower($reply_subj), 0, 3) != "re:")
266 $reply_subj = "Re: $reply_subj";
267 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
268 htmlspecialchars($reply_subj));
269 } else if ($forward_subj) {
270 $forward_subj = str_replace("\"", "'", $forward_subj);
271 $forward_subj = trim($forward_subj);
272 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
273 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
274 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
275 $forward_subj = "[Fwd: $forward_subj]";
276 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
277 htmlspecialchars($forward_subj));
279 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
280 htmlspecialchars($subject));
282 echo "</td></tr>\n\n";
284 if ($location_of_buttons == 'between') showComposeButtonRow();
287 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
288 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
289 echo htmlspecialchars($body);
290 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
291 if ( $prefix_sig == true )
292 echo "\n\n-- \n" . htmlspecialchars($signature);
294 echo "\n\n" . htmlspecialchars($signature);
296 echo "</TEXTAREA><BR>\n";
300 if ($location_of_buttons == 'bottom')
301 showComposeButtonRow();
303 echo " <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
306 // This code is for attachments
307 echo " <tr bgcolor=\"$color[0]\">\n";
308 echo " <TD VALIGN=TOP ALIGN=RIGHT>"._("Attach:");
309 echo " </td><td ALIGN=left>\n";
310 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
311 echo " <input type=\"submit\" name=\"attach\"";
312 echo " value=\"" . _("Add") ."\">\n";
315 if (count($attachments) > 0) {
316 echo "<tr bgcolor=\"$color[0]\"><td align=right>\n";
318 echo "</td><td align=left>";
319 foreach ($attachments as $localname => $remotename) {
320 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
321 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
324 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
327 // End of attachment code
331 do_hook("compose_bottom");
334 function showComposeButtonRow() {
335 global $use_javascript_addr_book;
336 echo " <TR><td>\n </td><td>\n";
337 if ($use_javascript_addr_book) {
338 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
339 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
340 echo " // --></SCRIPT><NOSCRIPT>\n";
341 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
342 echo " </NOSCRIPT>\n";
344 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
346 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
348 do_hook("compose_button_row");
354 function showSentForm () {
355 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
356 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
360 function checkInput ($show) {
361 /** I implemented the $show variable because the error messages
362 were getting sent before the page header. So, I check once
363 using $show=false, and then when i'm ready to display the
364 error message, show=true **/
365 global $body, $send_to, $subject, $color;
367 if ($send_to == "") {
369 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
373 } // function checkInput()
377 function saveAttachedFiles() {
378 global $HTTP_POST_FILES, $attachment_dir, $attachments;
381 $localfilename = GenerateRandomString(32, '', 7);
382 while (isset($attachments[$localfilename]))
383 $localfilename = GenerateRandomString(32, '', 7);
385 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
386 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
391 if (!isset($failed) ||
!$failed) {
392 // Write information about the file
393 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
394 fputs ($fp, $HTTP_POST_FILES['attachfile']['type']."\n".$HTTP_POST_FILES['attachfile']['name']."\n");
397 $attachments[$localfilename] = $HTTP_POST_FILES['attachfile']['name'];
401 function SqConvertRussianCharsets(){
403 // This function is here because Russian Apache is a bastard when it comes to
404 // attachments. The solution is to turn off attachment recoding for multipart
405 // forms and do it manually.
406 // See graf@relhum.org for support.
408 global $CHARSET, $SOURCE_CHARSET, $send_to, $send_to_cc, $send_to_bcc, $subject, $body;
409 $charset_ary = array("koi8-r" => "k",
410 "windows-1251" => "w",
412 "ISO-8859-5" => "i");
413 $body = convert_cyr_string($body, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
414 $send_to = convert_cyr_string($send_to, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
415 $send_to_cc = convert_cyr_string($send_to_cc, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
416 $send_to_bcc = convert_cyr_string($send_to_bcc, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
417 $subject = convert_cyr_string($subject, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
418 } // end SqConvertRussianCharsets()
420 // Russian Apache sets $CHARSET. See if this is Russian Apache.
421 // If so, check if the source charset (koi8-r) is different from the
422 // one submitted by the browser. If so, recode the parts of the form
423 // to the needed format so SM can proceed and not mangle the cyrillic
425 // See graf@relhum.org for support.
427 if (isset($CHARSET) && $CHARSET != $SOURCE_CHARSET) SqConvertRussianCharsets();
429 if (!isset($mailbox) ||
$mailbox == "" ||
($mailbox == "None"))
433 if (isset($HTTP_POST_FILES['attachfile']) &&
434 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
435 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
436 $AttachFailure = saveAttachedFiles();
437 if (checkInput(false) && !isset($AttachFailure)) {
438 $urlMailbox = urlencode (trim($mailbox));
439 if (! isset($reply_id))
441 // Set $default_charset to correspond with the user's selection
442 // of language interface.
444 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
445 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
447 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
448 displayPageHeader($color, $mailbox);
451 plain_error_message(_("Could not move/copy file. File not attached"), $color);
456 //sqimap_logout($imapConnection);
458 } else if (isset($html_addr_search_done)) {
460 displayPageHeader($color, $mailbox);
462 for ($i=0; $i < count($send_to_search); $i++
) {
465 $send_to .= $send_to_search[$i];
468 for ($i=0; $i < count($send_to_cc_search); $i++
) {
471 $send_to_cc .= $send_to_cc_search[$i];
475 } else if (isset($html_addr_search)) {
476 // I am using an include so as to elminiate an extra unnecessary click. If you
477 // can think of a better way, please implement it.
478 include ("./addrbook_search_html.php");
479 } else if (isset($attach)) {
480 if (saveAttachedFiles())
481 plain_error_message(_("Could not move/copy file. File not attached"), $color);
482 displayPageHeader($color, $mailbox);
484 } else if (isset($do_delete)) {
486 displayPageHeader($color, $mailbox);
488 while (list($lkey, $localname) = each($delete)) {
489 unset ($attachments[$localname]);
490 unlink ($attachment_dir.$localname);
491 unlink ($attachment_dir.$localname.".info");
496 // This handles the default case as well as the error case
497 // (they had the same code) --> if (isset($smtpErrors))
498 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
500 displayPageHeader($color, $mailbox);
503 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
508 sqimap_logout($imapConnection);