6 * Copyright (c) 1999-2002 The SquirrelMail Project 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)) {
45 $attachments = array();
46 session_register('attachments');
49 // This function is used when not sending or adding attachments
51 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
52 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size,
53 $draft_id, $use_signature;
55 $send_to = decodeHeader($send_to);
56 $send_to_cc = decodeHeader($send_to_cc);
57 $send_to_bcc = decodeHeader($send_to_bcc);
66 $use_signature = FALSE;
70 sqimap_mailbox_select($imapConnection, $mailbox);
71 $message = sqimap_get_message($imapConnection, $id, $mailbox);
72 $orig_header = $message->header
;
74 $message = getEntity($message, $ent_num);
76 if ($message->header
->type0
== 'text' ||
$message->header
->type1
== 'message') {
78 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header
->encoding
);
80 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header
->encoding
);
85 if ($message->header
->type1
== "html")
86 $body = strip_tags($body);
89 $body_ary = explode("\n", $body);
90 $i = count($body_ary) - 1;
91 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
96 for ($i=0; isset($body_ary[$i]); $i++
) {
99 if (ereg('^[ >]+', $body_ary[$i]))
101 $body_ary[$i] = '>' . $body_ary[$i];
105 $body_ary[$i] = '> ' . $body_ary[$i];
109 sqWordWrap($body_ary[$i], $editor_size - 1);
111 $body .= $body_ary[$i] . "\n";
112 unset($body_ary[$i]);
116 $bodyTop = "-------- " . _("Original Message") . " --------\n";
117 $bodyTop .= _("Subject") . ": " . $orig_header->subject
. "\n";
118 $bodyTop .= _("From") . ": " . $orig_header->from
. "\n";
119 $bodyTop .= _("To") . ": " . $orig_header->to
[0] . "\n";
120 if (count($orig_header->to
) > 1) {
121 for ($x=1; $x < count($orig_header->to
); $x++
) {
122 $bodyTop .= " " . $orig_header->to
[$x] . "\n";
126 $body = $bodyTop . $body;
127 } else if ($reply_id) {
128 $orig_from = decodeHeader($orig_header->from
);
129 $body = getReplyCitation($orig_from) . $body;
136 $send_to = sqimap_find_email($send_to);
139 /** This formats a CC string if they hit "reply all" **/
140 if ($send_to_cc != "") {
141 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
142 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
143 $sendcc = explode(",", $send_to_cc);
146 for ($i = 0; $i < count($sendcc); $i++
) {
147 $sendcc[$i] = trim($sendcc[$i]);
148 if ($sendcc[$i] == "")
151 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
152 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
153 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
155 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
156 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
157 (trim($sendcc[$i]) != "")) {
158 $send_to_cc .= trim($sendcc[$i]) . ", ";
161 $send_to_cc = trim($send_to_cc);
162 if (substr($send_to_cc, -1) == ",") {
163 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
166 } // function newMail()
168 function getAttachments($message) {
169 global $mailbox, $attachments, $attachment_dir, $imapConnection,
170 $ent_num, $forward_id, $draft_id, $username;
172 if (isset($draft_id))
178 sqimap_mailbox_select($imapConnection, $mailbox);
179 $message = sqimap_get_message($imapConnection, $id,
183 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
184 if (count($message->entities
) == 0) {
185 if ($message->header
->entity_id
!= $ent_num) {
186 $filename = decodeHeader($message->header
->filename
);
189 $filename = "untitled-".$message->header
->entity_id
;
191 $localfilename = GenerateRandomString(32, '', 7);
192 $full_localfilename = "$hashed_attachment_dir/$localfilename";
193 while (file_exists($full_localfilename)) {
194 $localfilename = GenerateRandomString(32, '', 7);
195 $full_localfilename = "$hashed_attachment_dir/$localfilename";
198 $newAttachment = array();
199 $newAttachment['localfilename'] = $localfilename;
200 $newAttachment['remotefilename'] = $filename;
201 $newAttachment['type'] = strtolower($message->header
->type0
.
202 '/' . $message->header
->type1
);
204 // Write Attachment to file
205 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'w');
206 fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
207 $id, $message->header
->entity_id
),
208 $message->header
->encoding
));
211 $attachments[] = $newAttachment;
214 for ($i = 0; $i < count($message->entities
); $i++
) {
215 getAttachments($message->entities
[$i]);
221 function showInputForm () {
222 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
223 $passed_body, $color, $use_signature, $signature, $prefix_sig,
224 $editor_size, $attachments, $subject, $newmail,
225 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
226 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
227 $username, $data_dir, $identity, $draft_id, $delete_draft,
230 $subject = decodeHeader($subject);
231 $reply_subj = decodeHeader($reply_subj);
232 $forward_subj = decodeHeader($forward_subj);
234 if ($use_javascript_addr_book) {
235 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
236 echo "function open_abook() { \n";
237 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
238 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
239 echo " if((!nwin.opener) && (document.windows != null))\n";
240 echo " nwin.opener = document.windows;\n";
242 echo "// --></SCRIPT>\n\n";
245 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
246 do_hook("compose_form");
249 if (isset($draft_id)) {
250 echo "<input type=\"hidden\" name=\"delete_draft\" value=\"$draft_id\">\n";
252 if (isset($delete_draft)) {
253 echo "<input type=\"hidden\" name=\"delete_draft\" value=\"$delete_draft\">\n";
256 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
258 if ($location_of_buttons == 'top') showComposeButtonRow();
260 $idents = getPref($data_dir, $username, 'identities');
261 if ($idents != '' && $idents > 1) {
263 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
265 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
266 echo "<select name=identity>\n";
267 echo "<option value=default>" .
268 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
269 $em = getPref($data_dir, $username, 'email_address');
271 echo htmlspecialchars(' <' . $em . '>') . "\n";
272 for ($i = 1; $i < $idents; $i ++
) {
273 echo '<option value="' . $i . '"';
274 if (isset($identity) && $identity == $i)
277 echo htmlspecialchars(getPref($data_dir, $username, 'full_name' .
279 $em = getPref($data_dir, $username, 'email_address' . $i);
281 echo htmlspecialchars(' <' . $em . '>') . "\n";
288 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
290 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
291 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
292 htmlspecialchars($send_to));
296 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
298 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
299 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
300 htmlspecialchars($send_to_cc));
304 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
306 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
307 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
308 htmlspecialchars($send_to_bcc));
312 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
314 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
316 $reply_subj = str_replace("\"", "'", $reply_subj);
317 $reply_subj = trim($reply_subj);
318 if (substr(strtolower($reply_subj), 0, 3) != "re:")
319 $reply_subj = "Re: $reply_subj";
320 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
321 htmlspecialchars($reply_subj));
322 } else if ($forward_subj) {
323 $forward_subj = trim($forward_subj);
324 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
325 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
326 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:'))
327 $forward_subj = "[Fwd: $forward_subj]";
328 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
329 htmlspecialchars($forward_subj));
331 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
332 htmlspecialchars($subject));
334 echo "</td></tr>\n\n";
336 if ($location_of_buttons == 'between') showComposeButtonRow();
339 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
340 echo " <TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
341 echo htmlspecialchars($body);
342 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
343 if ( $prefix_sig == true )
344 echo "\n\n-- \n" . htmlspecialchars($signature);
346 echo "\n\n" . htmlspecialchars($signature);
348 echo "</TEXTAREA><BR>\n";
352 if ($location_of_buttons == 'bottom')
353 showComposeButtonRow();
355 echo " <TR><TD> </TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
358 // This code is for attachments
360 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=MIDDLE ALIGN=RIGHT>\n";
362 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
363 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
364 echo " <input type=\"submit\" name=\"attach\"";
365 echo " value=\"" . _("Add") ."\">\n";
368 if (count($attachments)) {
369 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
370 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
372 echo "</td><td align=left bgcolor=\"$color[0]\">";
373 foreach ($attachments as $key => $info) {
374 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
375 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$key\">\n";
376 echo $info['remotefilename'] . " - " . $info['type'] . " (";
377 echo show_readable_size(filesize($attached_file)) . ")<br>\n";
380 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
383 // End of attachment code
387 echo "<input type=hidden name=reply_id value=$reply_id>\n";
389 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
391 do_hook("compose_bottom");
392 echo "</BODY></HTML>\n";
395 function showComposeButtonRow() {
396 global $use_javascript_addr_book, $save_as_draft,
397 $default_use_priority, $mailprio;
399 echo " <TR><td>\n </td><td>\n";
400 if ($use_javascript_addr_book) {
401 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
402 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
403 echo " // --></SCRIPT><NOSCRIPT>\n";
404 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
405 echo " </NOSCRIPT>\n";
407 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
409 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
411 if ($save_as_draft) {
412 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
414 if ($default_use_priority) {
415 if(!isset($mailprio)) {
418 echo "\n\t". _("Priority") .":<select name=\"mailprio\">".
419 "\n\t\t<option value=1".($mailprio=="1"?
" selected":"").">". _("High") ."</option>".
420 "\n\t\t<option value=3".($mailprio=="3"?
" selected":"").">". _("Normal") ."</option>".
421 "\n\t\t<option value=5".($mailprio=="5"?
" selected":"").">". _("Low")."</option>".
425 do_hook("compose_button_row");
431 function checkInput ($show) {
432 /** I implemented the $show variable because the error messages
433 were getting sent before the page header. So, I check once
434 using $show=false, and then when i'm ready to display the
435 error message, show=true **/
436 global $body, $send_to, $subject, $color;
438 if ($send_to == "") {
440 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
444 } // function checkInput()
448 function saveAttachedFiles() {
449 global $HTTP_POST_FILES, $attachment_dir, $attachments, $username;
451 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
452 $localfilename = GenerateRandomString(32, '', 7);
453 $full_localfilename = "$hashed_attachment_dir/$localfilename";
454 while (file_exists($full_localfilename)) {
455 $localfilename = GenerateRandomString(32, '', 7);
456 $full_localfilename = "$hashed_attachment_dir/$localfilename";
459 if (!@rename
($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
460 if (!@copy
($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
465 $newAttachment['localfilename'] = $localfilename;
466 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
467 $newAttachment['type'] =
468 strtolower($HTTP_POST_FILES['attachfile']['type']);
470 if ($newAttachment['type'] == "")
471 $newAttachment['type'] = 'application/octet-stream';
473 $attachments[] = $newAttachment;
476 if (!isset($mailbox) ||
$mailbox == '' ||
($mailbox == 'None'))
480 require_once ('../src/draft_actions.php');
481 if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id)) {
485 $draft_message = _("Draft Email Saved");
486 /* If this is a resumed draft, then delete the original */
487 if(isset($delete_draft)) {
488 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
491 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort&startMessage=1¬e=$draft_message");
498 if (isset($HTTP_POST_FILES['attachfile']) &&
499 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
500 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
501 $AttachFailure = saveAttachedFiles();
502 if (checkInput(false) && !isset($AttachFailure)) {
503 $urlMailbox = urlencode (trim($mailbox));
504 if (! isset($reply_id))
506 // Set $default_charset to correspond with the user's selection
507 // of language interface.
510 // This is to change all newlines to \n
511 // We'll change them to \r\n later (in the sendMessage function)
512 $body = str_replace("\r\n", "\n", $body);
513 $body = str_replace("\r", "\n", $body);
515 // Rewrap $body so that no line is bigger than $editor_size
516 // This should only really kick in the sqWordWrap function
517 // if the browser doesn't support "HARD" as the wrap type
518 // Or, in Opera's case, something goes wrong.
519 $body = explode("\n", $body);
521 foreach ($body as $line) {
523 $line = rtrim($line);
524 if (strlen($line) <= $editor_size +
1)
525 $newBody .= $line . "\n";
527 sqWordWrap($line, $editor_size) . "\n";
533 do_hook("compose_send");
535 if (! isset($mailprio))
536 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
537 $subject, $body, $reply_id);
539 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
540 $subject, $body, $reply_id, $mailprio);
545 if ( isset($delete_draft)) {
546 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
550 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
552 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
553 displayPageHeader($color, $mailbox);
555 if (isset($AttachFailure))
556 plain_error_message(_("Could not move/copy file. File not attached"), $color);
561 //sqimap_logout($imapConnection);
563 } else if (isset($html_addr_search_done)) {
564 displayPageHeader($color, $mailbox);
566 if (isset($send_to_search) && is_array($send_to_search)) {
567 foreach ($send_to_search as $k => $v) {
568 if (substr($k, 0, 1) == 'T') {
573 elseif (substr($k, 0, 1) == 'C') {
578 elseif (substr($k, 0, 1) == 'B') {
580 $send_to_bcc .= ', ';
587 } else if (isset($html_addr_search)) {
588 if (isset($HTTP_POST_FILES['attachfile']) &&
589 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
590 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
592 if (saveAttachedFiles())
593 plain_error_message(_("Could not move/copy file. File not attached"), $color);
595 // I am using an include so as to elminiate an extra unnecessary click. If you
596 // can think of a better way, please implement it.
597 include_once('./addrbook_search_html.php');
598 } else if (isset($attach)) {
599 if (saveAttachedFiles())
600 plain_error_message(_("Could not move/copy file. File not attached"), $color);
601 displayPageHeader($color, $mailbox);
603 } else if (isset($do_delete)) {
604 displayPageHeader($color, $mailbox);
606 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
607 if (isset($delete) && is_array($delete)) {
608 foreach($delete as $index) {
609 $attached_file = $hashed_attachment_dir . '/'
610 . $attachments[$index]['localfilename'];
611 unlink ($attached_file);
612 unset ($attachments[$index]);
618 // This handles the default case as well as the error case
619 // (they had the same code) --> if (isset($smtpErrors))
620 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
622 displayPageHeader($color, $mailbox);
628 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
631 if (isset($draft_id) && $draft_id && isset($ent_num) && $ent_num)
636 sqimap_logout($imapConnection);
639 function ClearAttachments() {
640 global $username, $attachments, $attachment_dir;
641 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
643 foreach ($attachments as $info) {
644 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
645 if (file_exists($attached_file)) {
646 unlink($attached_file);
650 $attachments = array();
653 function getReplyCitation($orig_from) {
654 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
656 /* First, return an empty string when no citation style selected. */
657 if (($reply_citation_style == '') ||
($reply_citation_style == 'none')) {
661 /* Decode the users name. */
662 $parpos = strpos($orig_from, '(');
663 if ($parpos === false) {
664 $orig_from = trim(substr($orig_from, 0, strpos($orig_from, '<')));
665 $orig_from = str_replace('"', '', $orig_from);
666 $orig_from = str_replace("'", '', $orig_from);
668 $end_parpos = strrpos($orig_from, ')');
669 $end_parpos -= ($end_parpos === false ?
$end_parpos : $parpos +
1);
670 $orig_from = trim(substr($orig_from, $parpos +
1, $end_parpos));
673 /* Make sure our final value isn't an empty string. */
674 if ($orig_from == '') {
678 /* Otherwise, try to select the desired citation style. */
679 switch ($reply_citation_style) {
682 $end = ' ' . _("said") . ':';
685 $start = '<' . _("quote") . ' ' . _("who") . '="';
689 $start = $reply_citation_start;
690 $end = $reply_citation_end;
692 default: return ('');
695 /* Build and return the citation string. */
696 return ($start . $orig_from . $end . "\n");