updated some documentation, fixed minor bugs in MIME decoding, made
[squirrelmail.git] / src / compose.php
CommitLineData
59177427 1<?php
df15de21 2 /** This code sends a mail.
3 **
4 ** There are 3 modes of operation:
5 ** - Start new mail
6 ** - Add an attachment
7 ** - Send mail
8 **/
9
2a32fc83 10 session_start();
11
d068c0ec 12 if (!isset($config_php))
13 include("../config/config.php");
14 if (!isset($strings_php))
15 include("../functions/strings.php");
16 if (!isset($page_header_php))
17 include("../functions/page_header.php");
18 if (!isset($imap_php))
19 include("../functions/imap.php");
20 if (!isset($date_php))
21 include("../functions/date.php");
22 if (!isset($mime_php))
23 include("../functions/mime.php");
24 if (!isset($smtp_php))
25 include("../functions/smtp.php");
26 if (!isset($display_messages_php))
27 include("../functions/display_messages.php");
f7fb20fe 28
d3cdb279 29 include("../src/load_prefs.php");
8467bf00 30
4ba45d11 31 // This function is used when not sending or adding attachments
df15de21 32 function newMail () {
33 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
3806fa52 34 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc;
e39d73e5 35
2e434774 36 $send_to = decodeHeader($send_to);
37 $send_to_cc = decodeHeader($send_to_cc);
a53e5469 38
429f8906 39 if ($forward_id)
40 $id = $forward_id;
41 else if ($reply_id)
42 $id = $reply_id;
43
44 if ($id) {
813eba2f 45 sqimap_mailbox_select($imapConnection, $mailbox);
429f8906 46 $message = sqimap_get_message($imapConnection, $id, $mailbox);
47 $message = getEntity($message, $ent_num);
48
49 if ($message->header->type0 == "text" || $message->header->type1 == "message") {
50 $body = decodeBody(mime_fetch_body($imapConnection, $id, $message->header->entity_id), $message->header->encoding);
51 } else {
52 $body = "";
df15de21 53 }
54
429f8906 55 if ($forward_id)
56 $tmp = _("-------- Original Message ---------\n");
57 if ($message->header->type1 == "html")
58 $body = strip_tags($body);
59
df15de21 60 $body_ary = explode("\n", $body);
61 $body = "";
429f8906 62 for ($i=0; $i < count($body_ary); $i++) {
63 $tmp = $body_ary[$i];
64
65 if ($forward_id)
66 $body = "$body$tmp\n";
df15de21 67 else
429f8906 68 $body = "$body> $tmp\n";
78509c54 69 }
429f8906 70
78509c54 71 }
429f8906 72
29d08a52 73 if (!$send_to) {
74 $send_to = sqimap_find_email($send_to);
75 }
76
df15de21 77 $send_to = ereg_replace("\"", "", $send_to);
78 $send_to = stripslashes($send_to);
79
80 /** This formats a CC string if they hit "reply all" **/
81 if ($send_to_cc != "") {
82 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
83 $sendcc = explode(",", $send_to_cc);
84 $send_to_cc = "";
85
86 for ($i = 0; $i < count($sendcc); $i++) {
87 $sendcc[$i] = trim($sendcc[$i]);
88 if ($sendcc[$i] == "")
89 continue;
90
a53e5469 91 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
813eba2f 92 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
93 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
df15de21 94
95 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
96 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
97 (trim($sendcc[$i]) != "")) {
98 $send_to_cc .= trim($sendcc[$i]) . ", ";
99 }
100 }
101 $send_to_cc = trim($send_to_cc);
102 if (substr($send_to_cc, -1) == ",") {
103 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
104 }
105 }
106 } // function newMail()
78509c54 107
df15de21 108 function showInputForm () {
109 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
4ba45d11 110 $passed_body, $color, $use_signature, $signature, $editor_size,
3806fa52 111 $attachments, $subject, $newmail, $use_javascript_addr_book,
966286ae 112 $send_to_bcc, $reply_id, $mailbox;
78509c54 113
2e434774 114 $subject = decodeHeader($subject);
115 $reply_subj = decodeHeader($reply_subj);
116 $forward_subj = decodeHeader($forward_subj);
a53e5469 117
3806fa52 118 if ($use_javascript_addr_book) {
119 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
120 echo "function open_abook() { \n";
121 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
122 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
123 echo " if((!nwin.opener) && (document.windows != null))\n";
124 echo " nwin.opener = document.windows;\n";
125 echo "}\n";
126 echo "// --></SCRIPT>\n\n";
127 }
5100704d 128
3806fa52 129 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\">\n";
966286ae 130 if ($reply_id) {
131 echo "<input type=hidden name=reply_id value=$reply_id>\n";
132 }
133 echo "<input type=hidden name=mailbox value=$mailbox>\n";
761d149e 134 echo "<TABLE WIDTH=50 ALIGN=center CELLSPACING=0 BORDER=0>\n";
df15de21 135 echo " <TR>\n";
136 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
df15de21 137 echo _("To:");
761d149e 138 echo " </TD><TD colspan=2 WIDTH=\"100%\" BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
df15de21 139 if ($send_to)
761d149e 140 echo " <INPUT TYPE=TEXT NAME=\"send_to\" VALUE=\"$send_to\" SIZE=60><BR>\n";
df15de21 141 else
761d149e 142 echo " <INPUT TYPE=TEXT NAME=\"send_to\" SIZE=60><BR>\n";
df15de21 143 echo " </TD>\n";
144 echo " </TR>\n";
145 echo " <TR>\n";
146 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
aae41ae9 147 echo _("CC:");
761d149e 148 echo " </TD><TD colspan=2 BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
df15de21 149 if ($send_to_cc)
761d149e 150 echo " <INPUT TYPE=TEXT NAME=\"send_to_cc\" SIZE=60 VALUE=\"$send_to_cc\"><BR>\n";
df15de21 151 else
761d149e 152 echo " <INPUT TYPE=TEXT NAME=\"send_to_cc\" SIZE=60><BR>\n";
df15de21 153 echo " </TD>\n";
154 echo " </TR>\n";
155 echo " <TR>\n";
156 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
aae41ae9 157 echo _("BCC:");
761d149e 158 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
df15de21 159 if ($send_to_bcc)
480feea7 160 echo " <INPUT TYPE=TEXT NAME=\"send_to_bcc\" VALUE=\"$send_to_bcc\" SIZE=60><BR>\n";
df15de21 161 else
480feea7 162 echo " <INPUT TYPE=TEXT NAME=\"send_to_bcc\" SIZE=60><BR>";
3806fa52 163
164 echo "</TD></TR>\n";
5100704d 165
df15de21 166 echo " <TR>\n";
167 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
df15de21 168 echo _("Subject:");
761d149e 169 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
df15de21 170 if ($reply_subj) {
171 $reply_subj = str_replace("\"", "'", $reply_subj);
172 $reply_subj = stripslashes($reply_subj);
173 $reply_subj = trim($reply_subj);
174 if (substr(strtolower($reply_subj), 0, 3) != "re:")
175 $reply_subj = "Re: $reply_subj";
480feea7 176 echo " <INPUT TYPE=TEXT NAME=subject SIZE=60 VALUE=\"$reply_subj\">";
df15de21 177 } else if ($forward_subj) {
178 $forward_subj = str_replace("\"", "'", $forward_subj);
179 $forward_subj = stripslashes($forward_subj);
180 $forward_subj = trim($forward_subj);
181 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
182 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
183 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
184 $forward_subj = "[Fwd: $forward_subj]";
480feea7 185 echo " <INPUT TYPE=TEXT NAME=subject SIZE=60 VALUE=\"$forward_subj\">";
df15de21 186 } else {
480feea7 187 echo " <INPUT TYPE=TEXT NAME=subject VALUE=\"$subject\" SIZE=60>";
31f3d7c0 188 }
480feea7 189 echo "</td></tr>\n\n";
190
191 echo " <TR><td>\n </td><td>\n";
192 if ($use_javascript_addr_book) {
193 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
194 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
195 echo " // --></SCRIPT><NOSCRIPT>\n";
196 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
197 echo " </NOSCRIPT>\n";
198 } else {
199 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
200 }
201 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
202 echo " </TD>\n";
203 echo " </TR>\n\n";
204
4ba45d11 205
e5b23ff2 206 echo " <TR>\n";
3806fa52 207 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=3>\n";
b57c4e63 208 if ($use_signature == true && $newmail == true)
209 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>". $body . "\n\n-- \n".$signature."</TEXTAREA><BR>";
e5b23ff2 210 else
211 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>".$body."</TEXTAREA><BR>\n";
212 echo " </TD>\n";
213 echo " </TR>\n";
6c7fd6ca 214 echo " <TR><TD COLSPAN=3 ALIGN=CENTER><INPUT TYPE=SUBMIT NAME=send VALUE=\"";
e5b23ff2 215 echo _("Send");
216 echo "\"></TD></TR>\n";
217
4ba45d11 218 // This code is for attachments
219 echo " <tr>\n";
e5b23ff2 220 echo " <TD WIDTH=50 BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
aae41ae9 221 echo " <SMALL><BR></SMALL>"._("Attach:");
761d149e 222 echo " </td><td colspan=2 ALIGN=left BGCOLOR=\"$color[0]\">\n";
4ba45d11 223 // echo " <INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\"\n";
224 // echo " value=\"10000\">\n";
225 echo " <INPUT NAME=\"attachfile\" TYPE=\"file\">\n";
469eb37b 226 echo " &nbsp;&nbsp;<input type=\"submit\" name=\"attach\"\n";
4ba45d11 227 echo " value=\"" . _("Add") ."\">\n";
469eb37b 228 echo " </td>\n";
469eb37b 229 echo " </tr>\n";
4ba45d11 230 if (isset($attachments) && count($attachments)>0) {
e5b23ff2 231 echo "</tr><tr><td width=50 bgcolor=\"$color[0]\" align=right>\n";
232 echo "&nbsp;";
761d149e 233 echo "</td><td align=left colspan=2 bgcolor=\"$color[0]\">";
4ba45d11 234 while (list($localname, $remotename) = each($attachments)) {
235 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
236 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
237 }
238
239 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
469eb37b 240 echo "</td></tr>";
4ba45d11 241 }
4ba45d11 242 // End of attachment code
243
ffc2ccbc 244 echo "</TABLE>\n";
df15de21 245 echo "</FORM>";
31f3d7c0 246 }
8467bf00 247
df15de21 248 function showSentForm () {
df15de21 249 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
9f2215a1 250 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
aae41ae9 251 echo "</CENTER>";
df15de21 252 }
b278172f 253
0ad7dbda 254 function checkInput ($show) {
255 /** I implemented the $show variable because the error messages
256 were getting sent before the page header. So, I check once
257 using $show=false, and then when i'm ready to display the
258 error message, show=true **/
259 global $body, $send_to, $subject, $color;
b278172f 260
df15de21 261 if ($body == "") {
0ad7dbda 262 if ($show)
263 plain_error_message(_("You have not entered a message body."), $color);
df15de21 264 return false;
265 } else if ($send_to == "") {
0ad7dbda 266 if ($show)
267 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
df15de21 268 return false;
269 } else if ($subject == "") {
0ad7dbda 270 if ($show)
271 plain_error_message(_("You have not entered a subject."), $color);
df15de21 272 return false;
b278172f 273 }
df15de21 274 return true;
275 } // function checkInput()
276
3806fa52 277
ecf51658 278 if (($mailbox == "") || ($mailbox == "None"))
dcb7f454 279 $mailbox = "INBOX";
3806fa52 280
4ba45d11 281 if(isset($send)) {
0ad7dbda 282 if (checkInput(false)) {
966286ae 283 $urlMailbox = urlencode ($mailbox);
284 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
285 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
df15de21 286 } else {
e1469126 287 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
dcb7f454 288 displayPageHeader($color, $mailbox);
0ad7dbda 289 checkInput(true);
290
df15de21 291 showInputForm();
7c6cb7ca 292 }
3806fa52 293 } else if ($html_addr_search_done) {
3806fa52 294 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
dcb7f454 295 displayPageHeader($color, $mailbox);
3806fa52 296
297 $body = stripslashes($body);
298 $send_to = stripslashes($send_to);
299 $send_to_cc = stripslashes($send_to_cc);
300 $send_to_bcc = stripslashes($send_to_bcc);
301 $subject = stripslashes($subject);
302
6c7fd6ca 303 for ($i=0; $i < count($send_to_search); $i++) {
304 if ($send_to)
305 $send_to .= ", ";
306 $send_to .= $send_to_search[$i];
307 }
308
309 for ($i=0; $i < count($send_to_cc_search); $i++) {
310 if ($send_to_cc)
311 $send_to_cc .= ", ";
312 $send_to_cc .= $send_to_cc_search[$i];
313 }
314
3806fa52 315 showInputForm();
6c7fd6ca 316 } else if ($html_addr_search) {
317 //* I am using an include so as to elminiate an extra unnecessary click. If you
318 //* can think of a better way, please implement it.
319 include ("addrbook_search_html.php");
4ba45d11 320 } else if (isset($attach)) {
e1469126 321 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
dcb7f454 322 displayPageHeader($color, $mailbox);
fc3348ac 323
4ba45d11 324 $localfilename = md5("$attachfile, $attachfile_name, $REMOTE_IP, $REMOTE_PORT, $UNIQUE_ID, and everything else that may add entropy");
c3c37167 325 $localfilename = $localfilename;
4ba45d11 326
327 // Put the file in a better place
328 error_reporting(0); // Rename will produce error output if it fails
c3c37167 329 if (!rename($attachfile, $attachment_dir.$localfilename)) {
330 if (!copy($attachfile, $attachment_dir.$localfilename)) {
4ba45d11 331 plain_error_message(_("Could not move/copy file. File not attached"));
c3c37167 332 $failed = true;
4ba45d11 333 }
334 }
335 // If it still exists, PHP will remove the original file
336
c3c37167 337 if (!$failed) {
338 // Write information about the file
339 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
340 fputs ($fp, "$attachfile_type\n$attachfile_name\n");
341 fclose ($fp);
4ba45d11 342
c3c37167 343 $attachments[$localfilename] = $attachfile_name;
344 }
4ba45d11 345
346 showInputForm();
347 } else if (isset($do_delete)) {
e1469126 348 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
dcb7f454 349 displayPageHeader($color, $mailbox);
fc3348ac 350
4ba45d11 351 while (list($key, $localname) = each($delete)) {
a53e5469 352 array_splice ($attachments, $key, 1);
c3c37167 353 unlink ($attachment_dir.$localname);
354 unlink ($attachment_dir.$localname.".info");
4ba45d11 355 }
4bfed9f3 356
4ba45d11 357 showInputForm();
358 } else {
e1469126 359 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
dcb7f454 360 displayPageHeader($color, $mailbox);
fc3348ac 361
b57c4e63 362 $newmail = true;
1220e677 363 newMail();
4ba45d11 364 showInputForm();
365 }
da79853a 366?>