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