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