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