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 include('../src/validate.php');
19 include("../functions/strings.php");
20 include("../config/config.php");
21 include("../functions/page_header.php");
22 include("../functions/imap.php");
23 include("../functions/date.php");
24 include("../functions/mime.php");
25 include("../functions/smtp.php");
26 include("../functions/display_messages.php");
27 include ("../functions/plugin.php");
28 include("../src/load_prefs.php");
30 if (!isset($attachments))
32 $attachments = array();
33 session_register('attachments');
37 // This function is used when not sending or adding attachments
39 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
40 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size;
42 $send_to = decodeHeader($send_to);
43 $send_to_cc = decodeHeader($send_to_cc);
44 $send_to_bcc = decodeHeader($send_to_bcc);
53 sqimap_mailbox_select($imapConnection, $mailbox);
54 $message = sqimap_get_message($imapConnection, $id, $mailbox);
55 $orig_header = $message->header
;
57 $message = getEntity($message, $ent_num);
59 if ($message->header
->type0
== "text" ||
$message->header
->type1
== "message") {
61 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header
->encoding
);
63 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header
->encoding
);
68 if ($message->header
->type1
== "html")
69 $body = strip_tags($body);
72 $body_ary = explode("\n", $body);
73 $i = count($body_ary) - 1;
74 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
79 for ($i=0; isset($body_ary[$i]); $i++
) {
82 if (ereg('^[\\s>]+', $body_ary[$i]))
84 $body_ary[$i] = '>' . $body_ary[$i];
88 $body_ary[$i] = '> ' . $body_ary[$i];
91 sqWordWrap($body_ary[$i], $editor_size - 1);
92 $body .= $body_ary[$i] . "\n";
97 $bodyTop = "-------- " . _("Original Message") . " --------\n";
98 $bodyTop .= _("Subject") . ": " . $orig_header->subject
. "\n";
99 $bodyTop .= _("From") . ": " . $orig_header->from
. "\n";
100 $bodyTop .= _("To") . ": " . $orig_header->to
[0] . "\n";
101 if (count($orig_header->to
) > 1) {
102 for ($x=1; $x < count($orig_header->to
); $x++
) {
103 $bodyTop .= " " . $orig_header->to
[$x] . "\n";
107 $body = $bodyTop . $body;
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 getAttachments($message) {
147 global $mailbox, $attachments, $attachment_dir, $imapConnection,
148 $ent_num, $forward_id;
151 sqimap_mailbox_select($imapConnection, $mailbox);
152 $message = sqimap_get_message($imapConnection, $forward_id,
156 if (count($message->entities
) == 0) {
157 if ($message->header
->entity_id
!= $ent_num) {
158 $filename = decodeHeader($message->header
->filename
);
161 $filename = "untitled-".$message->header
->entity_id
;
163 $localfilename = GenerateRandomString(32, '', 7);
164 while (file_exists($attachment_dir . $localfilename))
165 $localfilename = GenerateRandomString(32, '', 7);
167 $newAttachment['localfilename'] = $localfilename;
168 $newAttachment['remotefilename'] = $filename;
169 $newAttachment['type'] = strtolower($message->header
->type0
.
170 '/' . $message->header
->type1
);
172 // Write Attachment to file
173 $fp = fopen ($attachment_dir.$localfilename, 'w');
174 fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
175 $forward_id, $message->header
->entity_id
),
176 $message->header
->encoding
));
179 $attachments[] = $newAttachment;
182 for ($i = 0; $i < count($message->entities
); $i++
) {
183 getAttachments($message->entities
[$i]);
189 function showInputForm () {
190 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
191 $passed_body, $color, $use_signature, $signature, $prefix_sig,
192 $editor_size, $attachments, $subject, $newmail,
193 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
194 $from_htmladdr_search, $location_of_buttons, $attachment_dir;
196 $subject = decodeHeader($subject);
197 $reply_subj = decodeHeader($reply_subj);
198 $forward_subj = decodeHeader($forward_subj);
200 if ($use_javascript_addr_book) {
201 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
202 echo "function open_abook() { \n";
203 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
204 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
205 echo " if((!nwin.opener) && (document.windows != null))\n";
206 echo " nwin.opener = document.windows;\n";
208 echo "// --></SCRIPT>\n\n";
211 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
212 do_hook("compose_form");
215 echo "<input type=hidden name=reply_id value=$reply_id>\n";
217 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
218 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
220 if ($location_of_buttons == 'top') showComposeButtonRow();
223 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
225 echo " </TD><TD BGCOLOR=\"$color[4]\">\n";
226 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
227 htmlspecialchars($send_to));
231 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
233 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
234 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
235 htmlspecialchars($send_to_cc));
239 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
241 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
242 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
243 htmlspecialchars($send_to_bcc));
247 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
249 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
251 $reply_subj = str_replace("\"", "'", $reply_subj);
252 $reply_subj = trim($reply_subj);
253 if (substr(strtolower($reply_subj), 0, 3) != "re:")
254 $reply_subj = "Re: $reply_subj";
255 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
256 htmlspecialchars($reply_subj));
257 } else if ($forward_subj) {
258 $forward_subj = trim($forward_subj);
259 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
260 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
261 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:'))
262 $forward_subj = "[Fwd: $forward_subj]";
263 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
264 htmlspecialchars($forward_subj));
266 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
267 htmlspecialchars($subject));
269 echo "</td></tr>\n\n";
271 if ($location_of_buttons == 'between') showComposeButtonRow();
274 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
275 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
276 echo htmlspecialchars($body);
277 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
278 if ( $prefix_sig == true )
279 echo "\n\n-- \n" . htmlspecialchars($signature);
281 echo "\n\n" . htmlspecialchars($signature);
283 echo "</TEXTAREA><BR>\n";
287 if ($location_of_buttons == 'bottom')
288 showComposeButtonRow();
290 echo " <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
293 // This code is for attachments
295 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
296 echo " <SMALL><BR></SMALL>"._("Attach:");
297 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
298 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
299 echo " <input type=\"submit\" name=\"attach\"";
300 echo " value=\"" . _("Add") ."\">\n";
303 if (count($attachments))
305 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
307 echo "</td><td align=left bgcolor=\"$color[0]\">";
308 foreach ($attachments as $key => $info) {
309 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$key\">\n";
310 echo $info['remotefilename'] . " - " . $info['type'] . " (";
311 echo show_readable_size(filesize($attachment_dir .
312 $info['localfilename'])) . ")<br>\n";
315 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
318 // End of attachment code
322 do_hook("compose_bottom");
325 function showComposeButtonRow() {
326 global $use_javascript_addr_book;
328 echo " <TR><td>\n </td><td>\n";
329 if ($use_javascript_addr_book) {
330 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
331 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
332 echo " // --></SCRIPT><NOSCRIPT>\n";
333 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
334 echo " </NOSCRIPT>\n";
336 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
338 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
340 do_hook("compose_button_row");
346 function checkInput ($show) {
347 /** I implemented the $show variable because the error messages
348 were getting sent before the page header. So, I check once
349 using $show=false, and then when i'm ready to display the
350 error message, show=true **/
351 global $body, $send_to, $subject, $color;
353 if ($send_to == "") {
355 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
359 } // function checkInput()
363 function saveAttachedFiles() {
364 global $HTTP_POST_FILES, $attachment_dir, $attachments;
366 $localfilename = GenerateRandomString(32, '', 7);
367 while (file_exists($attachment_dir . $localfilename))
368 $localfilename = GenerateRandomString(32, '', 7);
370 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
371 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
376 $newAttachment['localfilename'] = $localfilename;
377 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
378 $newAttachment['type'] =
379 strtolower($HTTP_POST_FILES['attachfile']['type']);
381 $attachments[] = $newAttachment;
384 function SqConvertRussianCharsets(){
386 // This function is here because Russian Apache is a bastard when it comes to
387 // attachments. The solution is to turn off attachment recoding for multipart
388 // forms and do it manually.
389 // See graf@relhum.org for support.
391 global $CHARSET, $SOURCE_CHARSET, $send_to, $send_to_cc, $send_to_bcc, $subject, $body;
392 $charset_ary = array("koi8-r" => "k",
393 "windows-1251" => "w",
395 "ISO-8859-5" => "i");
396 $body = convert_cyr_string($body, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
397 $send_to = convert_cyr_string($send_to, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
398 $send_to_cc = convert_cyr_string($send_to_cc, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
399 $send_to_bcc = convert_cyr_string($send_to_bcc, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
400 $subject = convert_cyr_string($subject, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
401 } // end SqConvertRussianCharsets()
403 // Russian Apache sets $CHARSET. See if this is Russian Apache.
404 // If so, check if the source charset (koi8-r) is different from the
405 // one submitted by the browser. If so, recode the parts of the form
406 // to the needed format so SM can proceed and not mangle the cyrillic
408 // See graf@relhum.org for support.
410 if (isset($CHARSET) && $CHARSET != $SOURCE_CHARSET) SqConvertRussianCharsets();
412 if (!isset($mailbox) ||
$mailbox == "" ||
($mailbox == "None"))
416 if (isset($HTTP_POST_FILES['attachfile']) &&
417 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
418 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
419 $AttachFailure = saveAttachedFiles();
420 if (checkInput(false) && !isset($AttachFailure)) {
421 $urlMailbox = urlencode (trim($mailbox));
422 if (! isset($reply_id))
424 // Set $default_charset to correspond with the user's selection
425 // of language interface.
427 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
428 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
430 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
431 displayPageHeader($color, $mailbox);
434 plain_error_message(_("Could not move/copy file. File not attached"), $color);
439 //sqimap_logout($imapConnection);
441 } else if (isset($html_addr_search_done)) {
442 displayPageHeader($color, $mailbox);
444 if (isset($send_to_search) && is_array($send_to_search))
446 for ($i=0; $i < count($send_to_search); $i++
) {
449 $send_to .= $send_to_search[$i];
453 if (isset($send_to_cc_search) && is_array($send_to_cc_search))
455 for ($i=0; $i < count($send_to_cc_search); $i++
) {
458 $send_to_cc .= $send_to_cc_search[$i];
462 if (isset($send_to_bcc_search) && is_array($send_to_bcc_search))
464 for ($i=0; $i < count($send_to_bcc_search); $i++
) {
466 $send_to_bcc .= ", ";
467 $send_to_bcc .= $send_to_bcc_search[$i];
472 } else if (isset($html_addr_search)) {
473 if (isset($HTTP_POST_FILES['attachfile']) &&
474 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
475 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
477 if (saveAttachedFiles())
478 plain_error_message(_("Could not move/copy file. File not attached"), $color);
480 // I am using an include so as to elminiate an extra unnecessary click. If you
481 // can think of a better way, please implement it.
482 include ("./addrbook_search_html.php");
483 } else if (isset($attach)) {
484 if (saveAttachedFiles())
485 plain_error_message(_("Could not move/copy file. File not attached"), $color);
486 displayPageHeader($color, $mailbox);
488 } else if (isset($do_delete)) {
489 displayPageHeader($color, $mailbox);
491 if (isset($delete) && is_array($delete))
493 foreach($delete as $index)
495 unlink ($attachment_dir.$attachments[$index]['localfilename']);
496 unset ($attachments[$index]);
502 // This handles the default case as well as the error case
503 // (they had the same code) --> if (isset($smtpErrors))
504 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
506 displayPageHeader($color, $mailbox);
512 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
517 sqimap_logout($imapConnection);
523 function ClearAttachments()
525 global $attachments, $attachment_dir;
527 foreach ($attachments as $info)
529 if (file_exists($attachment_dir . $info['localfilename']))
531 unlink($attachment_dir . $info['localfilename']);
535 $attachments = array();