added saving attachments again, and removed a "click here" screen after
[squirrelmail.git] / src / compose.php
1 <?
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 if (!isset($config_php))
11 include("../config/config.php");
12 if (!isset($strings_php))
13 include("../functions/strings.php");
14 if (!isset($page_header_php))
15 include("../functions/page_header.php");
16 if (!isset($imap_php))
17 include("../functions/imap.php");
18 if (!isset($date_php))
19 include("../functions/date.php");
20 if (!isset($mime_php))
21 include("../functions/mime.php");
22 if (!isset($smtp_php))
23 include("../functions/smtp.php");
24 if (!isset($display_messages_php))
25 include("../functions/display_messages.php");
26
27 include("../src/load_prefs.php");
28
29 // This function is used when not sending or adding attachments
30 function newMail () {
31 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
32 $reply_id, $send_to, $send_to_cc, $mailbox;
33
34 $send_to = decodeHeader($send_to);
35 $send_to_cc = decodeHeader($send_to_cc);
36
37 if ($forward_id) {
38 sqimap_mailbox_select($imapConnection, $mailbox);
39 $msg = sqimap_get_message($imapConnection, $forward_id, $mailbox);
40
41 if (containsType($msg, "text", "html", $ent_num)) {
42 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"]);
43 } else if (containsType($msg, "text", "plain", $ent_num)) {
44 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"]);
45 }
46 // add other primary displaying msg types here
47 else {
48 // find any type that's displayable
49 if (containsType($msg, "text", "any_type", $ent_num)) {
50 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"]);
51 } else if (containsType($msg, "msg", "any_type", $ent_num)) {
52 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"]);
53 } else {
54 $body = _("No Message");
55 }
56 }
57
58 $type1 = $msg["ENTITIES"][$ent_num]["TYPE1"];
59
60 $tmp = _("-------- Original Message ---------\n");
61 $body_ary = explode("\n", $body);
62 $body = "";
63 for ($i=0;$i < count($body_ary);$i++) {
64 if ($type1 == "html")
65 $tmp .= strip_tags($body_ary[$i]);
66 else
67 $tmp .= $body_ary[$i];
68 $body = "$body$tmp\n";
69 $tmp = "";
70 }
71 }
72
73 if ($reply_id) {
74 sqimap_mailbox_select($imapConnection, $mailbox);
75 $msg = sqimap_get_message($imapConnection, $reply_id, $mailbox);
76
77 if (containsType($msg, "text", "html", $ent_num)) {
78 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"], false);
79 } else if (containsType($msg, "text", "plain", $ent_num)) {
80 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"], false);
81 }
82 // add other primary displaying msg types here
83 else {
84 // find any type that's displayable
85 if (containsType($msg, "text", "any_type", $ent_num)) {
86 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"], false);
87 } else if (containsType($msg, "msg", "any_type", $ent_num)) {
88 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"], false);
89 } else {
90 $body = _("No Message");
91 }
92 }
93
94 $type1 = $msg["ENTITIES"][$ent_num]["TYPE1"];
95
96 $body_ary = explode("\n", $body);
97 $body = "";
98 for ($i=0;$i < count($body_ary);$i++) {
99 if ($type1 == "html")
100 $tmp = strip_tags($body_ary[$i]);
101 else
102 $tmp = $body_ary[$i];
103 $body = "$body> $tmp\n";
104 }
105 }
106
107 $send_to = sqimap_find_email($send_to);
108
109 $send_to = ereg_replace("\"", "", $send_to);
110 $send_to = stripslashes($send_to);
111
112 /** This formats a CC string if they hit "reply all" **/
113 if ($send_to_cc != "") {
114 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
115 $sendcc = explode(",", $send_to_cc);
116 $send_to_cc = "";
117
118 for ($i = 0; $i < count($sendcc); $i++) {
119 $sendcc[$i] = trim($sendcc[$i]);
120 if ($sendcc[$i] == "")
121 continue;
122
123 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
124 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
125 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
126
127 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
128 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
129 (trim($sendcc[$i]) != "")) {
130 $send_to_cc .= trim($sendcc[$i]) . ", ";
131 }
132 }
133 $send_to_cc = trim($send_to_cc);
134 if (substr($send_to_cc, -1) == ",") {
135 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
136 }
137 }
138 } // function newMail()
139
140 function showInputForm () {
141 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
142 $passed_body, $color, $use_signature, $signature, $editor_size,
143 $attachments, $subject, $newmail;
144
145 $subject = decodeHeader($subject);
146 $reply_subj = decodeHeader($reply_subj);
147 $forward_subj = decodeHeader($forward_subj);
148
149 echo "\n<FORM action=\"compose.php\" METHOD=POST\n";
150 echo "ENCTYPE=\"multipart/form-data\">\n";
151 echo "<TABLE COLS=2 WIDTH=50 ALIGN=center CELLSPACING=0 BORDER=0>\n";
152 echo " <TR>\n";
153 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
154 echo _("To:");
155 echo " </TD><TD WIDTH=% BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
156 if ($send_to)
157 echo " <INPUT TYPE=TEXT NAME=send_to VALUE=\"$send_to\" SIZE=60><BR>";
158 else
159 echo " <INPUT TYPE=TEXT NAME=send_to SIZE=60><BR>";
160 echo " </TD>\n";
161 echo " </TR>\n";
162 echo " <TR>\n";
163 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
164 echo _("CC:");
165 echo " </TD><TD WIDTH=% BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
166 if ($send_to_cc)
167 echo " <INPUT TYPE=TEXT NAME=send_to_cc SIZE=60 VALUE=\"$send_to_cc\"><BR>";
168 else
169 echo " <INPUT TYPE=TEXT NAME=send_to_cc SIZE=60><BR>";
170 echo " </TD>\n";
171 echo " </TR>\n";
172 echo " <TR>\n";
173 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
174 echo _("BCC:");
175 echo " </TD><TD WIDTH=% BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
176 if ($send_to_bcc)
177 echo " <INPUT TYPE=TEXT NAME=send_to_bcc VALUE=\"$send_to_bcc\" SIZE=60><BR>";
178 else
179 echo " <INPUT TYPE=TEXT NAME=send_to_bcc SIZE=60><BR>";
180 echo " </TD>\n";
181 echo " </TR>\n";
182 echo " <TR>\n";
183 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
184 echo _("Subject:");
185 echo " </TD><TD WIDTH=% BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
186 if ($reply_subj) {
187 $reply_subj = str_replace("\"", "'", $reply_subj);
188 $reply_subj = stripslashes($reply_subj);
189 $reply_subj = trim($reply_subj);
190 if (substr(strtolower($reply_subj), 0, 3) != "re:")
191 $reply_subj = "Re: $reply_subj";
192 echo " <INPUT TYPE=TEXT NAME=subject SIZE=60 VALUE=\"$reply_subj\">";
193 } else if ($forward_subj) {
194 $forward_subj = str_replace("\"", "'", $forward_subj);
195 $forward_subj = stripslashes($forward_subj);
196 $forward_subj = trim($forward_subj);
197 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
198 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
199 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
200 $forward_subj = "[Fwd: $forward_subj]";
201 echo " <INPUT TYPE=TEXT NAME=subject SIZE=50 VALUE=\"$forward_subj\">";
202 } else {
203 echo " <INPUT TYPE=TEXT NAME=subject VALUE=\"$subject\" SIZE=50>";
204 }
205 echo "&nbsp;&nbsp;<INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">";
206 echo " </TD>\n";
207 echo " </TR>\n";
208
209 echo " <TR>\n";
210 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
211 if ($use_signature == true && $newmail == true)
212 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>". $body . "\n\n-- \n".$signature."</TEXTAREA><BR>";
213 else
214 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>".$body."</TEXTAREA><BR>\n";
215 echo " </TD>\n";
216 echo " </TR>\n";
217 echo " <TR><TD COLSPAN=2 ALIGN=CENTER><INPUT TYPE=SUBMIT NAME=send VALUE=\"";
218 echo _("Send");
219 echo "\"></TD></TR>\n";
220
221 // This code is for attachments
222 echo " <tr>\n";
223 echo " <TD WIDTH=50 BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
224 echo " <SMALL><BR></SMALL>"._("Attach:");
225 echo " </td><td width=% ALIGN=left BGCOLOR=\"$color[0]\">\n";
226 // echo " <INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\"\n";
227 // echo " value=\"10000\">\n";
228 echo " <INPUT NAME=\"attachfile\" TYPE=\"file\">\n";
229 echo " &nbsp;&nbsp;<input type=\"submit\" name=\"attach\"\n";
230 echo " value=\"" . _("Add") ."\">\n";
231 echo " </td>\n";
232 echo " </font>\n";
233 echo " </tr>\n";
234 if (isset($attachments) && count($attachments)>0) {
235 echo "</tr><tr><td width=50 bgcolor=\"$color[0]\" align=right>\n";
236 echo "&nbsp;";
237 echo "</td><td width=% align=left bgcolor=\"$color[0]\">";
238 while (list($localname, $remotename) = each($attachments)) {
239 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
240 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
241 }
242
243 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
244 echo "</td></tr>";
245 }
246 // End of attachment code
247
248 echo "</TABLE>\n";
249 echo "</FORM>";
250 }
251
252 function showSentForm () {
253 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
254 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
255 echo "</CENTER>";
256 }
257
258 function checkInput () {
259 global $body, $send_to, $subject;
260
261 if ($body == "") {
262 plain_error_message(_("You have not entered a message body."), $color);
263 return false;
264 } else if ($send_to == "") {
265 displayPageHeader($color, "None");
266 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
267 return false;
268 } else if ($subject == "") {
269 plain_error_message(_("You have not entered a subject."), $color);
270 return false;
271 }
272 return true;
273 } // function checkInput()
274
275 if(isset($send)) {
276 if (checkInput()) {
277 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body);
278 header ("Location: right_main.php");
279 } else {
280 echo "<HTML><BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
281 $imapConnection = sqimap_login($username, $key, $imapServerAddress, 0);
282 displayPageHeader($color, "None");
283
284 showInputForm();
285 }
286 } else if (isset($attach)) {
287 echo "<HTML><BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
288 $imapConnection = sqimap_login($username, $key, $imapServerAddress, 0);
289 displayPageHeader($color, "None");
290
291 $localfilename = md5("$attachfile, $attachfile_name, $REMOTE_IP, $REMOTE_PORT, $UNIQUE_ID, and everything else that may add entropy");
292 $localfilename = $localfilename;
293
294 // Put the file in a better place
295 error_reporting(0); // Rename will produce error output if it fails
296 if (!rename($attachfile, $attachment_dir.$localfilename)) {
297 if (!copy($attachfile, $attachment_dir.$localfilename)) {
298 plain_error_message(_("Could not move/copy file. File not attached"));
299 $failed = true;
300 }
301 }
302 // If it still exists, PHP will remove the original file
303
304 if (!$failed) {
305 // Write information about the file
306 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
307 fputs ($fp, "$attachfile_type\n$attachfile_name\n");
308 fclose ($fp);
309
310 $attachments[$localfilename] = $attachfile_name;
311 }
312
313 showInputForm();
314 } else if (isset($do_delete)) {
315 echo "<HTML><BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
316 $imapConnection = sqimap_login($username, $key, $imapServerAddress, 0);
317 displayPageHeader($color, "None");
318
319 while (list($key, $localname) = each($delete)) {
320 array_splice ($attachments, $key, 1);
321 unlink ($attachment_dir.$localname);
322 unlink ($attachment_dir.$localname.".info");
323 }
324
325 showInputForm();
326 } else {
327 echo "<HTML><BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
328 $imapConnection = sqimap_login($username, $key, $imapServerAddress, 0);
329 displayPageHeader($color, "None");
330
331 $newmail = true;
332 newMail();
333 showInputForm();
334 }
335 ?>