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/page_header.php");
20 include("../functions/imap.php");
21 include("../functions/date.php");
22 include("../functions/mime.php");
23 include("../functions/smtp.php");
24 include("../functions/display_messages.php");
25 include ("../functions/plugin.php");
26 include("../src/load_prefs.php");
28 if (!isset($attachments))
30 $attachments = array();
31 session_register('attachments');
35 // This function is used when not sending or adding attachments
37 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
38 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size;
40 $send_to = decodeHeader($send_to);
41 $send_to_cc = decodeHeader($send_to_cc);
42 $send_to_bcc = decodeHeader($send_to_bcc);
51 sqimap_mailbox_select($imapConnection, $mailbox);
52 $message = sqimap_get_message($imapConnection, $id, $mailbox);
53 $orig_header = $message->header
;
55 $message = getEntity($message, $ent_num);
57 if ($message->header
->type0
== "text" ||
$message->header
->type1
== "message") {
59 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header
->encoding
);
61 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header
->encoding
);
66 if ($message->header
->type1
== "html")
67 $body = strip_tags($body);
70 $body_ary = explode("\n", $body);
71 $i = count($body_ary) - 1;
72 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
77 for ($i=0; isset($body_ary[$i]); $i++
) {
80 if (ereg('^[ >]+', $body_ary[$i]))
82 $body_ary[$i] = '>' . $body_ary[$i];
86 $body_ary[$i] = '> ' . $body_ary[$i];
89 sqWordWrap($body_ary[$i], $editor_size - 1);
90 $body .= $body_ary[$i] . "\n";
95 $bodyTop = "-------- " . _("Original Message") . " --------\n";
96 $bodyTop .= _("Subject") . ": " . $orig_header->subject
. "\n";
97 $bodyTop .= _("From") . ": " . $orig_header->from
. "\n";
98 $bodyTop .= _("To") . ": " . $orig_header->to
[0] . "\n";
99 if (count($orig_header->to
) > 1) {
100 for ($x=1; $x < count($orig_header->to
); $x++
) {
101 $bodyTop .= " " . $orig_header->to
[$x] . "\n";
105 $body = $bodyTop . $body;
112 $send_to = sqimap_find_email($send_to);
115 /** This formats a CC string if they hit "reply all" **/
116 if ($send_to_cc != "") {
117 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
118 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
119 $sendcc = explode(",", $send_to_cc);
122 for ($i = 0; $i < count($sendcc); $i++
) {
123 $sendcc[$i] = trim($sendcc[$i]);
124 if ($sendcc[$i] == "")
127 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
128 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
129 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
131 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
132 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
133 (trim($sendcc[$i]) != "")) {
134 $send_to_cc .= trim($sendcc[$i]) . ", ";
137 $send_to_cc = trim($send_to_cc);
138 if (substr($send_to_cc, -1) == ",") {
139 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
142 } // function newMail()
144 function getAttachments($message) {
145 global $mailbox, $attachments, $attachment_dir, $imapConnection,
146 $ent_num, $forward_id;
149 sqimap_mailbox_select($imapConnection, $mailbox);
150 $message = sqimap_get_message($imapConnection, $forward_id,
154 if (count($message->entities
) == 0) {
155 if ($message->header
->entity_id
!= $ent_num) {
156 $filename = decodeHeader($message->header
->filename
);
159 $filename = "untitled-".$message->header
->entity_id
;
161 $localfilename = GenerateRandomString(32, '', 7);
162 while (file_exists($attachment_dir . $localfilename))
163 $localfilename = GenerateRandomString(32, '', 7);
165 $newAttachment['localfilename'] = $localfilename;
166 $newAttachment['remotefilename'] = $filename;
167 $newAttachment['type'] = strtolower($message->header
->type0
.
168 '/' . $message->header
->type1
);
170 // Write Attachment to file
171 $fp = fopen ($attachment_dir.$localfilename, 'w');
172 fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
173 $forward_id, $message->header
->entity_id
),
174 $message->header
->encoding
));
177 $attachments[] = $newAttachment;
180 for ($i = 0; $i < count($message->entities
); $i++
) {
181 getAttachments($message->entities
[$i]);
187 function showInputForm () {
188 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
189 $passed_body, $color, $use_signature, $signature, $prefix_sig,
190 $editor_size, $attachments, $subject, $newmail,
191 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
192 $from_htmladdr_search, $location_of_buttons, $attachment_dir;
194 $subject = decodeHeader($subject);
195 $reply_subj = decodeHeader($reply_subj);
196 $forward_subj = decodeHeader($forward_subj);
198 if ($use_javascript_addr_book) {
199 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
200 echo "function open_abook() { \n";
201 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
202 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
203 echo " if((!nwin.opener) && (document.windows != null))\n";
204 echo " nwin.opener = document.windows;\n";
206 echo "// --></SCRIPT>\n\n";
209 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
210 do_hook("compose_form");
213 echo "<input type=hidden name=reply_id value=$reply_id>\n";
215 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
216 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
218 if ($location_of_buttons == 'top') showComposeButtonRow();
221 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
223 echo " </TD><TD BGCOLOR=\"$color[4]\">\n";
224 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
225 htmlspecialchars($send_to));
229 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
231 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
232 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
233 htmlspecialchars($send_to_cc));
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_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
241 htmlspecialchars($send_to_bcc));
245 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
247 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
249 $reply_subj = str_replace("\"", "'", $reply_subj);
250 $reply_subj = trim($reply_subj);
251 if (substr(strtolower($reply_subj), 0, 3) != "re:")
252 $reply_subj = "Re: $reply_subj";
253 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
254 htmlspecialchars($reply_subj));
255 } else if ($forward_subj) {
256 $forward_subj = trim($forward_subj);
257 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
258 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
259 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:'))
260 $forward_subj = "[Fwd: $forward_subj]";
261 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
262 htmlspecialchars($forward_subj));
264 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
265 htmlspecialchars($subject));
267 echo "</td></tr>\n\n";
269 if ($location_of_buttons == 'between') showComposeButtonRow();
272 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
273 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
274 echo htmlspecialchars($body);
275 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
276 if ( $prefix_sig == true )
277 echo "\n\n-- \n" . htmlspecialchars($signature);
279 echo "\n\n" . htmlspecialchars($signature);
281 echo "</TEXTAREA><BR>\n";
285 if ($location_of_buttons == 'bottom')
286 showComposeButtonRow();
288 echo " <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
291 // This code is for attachments
293 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
294 echo " <SMALL><BR></SMALL>"._("Attach:");
295 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
296 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
297 echo " <input type=\"submit\" name=\"attach\"";
298 echo " value=\"" . _("Add") ."\">\n";
301 if (count($attachments))
303 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
305 echo "</td><td align=left bgcolor=\"$color[0]\">";
306 foreach ($attachments as $key => $info) {
307 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$key\">\n";
308 echo $info['remotefilename'] . " - " . $info['type'] . " (";
309 echo show_readable_size(filesize($attachment_dir .
310 $info['localfilename'])) . ")<br>\n";
313 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
316 // End of attachment code
320 do_hook("compose_bottom");
323 function showComposeButtonRow() {
324 global $use_javascript_addr_book;
326 echo " <TR><td>\n </td><td>\n";
327 if ($use_javascript_addr_book) {
328 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
329 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
330 echo " // --></SCRIPT><NOSCRIPT>\n";
331 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
332 echo " </NOSCRIPT>\n";
334 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
336 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
338 do_hook("compose_button_row");
344 function checkInput ($show) {
345 /** I implemented the $show variable because the error messages
346 were getting sent before the page header. So, I check once
347 using $show=false, and then when i'm ready to display the
348 error message, show=true **/
349 global $body, $send_to, $subject, $color;
351 if ($send_to == "") {
353 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
357 } // function checkInput()
361 function saveAttachedFiles() {
362 global $HTTP_POST_FILES, $attachment_dir, $attachments;
364 $localfilename = GenerateRandomString(32, '', 7);
365 while (file_exists($attachment_dir . $localfilename))
366 $localfilename = GenerateRandomString(32, '', 7);
368 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
369 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
374 $newAttachment['localfilename'] = $localfilename;
375 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
376 $newAttachment['type'] =
377 strtolower($HTTP_POST_FILES['attachfile']['type']);
379 $attachments[] = $newAttachment;
382 function SqConvertRussianCharsets(){
384 // This function is here because Russian Apache is a bastard when it comes to
385 // attachments. The solution is to turn off attachment recoding for multipart
386 // forms and do it manually.
387 // See graf@relhum.org for support.
389 global $CHARSET, $SOURCE_CHARSET, $send_to, $send_to_cc, $send_to_bcc, $subject, $body;
390 $charset_ary = array("koi8-r" => "k",
391 "windows-1251" => "w",
393 "ISO-8859-5" => "i");
394 $body = convert_cyr_string($body, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
395 $send_to = convert_cyr_string($send_to, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
396 $send_to_cc = convert_cyr_string($send_to_cc, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
397 $send_to_bcc = convert_cyr_string($send_to_bcc, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
398 $subject = convert_cyr_string($subject, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
399 } // end SqConvertRussianCharsets()
401 // Russian Apache sets $CHARSET. See if this is Russian Apache.
402 // If so, check if the source charset (koi8-r) is different from the
403 // one submitted by the browser. If so, recode the parts of the form
404 // to the needed format so SM can proceed and not mangle the cyrillic
406 // See graf@relhum.org for support.
408 if (isset($CHARSET) && $CHARSET != $SOURCE_CHARSET) SqConvertRussianCharsets();
410 if (!isset($mailbox) ||
$mailbox == "" ||
($mailbox == "None"))
414 if (isset($HTTP_POST_FILES['attachfile']) &&
415 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
416 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
417 $AttachFailure = saveAttachedFiles();
418 if (checkInput(false) && !isset($AttachFailure)) {
419 $urlMailbox = urlencode (trim($mailbox));
420 if (! isset($reply_id))
422 // Set $default_charset to correspond with the user's selection
423 // of language interface.
425 do_hook("compose_send");
427 if (! sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id)) {
431 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
433 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
434 displayPageHeader($color, $mailbox);
437 plain_error_message(_("Could not move/copy file. File not attached"), $color);
442 //sqimap_logout($imapConnection);
444 } else if (isset($html_addr_search_done)) {
445 displayPageHeader($color, $mailbox);
447 if (isset($send_to_search) && is_array($send_to_search)) {
448 foreach ($send_to_search as $k => $v) {
449 if (substr($k, 0, 1) == 'T') {
454 elseif (substr($k, 0, 1) == 'C') {
459 elseif (substr($k, 0, 1) == 'B') {
461 $send_to_bcc .= ', ';
468 } else if (isset($html_addr_search)) {
469 if (isset($HTTP_POST_FILES['attachfile']) &&
470 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
471 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
473 if (saveAttachedFiles())
474 plain_error_message(_("Could not move/copy file. File not attached"), $color);
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)) {
485 displayPageHeader($color, $mailbox);
487 if (isset($delete) && is_array($delete))
489 foreach($delete as $index)
491 unlink ($attachment_dir.$attachments[$index]['localfilename']);
492 unset ($attachments[$index]);
498 // This handles the default case as well as the error case
499 // (they had the same code) --> if (isset($smtpErrors))
500 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
502 displayPageHeader($color, $mailbox);
508 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
513 sqimap_logout($imapConnection);
519 function ClearAttachments() {
520 global $attachments, $attachment_dir;
522 foreach ($attachments as $info) {
523 if (file_exists($attachment_dir . $info['localfilename'])) {
524 unlink($attachment_dir . $info['localfilename']);
528 $attachments = array();