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 require_once('../src/validate.php');
19 require_once('../functions/imap.php');
20 require_once('../functions/date.php');
21 require_once('../functions/mime.php');
22 require_once('../functions/smtp.php');
23 require_once('../functions/display_messages.php');
24 require_once('../functions/plugin.php');
26 if (!isset($attachments))
28 $attachments = array();
29 session_register('attachments');
33 // This function is used when not sending or adding attachments
35 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
36 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size,
37 $draft_id, $use_signature;
39 $send_to = decodeHeader($send_to);
40 $send_to_cc = decodeHeader($send_to_cc);
41 $send_to_bcc = decodeHeader($send_to_bcc);
50 $use_signature = FALSE;
54 sqimap_mailbox_select($imapConnection, $mailbox);
55 $message = sqimap_get_message($imapConnection, $id, $mailbox);
56 $orig_header = $message->header
;
58 $message = getEntity($message, $ent_num);
60 if ($message->header
->type0
== 'text' ||
$message->header
->type1
== 'message') {
62 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header
->encoding
);
64 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header
->encoding
);
69 if ($message->header
->type1
== "html")
70 $body = strip_tags($body);
73 $body_ary = explode("\n", $body);
74 $i = count($body_ary) - 1;
75 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
80 for ($i=0; isset($body_ary[$i]); $i++
) {
83 if (ereg('^[ >]+', $body_ary[$i]))
85 $body_ary[$i] = '>' . $body_ary[$i];
89 $body_ary[$i] = '> ' . $body_ary[$i];
92 sqWordWrap($body_ary[$i], $editor_size - 1);
93 $body .= $body_ary[$i] . "\n";
98 $bodyTop = "-------- " . _("Original Message") . " --------\n";
99 $bodyTop .= _("Subject") . ": " . $orig_header->subject
. "\n";
100 $bodyTop .= _("From") . ": " . $orig_header->from
. "\n";
101 $bodyTop .= _("To") . ": " . $orig_header->to
[0] . "\n";
102 if (count($orig_header->to
) > 1) {
103 for ($x=1; $x < count($orig_header->to
); $x++
) {
104 $bodyTop .= " " . $orig_header->to
[$x] . "\n";
108 $body = $bodyTop . $body;
109 } else if ($reply_id) {
110 $orig_from = decodeHeader($orig_header->from
);
111 $orig_from = trim(substr($orig_from,0,strpos($orig_from,'<')));
112 $orig_from = str_replace('"','',$orig_from);
113 $orig_from = str_replace("'",'',$orig_from);
114 $body = getReplyCitation($orig_from) . $body;
121 $send_to = sqimap_find_email($send_to);
124 /** This formats a CC string if they hit "reply all" **/
125 if ($send_to_cc != "") {
126 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
127 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
128 $sendcc = explode(",", $send_to_cc);
131 for ($i = 0; $i < count($sendcc); $i++
) {
132 $sendcc[$i] = trim($sendcc[$i]);
133 if ($sendcc[$i] == "")
136 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
137 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
138 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
140 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
141 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
142 (trim($sendcc[$i]) != "")) {
143 $send_to_cc .= trim($sendcc[$i]) . ", ";
146 $send_to_cc = trim($send_to_cc);
147 if (substr($send_to_cc, -1) == ",") {
148 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
151 } // function newMail()
153 function getAttachments($message) {
154 global $mailbox, $attachments, $attachment_dir, $imapConnection,
155 $ent_num, $forward_id, $draft_id;
157 if (isset($draft_id))
163 sqimap_mailbox_select($imapConnection, $mailbox);
164 $message = sqimap_get_message($imapConnection, $id,
168 if (count($message->entities
) == 0) {
169 if ($message->header
->entity_id
!= $ent_num) {
170 $filename = decodeHeader($message->header
->filename
);
173 $filename = "untitled-".$message->header
->entity_id
;
175 $localfilename = GenerateRandomString(32, '', 7);
176 while (file_exists($attachment_dir . $localfilename))
177 $localfilename = GenerateRandomString(32, '', 7);
179 $newAttachment = array();
180 $newAttachment['localfilename'] = $localfilename;
181 $newAttachment['remotefilename'] = $filename;
182 $newAttachment['type'] = strtolower($message->header
->type0
.
183 '/' . $message->header
->type1
);
185 // Write Attachment to file
186 $fp = fopen ($attachment_dir.$localfilename, 'w');
187 fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
188 $id, $message->header
->entity_id
),
189 $message->header
->encoding
));
192 $attachments[] = $newAttachment;
195 for ($i = 0; $i < count($message->entities
); $i++
) {
196 getAttachments($message->entities
[$i]);
202 function showInputForm () {
203 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
204 $passed_body, $color, $use_signature, $signature, $prefix_sig,
205 $editor_size, $attachments, $subject, $newmail,
206 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
207 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
208 $username, $data_dir, $identity, $draft_id;
210 $subject = decodeHeader($subject);
211 $reply_subj = decodeHeader($reply_subj);
212 $forward_subj = decodeHeader($forward_subj);
214 if ($use_javascript_addr_book) {
215 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
216 echo "function open_abook() { \n";
217 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
218 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
219 echo " if((!nwin.opener) && (document.windows != null))\n";
220 echo " nwin.opener = document.windows;\n";
222 echo "// --></SCRIPT>\n\n";
225 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
226 do_hook("compose_form");
229 if ( isset($draft_id))
230 echo "<input type=\"hidden\" name=\"delete_draft\" value=\"$draft_id\">\n";
232 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
234 if ($location_of_buttons == 'top') showComposeButtonRow();
236 $idents = getPref($data_dir, $username, 'identities');
237 if ($idents != '' && $idents > 1) {
239 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
241 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
242 echo "<select name=identity>\n";
243 echo "<option value=default>" .
244 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
245 $em = getPref($data_dir, $username, 'email_address');
247 echo htmlspecialchars(' <' . $em . '>') . "\n";
248 for ($i = 1; $i < $idents; $i ++
) {
249 echo '<option value="' . $i . '"';
250 if (isset($identity) && $identity == $i)
253 echo htmlspecialchars(getPref($data_dir, $username, 'full_name' .
255 $em = getPref($data_dir, $username, 'email_address' . $i);
257 echo htmlspecialchars(' <' . $em . '>') . "\n";
264 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
266 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
267 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
268 htmlspecialchars($send_to));
272 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
274 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
275 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
276 htmlspecialchars($send_to_cc));
280 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
282 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
283 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
284 htmlspecialchars($send_to_bcc));
288 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
290 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
292 $reply_subj = str_replace("\"", "'", $reply_subj);
293 $reply_subj = trim($reply_subj);
294 if (substr(strtolower($reply_subj), 0, 3) != "re:")
295 $reply_subj = "Re: $reply_subj";
296 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
297 htmlspecialchars($reply_subj));
298 } else if ($forward_subj) {
299 $forward_subj = trim($forward_subj);
300 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
301 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
302 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:'))
303 $forward_subj = "[Fwd: $forward_subj]";
304 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
305 htmlspecialchars($forward_subj));
307 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
308 htmlspecialchars($subject));
310 echo "</td></tr>\n\n";
312 if ($location_of_buttons == 'between') showComposeButtonRow();
315 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
316 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
317 echo htmlspecialchars($body);
318 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
319 if ( $prefix_sig == true )
320 echo "\n\n-- \n" . htmlspecialchars($signature);
322 echo "\n\n" . htmlspecialchars($signature);
324 echo "</TEXTAREA><BR>\n";
328 if ($location_of_buttons == 'bottom')
329 showComposeButtonRow();
331 echo " <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
334 // This code is for attachments
336 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
337 echo " <SMALL><BR></SMALL>"._("Attach:");
338 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
339 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
340 echo " <input type=\"submit\" name=\"attach\"";
341 echo " value=\"" . _("Add") ."\">\n";
344 if (count($attachments))
346 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
348 echo "</td><td align=left bgcolor=\"$color[0]\">";
349 foreach ($attachments as $key => $info) {
350 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$key\">\n";
351 echo $info['remotefilename'] . " - " . $info['type'] . " (";
352 echo show_readable_size(filesize($attachment_dir .
353 $info['localfilename'])) . ")<br>\n";
356 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
359 // End of attachment code
363 echo "<input type=hidden name=reply_id value=$reply_id>\n";
365 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
367 do_hook("compose_bottom");
370 function showComposeButtonRow() {
371 global $use_javascript_addr_book, $save_as_draft;
373 echo " <TR><td>\n </td><td>\n";
374 if ($use_javascript_addr_book) {
375 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
376 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
377 echo " // --></SCRIPT><NOSCRIPT>\n";
378 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
379 echo " </NOSCRIPT>\n";
381 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
383 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
385 if ($save_as_draft == true)
386 echo "<input type=\"submit\" name =\"draft\" value=\"Save Draft\">\n";
389 do_hook("compose_button_row");
395 function checkInput ($show) {
396 /** I implemented the $show variable because the error messages
397 were getting sent before the page header. So, I check once
398 using $show=false, and then when i'm ready to display the
399 error message, show=true **/
400 global $body, $send_to, $subject, $color;
402 if ($send_to == "") {
404 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
408 } // function checkInput()
412 function saveAttachedFiles() {
413 global $HTTP_POST_FILES, $attachment_dir, $attachments;
415 $localfilename = GenerateRandomString(32, '', 7);
416 while (file_exists($attachment_dir . $localfilename))
417 $localfilename = GenerateRandomString(32, '', 7);
419 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
420 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
425 $newAttachment['localfilename'] = $localfilename;
426 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
427 $newAttachment['type'] =
428 strtolower($HTTP_POST_FILES['attachfile']['type']);
430 if ($newAttachment['type'] == "")
431 $newAttachment['type'] = 'application/octet-stream';
433 $attachments[] = $newAttachment;
436 if (!isset($mailbox) ||
$mailbox == "" ||
($mailbox == "None"))
440 require_once ('../src/draft_actions.php');
441 if(! saveMessagetoDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $
$reply_id)) {
445 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort&startMessage=1¬e=Draft%20Email%20Saved");
451 if (isset($HTTP_POST_FILES['attachfile']) &&
452 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
453 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
454 $AttachFailure = saveAttachedFiles();
455 if (checkInput(false) && !isset($AttachFailure)) {
456 $urlMailbox = urlencode (trim($mailbox));
457 if (! isset($reply_id))
459 // Set $default_charset to correspond with the user's selection
460 // of language interface.
463 // This is to change all newlines to \n
464 // We'll change them to \r\n later (in the sendMessage function)
465 $body = str_replace("\r\n", "\n", $body);
466 $body = str_replace("\r", "\n", $body);
468 // Rewrap $body so that no line is bigger than $editor_size
469 // This should only really kick in the sqWordWrap function
470 // if the browser doesn't support "HARD" as the wrap type
471 // Or, in Opera's case, something goes wrong.
472 $body = explode("\n", $body);
474 foreach ($body as $line) {
476 $line = rtrim($line);
477 if (strlen($line) <= $editor_size +
1)
478 $newBody .= $line . "\n";
480 sqWordWrap($line, $editor_size) . "\n";
486 do_hook("compose_send");
488 if (! sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id)) {
492 if ( isset($delete_draft)) {
493 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
497 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
499 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
500 displayPageHeader($color, $mailbox);
502 if (isset($AttachFailure))
503 plain_error_message(_("Could not move/copy file. File not attached"), $color);
508 //sqimap_logout($imapConnection);
510 } else if (isset($html_addr_search_done)) {
511 displayPageHeader($color, $mailbox);
513 if (isset($send_to_search) && is_array($send_to_search)) {
514 foreach ($send_to_search as $k => $v) {
515 if (substr($k, 0, 1) == 'T') {
520 elseif (substr($k, 0, 1) == 'C') {
525 elseif (substr($k, 0, 1) == 'B') {
527 $send_to_bcc .= ', ';
534 } else if (isset($html_addr_search)) {
535 if (isset($HTTP_POST_FILES['attachfile']) &&
536 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
537 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
539 if (saveAttachedFiles())
540 plain_error_message(_("Could not move/copy file. File not attached"), $color);
542 // I am using an include so as to elminiate an extra unnecessary click. If you
543 // can think of a better way, please implement it.
544 include_once('./addrbook_search_html.php');
545 } else if (isset($attach)) {
546 if (saveAttachedFiles())
547 plain_error_message(_("Could not move/copy file. File not attached"), $color);
548 displayPageHeader($color, $mailbox);
550 } else if (isset($do_delete)) {
551 displayPageHeader($color, $mailbox);
553 if (isset($delete) && is_array($delete))
555 foreach($delete as $index)
557 unlink ($attachment_dir.$attachments[$index]['localfilename']);
558 unset ($attachments[$index]);
564 // This handles the default case as well as the error case
565 // (they had the same code) --> if (isset($smtpErrors))
566 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
568 displayPageHeader($color, $mailbox);
574 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
577 if (isset($draft_id) && $draft_id && isset($ent_num) && $ent_num)
582 sqimap_logout($imapConnection);
585 function ClearAttachments() {
586 global $attachments, $attachment_dir;
588 foreach ($attachments as $info) {
589 if (file_exists($attachment_dir . $info['localfilename'])) {
590 unlink($attachment_dir . $info['localfilename']);
594 $attachments = array();
597 function getReplyCitation($orig_from) {
598 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
600 /* First, return an empty string when no citation style selected. */
601 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
605 /* Otherwise, try to select the desired citation style. */
606 switch ($reply_citation_style) {
609 $end = ' ' . _("said") . ':';
612 $start = '<' . _("quote") . ' ' . _("who") . '="';
616 $start = $reply_citation_start;
617 $end = $reply_citation_end;
619 default: return ('');
622 /* Build and return the citation string. */
623 return ($start . $orig_from . $end . "\n");