6 ** Copyright (c) 1999-2001 The SquirrelMail development team
7 ** Licensed under the GNU GPL. For full terms see the file COPYING.
9 ** This code sends a mail.
11 ** There are 4 modes of operation:
13 ** - Add an attachment
20 require_once('../src/validate.php');
21 require_once('../functions/imap.php');
22 require_once('../functions/date.php');
23 require_once('../functions/mime.php');
24 require_once('../functions/smtp.php');
25 require_once('../functions/display_messages.php');
26 require_once('../functions/plugin.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,
39 $draft_id, $use_signature;
41 $send_to = decodeHeader($send_to);
42 $send_to_cc = decodeHeader($send_to_cc);
43 $send_to_bcc = decodeHeader($send_to_bcc);
52 $use_signature = FALSE;
56 sqimap_mailbox_select($imapConnection, $mailbox);
57 $message = sqimap_get_message($imapConnection, $id, $mailbox);
58 $orig_header = $message->header
;
60 $message = getEntity($message, $ent_num);
62 if ($message->header
->type0
== 'text' ||
$message->header
->type1
== 'message') {
64 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header
->encoding
);
66 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header
->encoding
);
71 if ($message->header
->type1
== "html")
72 $body = strip_tags($body);
75 $body_ary = explode("\n", $body);
76 $i = count($body_ary) - 1;
77 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
82 for ($i=0; isset($body_ary[$i]); $i++
) {
85 if (ereg('^[ >]+', $body_ary[$i]))
87 $body_ary[$i] = '>' . $body_ary[$i];
91 $body_ary[$i] = '> ' . $body_ary[$i];
95 sqWordWrap($body_ary[$i], $editor_size - 1);
97 $body .= $body_ary[$i] . "\n";
102 $bodyTop = "-------- " . _("Original Message") . " --------\n";
103 $bodyTop .= _("Subject") . ": " . $orig_header->subject
. "\n";
104 $bodyTop .= _("From") . ": " . $orig_header->from
. "\n";
105 $bodyTop .= _("To") . ": " . $orig_header->to
[0] . "\n";
106 if (count($orig_header->to
) > 1) {
107 for ($x=1; $x < count($orig_header->to
); $x++
) {
108 $bodyTop .= " " . $orig_header->to
[$x] . "\n";
112 $body = $bodyTop . $body;
113 } else if ($reply_id) {
114 $orig_from = decodeHeader($orig_header->from
);
115 $orig_from = trim(substr($orig_from,0,strpos($orig_from,'<')));
116 $orig_from = str_replace('"','',$orig_from);
117 $orig_from = str_replace("'",'',$orig_from);
118 $body = getReplyCitation($orig_from) . $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, $draft_id;
161 if (isset($draft_id))
167 sqimap_mailbox_select($imapConnection, $mailbox);
168 $message = sqimap_get_message($imapConnection, $id,
172 if (count($message->entities
) == 0) {
173 if ($message->header
->entity_id
!= $ent_num) {
174 $filename = decodeHeader($message->header
->filename
);
177 $filename = "untitled-".$message->header
->entity_id
;
179 $localfilename = GenerateRandomString(32, '', 7);
180 while (file_exists($attachment_dir . $localfilename))
181 $localfilename = GenerateRandomString(32, '', 7);
183 $newAttachment = array();
184 $newAttachment['localfilename'] = $localfilename;
185 $newAttachment['remotefilename'] = $filename;
186 $newAttachment['type'] = strtolower($message->header
->type0
.
187 '/' . $message->header
->type1
);
189 // Write Attachment to file
190 $fp = fopen ($attachment_dir.$localfilename, 'w');
191 fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
192 $id, $message->header
->entity_id
),
193 $message->header
->encoding
));
196 $attachments[] = $newAttachment;
199 for ($i = 0; $i < count($message->entities
); $i++
) {
200 getAttachments($message->entities
[$i]);
206 function showInputForm () {
207 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
208 $passed_body, $color, $use_signature, $signature, $prefix_sig,
209 $editor_size, $attachments, $subject, $newmail,
210 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
211 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
212 $username, $data_dir, $identity, $draft_id, $delete_draft,
215 $subject = decodeHeader($subject);
216 $reply_subj = decodeHeader($reply_subj);
217 $forward_subj = decodeHeader($forward_subj);
219 if ($use_javascript_addr_book) {
220 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
221 echo "function open_abook() { \n";
222 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
223 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
224 echo " if((!nwin.opener) && (document.windows != null))\n";
225 echo " nwin.opener = document.windows;\n";
227 echo "// --></SCRIPT>\n\n";
230 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
231 do_hook("compose_form");
234 if (isset($draft_id)) {
235 echo "<input type=\"hidden\" name=\"delete_draft\" value=\"$draft_id\">\n";
237 if (isset($delete_draft)) {
238 echo "<input type=\"hidden\" name=\"delete_draft\" value=\"$delete_draft\">\n";
241 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
243 if ($location_of_buttons == 'top') showComposeButtonRow();
245 $idents = getPref($data_dir, $username, 'identities');
246 if ($idents != '' && $idents > 1) {
248 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
250 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
251 echo "<select name=identity>\n";
252 echo "<option value=default>" .
253 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
254 $em = getPref($data_dir, $username, 'email_address');
256 echo htmlspecialchars(' <' . $em . '>') . "\n";
257 for ($i = 1; $i < $idents; $i ++
) {
258 echo '<option value="' . $i . '"';
259 if (isset($identity) && $identity == $i)
262 echo htmlspecialchars(getPref($data_dir, $username, 'full_name' .
264 $em = getPref($data_dir, $username, 'email_address' . $i);
266 echo htmlspecialchars(' <' . $em . '>') . "\n";
273 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
275 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
276 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
277 htmlspecialchars($send_to));
281 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
283 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
284 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
285 htmlspecialchars($send_to_cc));
289 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
291 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
292 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
293 htmlspecialchars($send_to_bcc));
297 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
299 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
301 $reply_subj = str_replace("\"", "'", $reply_subj);
302 $reply_subj = trim($reply_subj);
303 if (substr(strtolower($reply_subj), 0, 3) != "re:")
304 $reply_subj = "Re: $reply_subj";
305 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
306 htmlspecialchars($reply_subj));
307 } else if ($forward_subj) {
308 $forward_subj = trim($forward_subj);
309 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
310 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
311 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:'))
312 $forward_subj = "[Fwd: $forward_subj]";
313 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
314 htmlspecialchars($forward_subj));
316 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
317 htmlspecialchars($subject));
319 echo "</td></tr>\n\n";
321 if ($location_of_buttons == 'between') showComposeButtonRow();
324 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
325 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
326 echo htmlspecialchars($body);
327 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
328 if ( $prefix_sig == true )
329 echo "\n\n-- \n" . htmlspecialchars($signature);
331 echo "\n\n" . htmlspecialchars($signature);
333 echo "</TEXTAREA><BR>\n";
337 if ($location_of_buttons == 'bottom')
338 showComposeButtonRow();
340 echo " <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
343 // This code is for attachments
345 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=MIDDLE ALIGN=RIGHT>\n";
347 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
348 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
349 echo " <input type=\"submit\" name=\"attach\"";
350 echo " value=\"" . _("Add") ."\">\n";
353 if (count($attachments))
355 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
357 echo "</td><td align=left bgcolor=\"$color[0]\">";
358 foreach ($attachments as $key => $info) {
359 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$key\">\n";
360 echo $info['remotefilename'] . " - " . $info['type'] . " (";
361 echo show_readable_size(filesize($attachment_dir .
362 $info['localfilename'])) . ")<br>\n";
365 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
368 // End of attachment code
372 echo "<input type=hidden name=reply_id value=$reply_id>\n";
374 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
376 do_hook("compose_bottom");
377 echo "</BODY></HTML>\n";
380 function showComposeButtonRow() {
381 global $use_javascript_addr_book, $save_as_draft,
382 $default_use_priority, $mailprio;
384 echo " <TR><td>\n </td><td>\n";
385 if ($use_javascript_addr_book) {
386 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
387 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
388 echo " // --></SCRIPT><NOSCRIPT>\n";
389 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
390 echo " </NOSCRIPT>\n";
392 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
394 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
396 if ($save_as_draft) {
397 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
399 if ($default_use_priority) {
400 if(!isset($mailprio)) {
403 echo "\n\t". _("Priority") .":<select name=\"mailprio\">".
404 "\n\t\t<option value=1".($mailprio=="1"?
" selected":"").">". _("High") ."</option>".
405 "\n\t\t<option value=3".($mailprio=="3"?
" selected":"").">". _("Normal") ."</option>".
406 "\n\t\t<option value=5".($mailprio=="5"?
" selected":"").">". _("Low")."</option>".
410 do_hook("compose_button_row");
416 function checkInput ($show) {
417 /** I implemented the $show variable because the error messages
418 were getting sent before the page header. So, I check once
419 using $show=false, and then when i'm ready to display the
420 error message, show=true **/
421 global $body, $send_to, $subject, $color;
423 if ($send_to == "") {
425 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
429 } // function checkInput()
433 function saveAttachedFiles() {
434 global $HTTP_POST_FILES, $attachment_dir, $attachments;
436 $localfilename = GenerateRandomString(32, '', 7);
437 while (file_exists($attachment_dir . $localfilename))
438 $localfilename = GenerateRandomString(32, '', 7);
440 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
441 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
446 $newAttachment['localfilename'] = $localfilename;
447 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
448 $newAttachment['type'] =
449 strtolower($HTTP_POST_FILES['attachfile']['type']);
451 if ($newAttachment['type'] == "")
452 $newAttachment['type'] = 'application/octet-stream';
454 $attachments[] = $newAttachment;
457 if (!isset($mailbox) ||
$mailbox == '' ||
($mailbox == 'None'))
461 require_once ('../src/draft_actions.php');
462 if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id)) {
466 $draft_message = _("Draft Email Saved");
467 /* If this is a resumed draft, then delete the original */
468 if(isset($delete_draft)) {
469 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
472 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort&startMessage=1¬e=$draft_message");
479 if (isset($HTTP_POST_FILES['attachfile']) &&
480 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
481 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
482 $AttachFailure = saveAttachedFiles();
483 if (checkInput(false) && !isset($AttachFailure)) {
484 $urlMailbox = urlencode (trim($mailbox));
485 if (! isset($reply_id))
487 // Set $default_charset to correspond with the user's selection
488 // of language interface.
491 // This is to change all newlines to \n
492 // We'll change them to \r\n later (in the sendMessage function)
493 $body = str_replace("\r\n", "\n", $body);
494 $body = str_replace("\r", "\n", $body);
496 // Rewrap $body so that no line is bigger than $editor_size
497 // This should only really kick in the sqWordWrap function
498 // if the browser doesn't support "HARD" as the wrap type
499 // Or, in Opera's case, something goes wrong.
500 $body = explode("\n", $body);
502 foreach ($body as $line) {
504 $line = rtrim($line);
505 if (strlen($line) <= $editor_size +
1)
506 $newBody .= $line . "\n";
508 sqWordWrap($line, $editor_size) . "\n";
514 do_hook("compose_send");
516 if (! isset($mailprio))
517 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
518 $subject, $body, $reply_id);
520 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
521 $subject, $body, $reply_id, $mailprio);
526 if ( isset($delete_draft)) {
527 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
531 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
533 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
534 displayPageHeader($color, $mailbox);
536 if (isset($AttachFailure))
537 plain_error_message(_("Could not move/copy file. File not attached"), $color);
542 //sqimap_logout($imapConnection);
544 } else if (isset($html_addr_search_done)) {
545 displayPageHeader($color, $mailbox);
547 if (isset($send_to_search) && is_array($send_to_search)) {
548 foreach ($send_to_search as $k => $v) {
549 if (substr($k, 0, 1) == 'T') {
554 elseif (substr($k, 0, 1) == 'C') {
559 elseif (substr($k, 0, 1) == 'B') {
561 $send_to_bcc .= ', ';
568 } else if (isset($html_addr_search)) {
569 if (isset($HTTP_POST_FILES['attachfile']) &&
570 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
571 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
573 if (saveAttachedFiles())
574 plain_error_message(_("Could not move/copy file. File not attached"), $color);
576 // I am using an include so as to elminiate an extra unnecessary click. If you
577 // can think of a better way, please implement it.
578 include_once('./addrbook_search_html.php');
579 } else if (isset($attach)) {
580 if (saveAttachedFiles())
581 plain_error_message(_("Could not move/copy file. File not attached"), $color);
582 displayPageHeader($color, $mailbox);
584 } else if (isset($do_delete)) {
585 displayPageHeader($color, $mailbox);
587 if (isset($delete) && is_array($delete))
589 foreach($delete as $index)
591 unlink ($attachment_dir.$attachments[$index]['localfilename']);
592 unset ($attachments[$index]);
598 // This handles the default case as well as the error case
599 // (they had the same code) --> if (isset($smtpErrors))
600 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
602 displayPageHeader($color, $mailbox);
608 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
611 if (isset($draft_id) && $draft_id && isset($ent_num) && $ent_num)
616 sqimap_logout($imapConnection);
619 function ClearAttachments() {
620 global $attachments, $attachment_dir;
622 foreach ($attachments as $info) {
623 if (file_exists($attachment_dir . $info['localfilename'])) {
624 unlink($attachment_dir . $info['localfilename']);
628 $attachments = array();
631 function getReplyCitation($orig_from) {
632 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
634 /* First, return an empty string when no citation style selected. */
635 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
639 /* Otherwise, try to select the desired citation style. */
640 switch ($reply_citation_style) {
643 $end = ' ' . _("said") . ':';
646 $start = '<' . _("quote") . ' ' . _("who") . '="';
650 $start = $reply_citation_start;
651 $end = $reply_citation_end;
653 default: return ('');
656 /* Build and return the citation string. */
657 return ($start . $orig_from . $end . "\n");