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:
20 /*****************************************************************/
21 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
22 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
23 /*** + Base level indent should begin at left margin, as ***/
24 /*** the require_once below looks. ***/
25 /*** + All identation should consist of four space blocks ***/
26 /*** + Tab characters are evil. ***/
27 /*** + all comments should use "slash-star ... star-slash" ***/
28 /*** style -- no pound characters, no slash-slash style ***/
29 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
30 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
31 /*** + Please use ' instead of ", when possible. Note " ***/
32 /*** should always be used in _( ) function calls. ***/
33 /*** Thank you for your help making the SM code more readable. ***/
34 /*****************************************************************/
36 require_once('../src/validate.php');
37 require_once('../functions/imap.php');
38 require_once('../functions/date.php');
39 require_once('../functions/mime.php');
40 require_once('../functions/smtp.php');
41 require_once('../functions/display_messages.php');
42 require_once('../functions/plugin.php');
44 if (!isset($attachments))
46 $attachments = array();
47 session_register('attachments');
51 // This function is used when not sending or adding attachments
53 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
54 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size,
55 $draft_id, $use_signature;
57 $send_to = decodeHeader($send_to);
58 $send_to_cc = decodeHeader($send_to_cc);
59 $send_to_bcc = decodeHeader($send_to_bcc);
68 $use_signature = FALSE;
72 sqimap_mailbox_select($imapConnection, $mailbox);
73 $message = sqimap_get_message($imapConnection, $id, $mailbox);
74 $orig_header = $message->header
;
76 $message = getEntity($message, $ent_num);
78 if ($message->header
->type0
== 'text' ||
$message->header
->type1
== 'message') {
80 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header
->encoding
);
82 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header
->encoding
);
87 if ($message->header
->type1
== "html")
88 $body = strip_tags($body);
91 $body_ary = explode("\n", $body);
92 $i = count($body_ary) - 1;
93 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
98 for ($i=0; isset($body_ary[$i]); $i++
) {
101 if (ereg('^[ >]+', $body_ary[$i]))
103 $body_ary[$i] = '>' . $body_ary[$i];
107 $body_ary[$i] = '> ' . $body_ary[$i];
111 sqWordWrap($body_ary[$i], $editor_size - 1);
113 $body .= $body_ary[$i] . "\n";
114 unset($body_ary[$i]);
118 $bodyTop = "-------- " . _("Original Message") . " --------\n";
119 $bodyTop .= _("Subject") . ": " . $orig_header->subject
. "\n";
120 $bodyTop .= _("From") . ": " . $orig_header->from
. "\n";
121 $bodyTop .= _("To") . ": " . $orig_header->to
[0] . "\n";
122 if (count($orig_header->to
) > 1) {
123 for ($x=1; $x < count($orig_header->to
); $x++
) {
124 $bodyTop .= " " . $orig_header->to
[$x] . "\n";
128 $body = $bodyTop . $body;
129 } else if ($reply_id) {
130 $orig_from = decodeHeader($orig_header->from
);
131 $orig_from = trim(substr($orig_from,0,strpos($orig_from,'<')));
132 $orig_from = str_replace('"','',$orig_from);
133 $orig_from = str_replace("'",'',$orig_from);
134 $body = getReplyCitation($orig_from) . $body;
141 $send_to = sqimap_find_email($send_to);
144 /** This formats a CC string if they hit "reply all" **/
145 if ($send_to_cc != "") {
146 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
147 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
148 $sendcc = explode(",", $send_to_cc);
151 for ($i = 0; $i < count($sendcc); $i++
) {
152 $sendcc[$i] = trim($sendcc[$i]);
153 if ($sendcc[$i] == "")
156 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
157 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
158 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
160 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
161 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
162 (trim($sendcc[$i]) != "")) {
163 $send_to_cc .= trim($sendcc[$i]) . ", ";
166 $send_to_cc = trim($send_to_cc);
167 if (substr($send_to_cc, -1) == ",") {
168 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
171 } // function newMail()
173 function getAttachments($message) {
174 global $mailbox, $attachments, $attachment_dir, $imapConnection,
175 $ent_num, $forward_id, $draft_id;
177 if (isset($draft_id))
183 sqimap_mailbox_select($imapConnection, $mailbox);
184 $message = sqimap_get_message($imapConnection, $id,
188 if (count($message->entities
) == 0) {
189 if ($message->header
->entity_id
!= $ent_num) {
190 $filename = decodeHeader($message->header
->filename
);
193 $filename = "untitled-".$message->header
->entity_id
;
195 $localfilename = GenerateRandomString(32, '', 7);
196 while (file_exists($attachment_dir . $localfilename))
197 $localfilename = GenerateRandomString(32, '', 7);
199 $newAttachment = array();
200 $newAttachment['localfilename'] = $localfilename;
201 $newAttachment['remotefilename'] = $filename;
202 $newAttachment['type'] = strtolower($message->header
->type0
.
203 '/' . $message->header
->type1
);
205 // Write Attachment to file
206 $fp = fopen ($attachment_dir.$localfilename, 'w');
207 fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
208 $id, $message->header
->entity_id
),
209 $message->header
->encoding
));
212 $attachments[] = $newAttachment;
215 for ($i = 0; $i < count($message->entities
); $i++
) {
216 getAttachments($message->entities
[$i]);
222 function showInputForm () {
223 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
224 $passed_body, $color, $use_signature, $signature, $prefix_sig,
225 $editor_size, $attachments, $subject, $newmail,
226 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
227 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
228 $username, $data_dir, $identity, $draft_id, $delete_draft,
231 $subject = decodeHeader($subject);
232 $reply_subj = decodeHeader($reply_subj);
233 $forward_subj = decodeHeader($forward_subj);
235 if ($use_javascript_addr_book) {
236 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
237 echo "function open_abook() { \n";
238 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
239 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
240 echo " if((!nwin.opener) && (document.windows != null))\n";
241 echo " nwin.opener = document.windows;\n";
243 echo "// --></SCRIPT>\n\n";
246 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
247 do_hook("compose_form");
250 if (isset($draft_id)) {
251 echo "<input type=\"hidden\" name=\"delete_draft\" value=\"$draft_id\">\n";
253 if (isset($delete_draft)) {
254 echo "<input type=\"hidden\" name=\"delete_draft\" value=\"$delete_draft\">\n";
257 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
259 if ($location_of_buttons == 'top') showComposeButtonRow();
261 $idents = getPref($data_dir, $username, 'identities');
262 if ($idents != '' && $idents > 1) {
264 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
266 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
267 echo "<select name=identity>\n";
268 echo "<option value=default>" .
269 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
270 $em = getPref($data_dir, $username, 'email_address');
272 echo htmlspecialchars(' <' . $em . '>') . "\n";
273 for ($i = 1; $i < $idents; $i ++
) {
274 echo '<option value="' . $i . '"';
275 if (isset($identity) && $identity == $i)
278 echo htmlspecialchars(getPref($data_dir, $username, 'full_name' .
280 $em = getPref($data_dir, $username, 'email_address' . $i);
282 echo htmlspecialchars(' <' . $em . '>') . "\n";
289 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
291 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
292 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
293 htmlspecialchars($send_to));
297 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
299 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
300 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
301 htmlspecialchars($send_to_cc));
305 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
307 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
308 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
309 htmlspecialchars($send_to_bcc));
313 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
315 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
317 $reply_subj = str_replace("\"", "'", $reply_subj);
318 $reply_subj = trim($reply_subj);
319 if (substr(strtolower($reply_subj), 0, 3) != "re:")
320 $reply_subj = "Re: $reply_subj";
321 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
322 htmlspecialchars($reply_subj));
323 } else if ($forward_subj) {
324 $forward_subj = trim($forward_subj);
325 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
326 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
327 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:'))
328 $forward_subj = "[Fwd: $forward_subj]";
329 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
330 htmlspecialchars($forward_subj));
332 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
333 htmlspecialchars($subject));
335 echo "</td></tr>\n\n";
337 if ($location_of_buttons == 'between') showComposeButtonRow();
340 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
341 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
342 echo htmlspecialchars($body);
343 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
344 if ( $prefix_sig == true )
345 echo "\n\n-- \n" . htmlspecialchars($signature);
347 echo "\n\n" . htmlspecialchars($signature);
349 echo "</TEXTAREA><BR>\n";
353 if ($location_of_buttons == 'bottom')
354 showComposeButtonRow();
356 echo " <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
359 // This code is for attachments
361 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=MIDDLE ALIGN=RIGHT>\n";
363 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
364 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
365 echo " <input type=\"submit\" name=\"attach\"";
366 echo " value=\"" . _("Add") ."\">\n";
369 if (count($attachments))
371 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
373 echo "</td><td align=left bgcolor=\"$color[0]\">";
374 foreach ($attachments as $key => $info) {
375 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$key\">\n";
376 echo $info['remotefilename'] . " - " . $info['type'] . " (";
377 echo show_readable_size(filesize($attachment_dir .
378 $info['localfilename'])) . ")<br>\n";
381 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
384 // End of attachment code
388 echo "<input type=hidden name=reply_id value=$reply_id>\n";
390 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
392 do_hook("compose_bottom");
393 echo "</BODY></HTML>\n";
396 function showComposeButtonRow() {
397 global $use_javascript_addr_book, $save_as_draft,
398 $default_use_priority, $mailprio;
400 echo " <TR><td>\n </td><td>\n";
401 if ($use_javascript_addr_book) {
402 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
403 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
404 echo " // --></SCRIPT><NOSCRIPT>\n";
405 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
406 echo " </NOSCRIPT>\n";
408 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
410 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
412 if ($save_as_draft) {
413 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
415 if ($default_use_priority) {
416 if(!isset($mailprio)) {
419 echo "\n\t". _("Priority") .":<select name=\"mailprio\">".
420 "\n\t\t<option value=1".($mailprio=="1"?
" selected":"").">". _("High") ."</option>".
421 "\n\t\t<option value=3".($mailprio=="3"?
" selected":"").">". _("Normal") ."</option>".
422 "\n\t\t<option value=5".($mailprio=="5"?
" selected":"").">". _("Low")."</option>".
426 do_hook("compose_button_row");
432 function checkInput ($show) {
433 /** I implemented the $show variable because the error messages
434 were getting sent before the page header. So, I check once
435 using $show=false, and then when i'm ready to display the
436 error message, show=true **/
437 global $body, $send_to, $subject, $color;
439 if ($send_to == "") {
441 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
445 } // function checkInput()
449 function saveAttachedFiles() {
450 global $HTTP_POST_FILES, $attachment_dir, $attachments;
452 $localfilename = GenerateRandomString(32, '', 7);
453 while (file_exists($attachment_dir . $localfilename))
454 $localfilename = GenerateRandomString(32, '', 7);
456 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
457 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
462 $newAttachment['localfilename'] = $localfilename;
463 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
464 $newAttachment['type'] =
465 strtolower($HTTP_POST_FILES['attachfile']['type']);
467 if ($newAttachment['type'] == "")
468 $newAttachment['type'] = 'application/octet-stream';
470 $attachments[] = $newAttachment;
473 if (!isset($mailbox) ||
$mailbox == '' ||
($mailbox == 'None'))
477 require_once ('../src/draft_actions.php');
478 if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id)) {
482 $draft_message = _("Draft Email Saved");
483 /* If this is a resumed draft, then delete the original */
484 if(isset($delete_draft)) {
485 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
488 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort&startMessage=1¬e=$draft_message");
495 if (isset($HTTP_POST_FILES['attachfile']) &&
496 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
497 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
498 $AttachFailure = saveAttachedFiles();
499 if (checkInput(false) && !isset($AttachFailure)) {
500 $urlMailbox = urlencode (trim($mailbox));
501 if (! isset($reply_id))
503 // Set $default_charset to correspond with the user's selection
504 // of language interface.
507 // This is to change all newlines to \n
508 // We'll change them to \r\n later (in the sendMessage function)
509 $body = str_replace("\r\n", "\n", $body);
510 $body = str_replace("\r", "\n", $body);
512 // Rewrap $body so that no line is bigger than $editor_size
513 // This should only really kick in the sqWordWrap function
514 // if the browser doesn't support "HARD" as the wrap type
515 // Or, in Opera's case, something goes wrong.
516 $body = explode("\n", $body);
518 foreach ($body as $line) {
520 $line = rtrim($line);
521 if (strlen($line) <= $editor_size +
1)
522 $newBody .= $line . "\n";
524 sqWordWrap($line, $editor_size) . "\n";
530 do_hook("compose_send");
532 if (! isset($mailprio))
533 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
534 $subject, $body, $reply_id);
536 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
537 $subject, $body, $reply_id, $mailprio);
542 if ( isset($delete_draft)) {
543 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
547 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
549 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
550 displayPageHeader($color, $mailbox);
552 if (isset($AttachFailure))
553 plain_error_message(_("Could not move/copy file. File not attached"), $color);
558 //sqimap_logout($imapConnection);
560 } else if (isset($html_addr_search_done)) {
561 displayPageHeader($color, $mailbox);
563 if (isset($send_to_search) && is_array($send_to_search)) {
564 foreach ($send_to_search as $k => $v) {
565 if (substr($k, 0, 1) == 'T') {
570 elseif (substr($k, 0, 1) == 'C') {
575 elseif (substr($k, 0, 1) == 'B') {
577 $send_to_bcc .= ', ';
584 } else if (isset($html_addr_search)) {
585 if (isset($HTTP_POST_FILES['attachfile']) &&
586 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
587 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
589 if (saveAttachedFiles())
590 plain_error_message(_("Could not move/copy file. File not attached"), $color);
592 // I am using an include so as to elminiate an extra unnecessary click. If you
593 // can think of a better way, please implement it.
594 include_once('./addrbook_search_html.php');
595 } else if (isset($attach)) {
596 if (saveAttachedFiles())
597 plain_error_message(_("Could not move/copy file. File not attached"), $color);
598 displayPageHeader($color, $mailbox);
600 } else if (isset($do_delete)) {
601 displayPageHeader($color, $mailbox);
603 if (isset($delete) && is_array($delete))
605 foreach($delete as $index)
607 unlink ($attachment_dir.$attachments[$index]['localfilename']);
608 unset ($attachments[$index]);
614 // This handles the default case as well as the error case
615 // (they had the same code) --> if (isset($smtpErrors))
616 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
618 displayPageHeader($color, $mailbox);
624 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
627 if (isset($draft_id) && $draft_id && isset($ent_num) && $ent_num)
632 sqimap_logout($imapConnection);
635 function ClearAttachments() {
636 global $attachments, $attachment_dir;
638 foreach ($attachments as $info) {
639 if (file_exists($attachment_dir . $info['localfilename'])) {
640 unlink($attachment_dir . $info['localfilename']);
644 $attachments = array();
647 function getReplyCitation($orig_from) {
648 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
650 /* First, return an empty string when no citation style selected. */
651 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
655 /* Otherwise, try to select the desired citation style. */
656 switch ($reply_citation_style) {
659 $end = ' ' . _("said") . ':';
662 $start = '<' . _("quote") . ' ' . _("who") . '="';
666 $start = $reply_citation_start;
667 $end = $reply_citation_end;
669 default: return ('');
672 /* Build and return the citation string. */
673 return ($start . $orig_from . $end . "\n");