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 4 modes of operation:
19 require_once('../src/validate.php');
20 require_once('../functions/imap.php');
21 require_once('../functions/date.php');
22 require_once('../functions/mime.php');
23 require_once('../functions/smtp.php');
24 require_once('../functions/display_messages.php');
25 require_once('../functions/plugin.php');
27 if (!isset($attachments))
29 $attachments = array();
30 session_register('attachments');
34 // This function is used when not sending or adding attachments
36 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
37 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size,
38 $draft_id, $use_signature;
40 $send_to = decodeHeader($send_to);
41 $send_to_cc = decodeHeader($send_to_cc);
42 $send_to_bcc = decodeHeader($send_to_bcc);
51 $use_signature = FALSE;
55 sqimap_mailbox_select($imapConnection, $mailbox);
56 $message = sqimap_get_message($imapConnection, $id, $mailbox);
57 $orig_header = $message->header
;
59 $message = getEntity($message, $ent_num);
61 if ($message->header
->type0
== 'text' ||
$message->header
->type1
== 'message') {
63 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header
->encoding
);
65 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header
->encoding
);
70 if ($message->header
->type1
== "html")
71 $body = strip_tags($body);
74 $body_ary = explode("\n", $body);
75 $i = count($body_ary) - 1;
76 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
81 for ($i=0; isset($body_ary[$i]); $i++
) {
84 if (ereg('^[ >]+', $body_ary[$i]))
86 $body_ary[$i] = '>' . $body_ary[$i];
90 $body_ary[$i] = '> ' . $body_ary[$i];
93 sqWordWrap($body_ary[$i], $editor_size - 1);
94 $body .= $body_ary[$i] . "\n";
99 $bodyTop = "-------- " . _("Original Message") . " --------\n";
100 $bodyTop .= _("Subject") . ": " . $orig_header->subject
. "\n";
101 $bodyTop .= _("From") . ": " . $orig_header->from
. "\n";
102 $bodyTop .= _("To") . ": " . $orig_header->to
[0] . "\n";
103 if (count($orig_header->to
) > 1) {
104 for ($x=1; $x < count($orig_header->to
); $x++
) {
105 $bodyTop .= " " . $orig_header->to
[$x] . "\n";
109 $body = $bodyTop . $body;
110 } else if ($reply_id) {
111 $orig_from = decodeHeader($orig_header->from
);
112 $orig_from = trim(substr($orig_from,0,strpos($orig_from,'<')));
113 $orig_from = str_replace('"','',$orig_from);
114 $orig_from = str_replace("'",'',$orig_from);
115 $body = getReplyCitation($orig_from) . $body;
122 $send_to = sqimap_find_email($send_to);
125 /** This formats a CC string if they hit "reply all" **/
126 if ($send_to_cc != "") {
127 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
128 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
129 $sendcc = explode(",", $send_to_cc);
132 for ($i = 0; $i < count($sendcc); $i++
) {
133 $sendcc[$i] = trim($sendcc[$i]);
134 if ($sendcc[$i] == "")
137 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
138 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
139 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
141 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
142 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
143 (trim($sendcc[$i]) != "")) {
144 $send_to_cc .= trim($sendcc[$i]) . ", ";
147 $send_to_cc = trim($send_to_cc);
148 if (substr($send_to_cc, -1) == ",") {
149 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
152 } // function newMail()
154 function getAttachments($message) {
155 global $mailbox, $attachments, $attachment_dir, $imapConnection,
156 $ent_num, $forward_id, $draft_id;
158 if (isset($draft_id))
164 sqimap_mailbox_select($imapConnection, $mailbox);
165 $message = sqimap_get_message($imapConnection, $id,
169 if (count($message->entities
) == 0) {
170 if ($message->header
->entity_id
!= $ent_num) {
171 $filename = decodeHeader($message->header
->filename
);
174 $filename = "untitled-".$message->header
->entity_id
;
176 $localfilename = GenerateRandomString(32, '', 7);
177 while (file_exists($attachment_dir . $localfilename))
178 $localfilename = GenerateRandomString(32, '', 7);
180 $newAttachment = array();
181 $newAttachment['localfilename'] = $localfilename;
182 $newAttachment['remotefilename'] = $filename;
183 $newAttachment['type'] = strtolower($message->header
->type0
.
184 '/' . $message->header
->type1
);
186 // Write Attachment to file
187 $fp = fopen ($attachment_dir.$localfilename, 'w');
188 fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
189 $id, $message->header
->entity_id
),
190 $message->header
->encoding
));
193 $attachments[] = $newAttachment;
196 for ($i = 0; $i < count($message->entities
); $i++
) {
197 getAttachments($message->entities
[$i]);
203 function showInputForm () {
204 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
205 $passed_body, $color, $use_signature, $signature, $prefix_sig,
206 $editor_size, $attachments, $subject, $newmail,
207 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
208 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
209 $username, $data_dir, $identity, $draft_id;
211 $subject = decodeHeader($subject);
212 $reply_subj = decodeHeader($reply_subj);
213 $forward_subj = decodeHeader($forward_subj);
215 if ($use_javascript_addr_book) {
216 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
217 echo "function open_abook() { \n";
218 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
219 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
220 echo " if((!nwin.opener) && (document.windows != null))\n";
221 echo " nwin.opener = document.windows;\n";
223 echo "// --></SCRIPT>\n\n";
226 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
227 do_hook("compose_form");
230 if (isset($draft_id)) {
231 echo "<input type=\"hidden\" name=\"delete_draft\" value=\"$draft_id\">\n";
234 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
236 if ($location_of_buttons == 'top') showComposeButtonRow();
238 $idents = getPref($data_dir, $username, 'identities');
239 if ($idents != '' && $idents > 1) {
241 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
243 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
244 echo "<select name=identity>\n";
245 echo "<option value=default>" .
246 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
247 $em = getPref($data_dir, $username, 'email_address');
249 echo htmlspecialchars(' <' . $em . '>') . "\n";
250 for ($i = 1; $i < $idents; $i ++
) {
251 echo '<option value="' . $i . '"';
252 if (isset($identity) && $identity == $i)
255 echo htmlspecialchars(getPref($data_dir, $username, 'full_name' .
257 $em = getPref($data_dir, $username, 'email_address' . $i);
259 echo htmlspecialchars(' <' . $em . '>') . "\n";
266 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
268 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
269 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
270 htmlspecialchars($send_to));
274 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
276 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
277 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
278 htmlspecialchars($send_to_cc));
282 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
284 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
285 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
286 htmlspecialchars($send_to_bcc));
290 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
292 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
294 $reply_subj = str_replace("\"", "'", $reply_subj);
295 $reply_subj = trim($reply_subj);
296 if (substr(strtolower($reply_subj), 0, 3) != "re:")
297 $reply_subj = "Re: $reply_subj";
298 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
299 htmlspecialchars($reply_subj));
300 } else if ($forward_subj) {
301 $forward_subj = trim($forward_subj);
302 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
303 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
304 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:'))
305 $forward_subj = "[Fwd: $forward_subj]";
306 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
307 htmlspecialchars($forward_subj));
309 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
310 htmlspecialchars($subject));
312 echo "</td></tr>\n\n";
314 if ($location_of_buttons == 'between') showComposeButtonRow();
317 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
318 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
319 echo htmlspecialchars($body);
320 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
321 if ( $prefix_sig == true )
322 echo "\n\n-- \n" . htmlspecialchars($signature);
324 echo "\n\n" . htmlspecialchars($signature);
326 echo "</TEXTAREA><BR>\n";
330 if ($location_of_buttons == 'bottom')
331 showComposeButtonRow();
333 echo " <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
336 // This code is for attachments
338 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
339 echo " <SMALL><BR></SMALL>"._("Attach:");
340 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
341 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
342 echo " <input type=\"submit\" name=\"attach\"";
343 echo " value=\"" . _("Add") ."\">\n";
346 if (count($attachments))
348 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
350 echo "</td><td align=left bgcolor=\"$color[0]\">";
351 foreach ($attachments as $key => $info) {
352 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$key\">\n";
353 echo $info['remotefilename'] . " - " . $info['type'] . " (";
354 echo show_readable_size(filesize($attachment_dir .
355 $info['localfilename'])) . ")<br>\n";
358 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
361 // End of attachment code
365 echo "<input type=hidden name=reply_id value=$reply_id>\n";
367 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
369 do_hook("compose_bottom");
372 function showComposeButtonRow() {
373 global $use_javascript_addr_book, $save_as_draft;
375 echo " <TR><td>\n </td><td>\n";
376 if ($use_javascript_addr_book) {
377 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
378 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
379 echo " // --></SCRIPT><NOSCRIPT>\n";
380 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
381 echo " </NOSCRIPT>\n";
383 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
385 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
387 if ($save_as_draft) {
388 echo "<input type=\"submit\" name =\"draft\" value=\"Save Draft\">\n";
391 do_hook("compose_button_row");
397 function checkInput ($show) {
398 /** I implemented the $show variable because the error messages
399 were getting sent before the page header. So, I check once
400 using $show=false, and then when i'm ready to display the
401 error message, show=true **/
402 global $body, $send_to, $subject, $color;
404 if ($send_to == "") {
406 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
410 } // function checkInput()
414 function saveAttachedFiles() {
415 global $HTTP_POST_FILES, $attachment_dir, $attachments;
417 $localfilename = GenerateRandomString(32, '', 7);
418 while (file_exists($attachment_dir . $localfilename))
419 $localfilename = GenerateRandomString(32, '', 7);
421 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
422 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
427 $newAttachment['localfilename'] = $localfilename;
428 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
429 $newAttachment['type'] =
430 strtolower($HTTP_POST_FILES['attachfile']['type']);
432 if ($newAttachment['type'] == "")
433 $newAttachment['type'] = 'application/octet-stream';
435 $attachments[] = $newAttachment;
438 if (!isset($mailbox) ||
$mailbox == "" ||
($mailbox == "None"))
442 require_once ('../src/draft_actions.php');
443 if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id)) {
447 $draft_message = _("Draft Email Saved");
448 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort&startMessage=1¬e=$draft_message");
454 if (isset($HTTP_POST_FILES['attachfile']) &&
455 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
456 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
457 $AttachFailure = saveAttachedFiles();
458 if (checkInput(false) && !isset($AttachFailure)) {
459 $urlMailbox = urlencode (trim($mailbox));
460 if (! isset($reply_id))
462 // Set $default_charset to correspond with the user's selection
463 // of language interface.
466 // This is to change all newlines to \n
467 // We'll change them to \r\n later (in the sendMessage function)
468 $body = str_replace("\r\n", "\n", $body);
469 $body = str_replace("\r", "\n", $body);
471 // Rewrap $body so that no line is bigger than $editor_size
472 // This should only really kick in the sqWordWrap function
473 // if the browser doesn't support "HARD" as the wrap type
474 // Or, in Opera's case, something goes wrong.
475 $body = explode("\n", $body);
477 foreach ($body as $line) {
479 $line = rtrim($line);
480 if (strlen($line) <= $editor_size +
1)
481 $newBody .= $line . "\n";
483 sqWordWrap($line, $editor_size) . "\n";
489 do_hook("compose_send");
491 if (! sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id)) {
495 if ( isset($delete_draft)) {
496 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
500 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
502 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
503 displayPageHeader($color, $mailbox);
505 if (isset($AttachFailure))
506 plain_error_message(_("Could not move/copy file. File not attached"), $color);
511 //sqimap_logout($imapConnection);
513 } else if (isset($html_addr_search_done)) {
514 displayPageHeader($color, $mailbox);
516 if (isset($send_to_search) && is_array($send_to_search)) {
517 foreach ($send_to_search as $k => $v) {
518 if (substr($k, 0, 1) == 'T') {
523 elseif (substr($k, 0, 1) == 'C') {
528 elseif (substr($k, 0, 1) == 'B') {
530 $send_to_bcc .= ', ';
537 } else if (isset($html_addr_search)) {
538 if (isset($HTTP_POST_FILES['attachfile']) &&
539 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
540 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
542 if (saveAttachedFiles())
543 plain_error_message(_("Could not move/copy file. File not attached"), $color);
545 // I am using an include so as to elminiate an extra unnecessary click. If you
546 // can think of a better way, please implement it.
547 include_once('./addrbook_search_html.php');
548 } else if (isset($attach)) {
549 if (saveAttachedFiles())
550 plain_error_message(_("Could not move/copy file. File not attached"), $color);
551 displayPageHeader($color, $mailbox);
553 } else if (isset($do_delete)) {
554 displayPageHeader($color, $mailbox);
556 if (isset($delete) && is_array($delete))
558 foreach($delete as $index)
560 unlink ($attachment_dir.$attachments[$index]['localfilename']);
561 unset ($attachments[$index]);
567 // This handles the default case as well as the error case
568 // (they had the same code) --> if (isset($smtpErrors))
569 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
571 displayPageHeader($color, $mailbox);
577 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
580 if (isset($draft_id) && $draft_id && isset($ent_num) && $ent_num)
585 sqimap_logout($imapConnection);
588 function ClearAttachments() {
589 global $attachments, $attachment_dir;
591 foreach ($attachments as $info) {
592 if (file_exists($attachment_dir . $info['localfilename'])) {
593 unlink($attachment_dir . $info['localfilename']);
597 $attachments = array();
600 function getReplyCitation($orig_from) {
601 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
603 /* First, return an empty string when no citation style selected. */
604 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
608 /* Otherwise, try to select the desired citation style. */
609 switch ($reply_citation_style) {
612 $end = ' ' . _("said") . ':';
615 $start = '<' . _("quote") . ' ' . _("who") . '="';
619 $start = $reply_citation_start;
620 $end = $reply_citation_end;
622 default: return ('');
625 /* Build and return the citation string. */
626 return ($start . $orig_from . $end . "\n");