27dee5d99b45098eeaf5b7a5ecad6d03914b55ad
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, $username;
177 if (isset($draft_id))
183 sqimap_mailbox_select($imapConnection, $mailbox);
184 $message = sqimap_get_message($imapConnection, $id,
188 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
189 if (count($message->entities
) == 0) {
190 if ($message->header
->entity_id
!= $ent_num) {
191 $filename = decodeHeader($message->header
->filename
);
194 $filename = "untitled-".$message->header
->entity_id
;
196 $localfilename = GenerateRandomString(32, '', 7);
197 $full_localfilename = "$hashed_attachment_dir/$localfilename";
198 while (file_exists($full_localfilename)) {
199 $localfilename = GenerateRandomString(32, '', 7);
200 $full_localfilename = "$hashed_attachment_dir/$localfilename";
203 $newAttachment = array();
204 $newAttachment['localfilename'] = $localfilename;
205 $newAttachment['remotefilename'] = $filename;
206 $newAttachment['type'] = strtolower($message->header
->type0
.
207 '/' . $message->header
->type1
);
209 // Write Attachment to file
210 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'w');
211 fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
212 $id, $message->header
->entity_id
),
213 $message->header
->encoding
));
216 $attachments[] = $newAttachment;
219 for ($i = 0; $i < count($message->entities
); $i++
) {
220 getAttachments($message->entities
[$i]);
226 function showInputForm () {
227 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
228 $passed_body, $color, $use_signature, $signature, $prefix_sig,
229 $editor_size, $attachments, $subject, $newmail,
230 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
231 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
232 $username, $data_dir, $identity, $draft_id, $delete_draft,
235 $subject = decodeHeader($subject);
236 $reply_subj = decodeHeader($reply_subj);
237 $forward_subj = decodeHeader($forward_subj);
239 if ($use_javascript_addr_book) {
240 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
241 echo "function open_abook() { \n";
242 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
243 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
244 echo " if((!nwin.opener) && (document.windows != null))\n";
245 echo " nwin.opener = document.windows;\n";
247 echo "// --></SCRIPT>\n\n";
250 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
251 do_hook("compose_form");
254 if (isset($draft_id)) {
255 echo "<input type=\"hidden\" name=\"delete_draft\" value=\"$draft_id\">\n";
257 if (isset($delete_draft)) {
258 echo "<input type=\"hidden\" name=\"delete_draft\" value=\"$delete_draft\">\n";
261 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
263 if ($location_of_buttons == 'top') showComposeButtonRow();
265 $idents = getPref($data_dir, $username, 'identities');
266 if ($idents != '' && $idents > 1) {
268 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
270 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
271 echo "<select name=identity>\n";
272 echo "<option value=default>" .
273 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
274 $em = getPref($data_dir, $username, 'email_address');
276 echo htmlspecialchars(' <' . $em . '>') . "\n";
277 for ($i = 1; $i < $idents; $i ++
) {
278 echo '<option value="' . $i . '"';
279 if (isset($identity) && $identity == $i)
282 echo htmlspecialchars(getPref($data_dir, $username, 'full_name' .
284 $em = getPref($data_dir, $username, 'email_address' . $i);
286 echo htmlspecialchars(' <' . $em . '>') . "\n";
293 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
295 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
296 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
297 htmlspecialchars($send_to));
301 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
303 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
304 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
305 htmlspecialchars($send_to_cc));
309 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
311 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
312 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
313 htmlspecialchars($send_to_bcc));
317 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
319 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
321 $reply_subj = str_replace("\"", "'", $reply_subj);
322 $reply_subj = trim($reply_subj);
323 if (substr(strtolower($reply_subj), 0, 3) != "re:")
324 $reply_subj = "Re: $reply_subj";
325 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
326 htmlspecialchars($reply_subj));
327 } else if ($forward_subj) {
328 $forward_subj = trim($forward_subj);
329 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
330 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
331 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:'))
332 $forward_subj = "[Fwd: $forward_subj]";
333 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
334 htmlspecialchars($forward_subj));
336 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
337 htmlspecialchars($subject));
339 echo "</td></tr>\n\n";
341 if ($location_of_buttons == 'between') showComposeButtonRow();
344 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
345 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
346 echo htmlspecialchars($body);
347 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
348 if ( $prefix_sig == true )
349 echo "\n\n-- \n" . htmlspecialchars($signature);
351 echo "\n\n" . htmlspecialchars($signature);
353 echo "</TEXTAREA><BR>\n";
357 if ($location_of_buttons == 'bottom')
358 showComposeButtonRow();
360 echo " <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
363 // This code is for attachments
365 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=MIDDLE ALIGN=RIGHT>\n";
367 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
368 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
369 echo " <input type=\"submit\" name=\"attach\"";
370 echo " value=\"" . _("Add") ."\">\n";
373 if (count($attachments)) {
374 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
375 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
377 echo "</td><td align=left bgcolor=\"$color[0]\">";
378 foreach ($attachments as $key => $info) {
379 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
380 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$key\">\n";
381 echo $info['remotefilename'] . " - " . $info['type'] . " (";
382 echo show_readable_size(filesize($attached_file)) . ")<br>\n";
385 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
388 // End of attachment code
392 echo "<input type=hidden name=reply_id value=$reply_id>\n";
394 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
396 do_hook("compose_bottom");
397 echo "</BODY></HTML>\n";
400 function showComposeButtonRow() {
401 global $use_javascript_addr_book, $save_as_draft,
402 $default_use_priority, $mailprio;
404 echo " <TR><td>\n </td><td>\n";
405 if ($use_javascript_addr_book) {
406 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
407 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
408 echo " // --></SCRIPT><NOSCRIPT>\n";
409 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
410 echo " </NOSCRIPT>\n";
412 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
414 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
416 if ($save_as_draft) {
417 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
419 if ($default_use_priority) {
420 if(!isset($mailprio)) {
423 echo "\n\t". _("Priority") .":<select name=\"mailprio\">".
424 "\n\t\t<option value=1".($mailprio=="1"?
" selected":"").">". _("High") ."</option>".
425 "\n\t\t<option value=3".($mailprio=="3"?
" selected":"").">". _("Normal") ."</option>".
426 "\n\t\t<option value=5".($mailprio=="5"?
" selected":"").">". _("Low")."</option>".
430 do_hook("compose_button_row");
436 function checkInput ($show) {
437 /** I implemented the $show variable because the error messages
438 were getting sent before the page header. So, I check once
439 using $show=false, and then when i'm ready to display the
440 error message, show=true **/
441 global $body, $send_to, $subject, $color;
443 if ($send_to == "") {
445 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
449 } // function checkInput()
453 function saveAttachedFiles() {
454 global $HTTP_POST_FILES, $attachment_dir, $attachments, $username;
456 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
457 $localfilename = GenerateRandomString(32, '', 7);
458 $full_localfilename = "$hashed_attachment_dir/$localfilename";
459 while (file_exists($full_localfilename)) {
460 $localfilename = GenerateRandomString(32, '', 7);
461 $full_localfilename = "$hashed_attachment_dir/$localfilename";
464 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
465 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
470 $newAttachment['localfilename'] = $localfilename;
471 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
472 $newAttachment['type'] =
473 strtolower($HTTP_POST_FILES['attachfile']['type']);
475 if ($newAttachment['type'] == "")
476 $newAttachment['type'] = 'application/octet-stream';
478 $attachments[] = $newAttachment;
481 if (!isset($mailbox) ||
$mailbox == '' ||
($mailbox == 'None'))
485 require_once ('../src/draft_actions.php');
486 if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id)) {
490 $draft_message = _("Draft Email Saved");
491 /* If this is a resumed draft, then delete the original */
492 if(isset($delete_draft)) {
493 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
496 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort&startMessage=1¬e=$draft_message");
503 if (isset($HTTP_POST_FILES['attachfile']) &&
504 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
505 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
506 $AttachFailure = saveAttachedFiles();
507 if (checkInput(false) && !isset($AttachFailure)) {
508 $urlMailbox = urlencode (trim($mailbox));
509 if (! isset($reply_id))
511 // Set $default_charset to correspond with the user's selection
512 // of language interface.
515 // This is to change all newlines to \n
516 // We'll change them to \r\n later (in the sendMessage function)
517 $body = str_replace("\r\n", "\n", $body);
518 $body = str_replace("\r", "\n", $body);
520 // Rewrap $body so that no line is bigger than $editor_size
521 // This should only really kick in the sqWordWrap function
522 // if the browser doesn't support "HARD" as the wrap type
523 // Or, in Opera's case, something goes wrong.
524 $body = explode("\n", $body);
526 foreach ($body as $line) {
528 $line = rtrim($line);
529 if (strlen($line) <= $editor_size +
1)
530 $newBody .= $line . "\n";
532 sqWordWrap($line, $editor_size) . "\n";
538 do_hook("compose_send");
540 if (! isset($mailprio))
541 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
542 $subject, $body, $reply_id);
544 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
545 $subject, $body, $reply_id, $mailprio);
550 if ( isset($delete_draft)) {
551 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
555 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
557 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
558 displayPageHeader($color, $mailbox);
560 if (isset($AttachFailure))
561 plain_error_message(_("Could not move/copy file. File not attached"), $color);
566 //sqimap_logout($imapConnection);
568 } else if (isset($html_addr_search_done)) {
569 displayPageHeader($color, $mailbox);
571 if (isset($send_to_search) && is_array($send_to_search)) {
572 foreach ($send_to_search as $k => $v) {
573 if (substr($k, 0, 1) == 'T') {
578 elseif (substr($k, 0, 1) == 'C') {
583 elseif (substr($k, 0, 1) == 'B') {
585 $send_to_bcc .= ', ';
592 } else if (isset($html_addr_search)) {
593 if (isset($HTTP_POST_FILES['attachfile']) &&
594 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
595 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
597 if (saveAttachedFiles())
598 plain_error_message(_("Could not move/copy file. File not attached"), $color);
600 // I am using an include so as to elminiate an extra unnecessary click. If you
601 // can think of a better way, please implement it.
602 include_once('./addrbook_search_html.php');
603 } else if (isset($attach)) {
604 if (saveAttachedFiles())
605 plain_error_message(_("Could not move/copy file. File not attached"), $color);
606 displayPageHeader($color, $mailbox);
608 } else if (isset($do_delete)) {
609 displayPageHeader($color, $mailbox);
611 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
612 if (isset($delete) && is_array($delete)) {
613 foreach($delete as $index) {
614 $attached_file = $hashed_attachment_dir . '/'
615 . $attachments[$index]['localfilename'];
616 unlink ($attached_file);
617 unset ($attachments[$index]);
623 // This handles the default case as well as the error case
624 // (they had the same code) --> if (isset($smtpErrors))
625 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
627 displayPageHeader($color, $mailbox);
633 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
636 if (isset($draft_id) && $draft_id && isset($ent_num) && $ent_num)
641 sqimap_logout($imapConnection);
644 function ClearAttachments() {
645 global $username, $attachments, $attachment_dir;
646 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
648 foreach ($attachments as $info) {
649 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
650 if (file_exists($attached_file)) {
651 unlink($attached_file);
655 $attachments = array();
658 function getReplyCitation($orig_from) {
659 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
661 /* First, return an empty string when no citation style selected. */
662 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
666 /* Otherwise, try to select the desired citation style. */
667 switch ($reply_citation_style) {
670 $end = ' ' . _("said") . ':';
673 $start = '<' . _("quote") . ' ' . _("who") . '="';
677 $start = $reply_citation_start;
678 $end = $reply_citation_end;
680 default: return ('');
683 /* Build and return the citation string. */
684 return ($start . $orig_from . $end . "\n");