Fixed a few minor bugs. Mail address was wrong when replying.
[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 include("../config/config.php");
11 include("../functions/strings.php");
12 include("../functions/page_header.php");
13 include("../functions/imap.php");
14 include("../functions/date.php");
15 include("../functions/mime.php");
16 include("../functions/smtp.php");
17 include("../functions/display_messages.php");
18
19 include("../src/load_prefs.php");
20
21 echo "<HTML><BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
22 $imapConnection = sqimap_login($username, $key, $imapServerAddress, 0);
23 displayPageHeader($color, "None");
24
25 // This function is used when not sending or adding attachments
26 function newMail () {
27 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
28 $reply_id, $send_to, $send_to_cc, $mailbox;
29
30 $send_to = rfc1522Decode($send_to);
31 $send_to_cc = rfc1522Decode($send_to_cc);
32
33 if ($forward_id) {
34 sqimap_mailbox_select($imapConnection, $mailbox);
35 $msg = sqimap_get_message($imapConnection, $forward_id, $mailbox);
36
37 if (containsType($msg, "text", "html", $ent_num)) {
38 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"]);
39 } else if (containsType($msg, "text", "plain", $ent_num)) {
40 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"]);
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 = "";
66 }
67 }
68
69 if ($reply_id) {
70 sqimap_mailbox_select($imapConnection, $mailbox);
71 $msg = sqimap_get_message($imapConnection, $reply_id, $mailbox);
72
73 if (containsType($msg, "text", "html", $ent_num)) {
74 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"], false);
75 } else if (containsType($msg, "text", "plain", $ent_num)) {
76 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"], false);
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";
100 }
101 }
102
103 $send_to = sqimap_find_email($send_to);
104
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
119 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
120 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
121 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
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()
135
136 function showInputForm () {
137 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
138 $passed_body, $color, $use_signature, $signature, $editor_size,
139 $attachments, $subject, $newmail;
140
141 $subject = rfc1522Decode($subject);
142 $reply_subj = rfc1522Decode($reply_subj);
143 $forward_subj = rfc1522Decode($forward_subj);
144
145 echo "\n<FORM action=\"compose.php\" METHOD=POST\n";
146 echo "ENCTYPE=\"multipart/form-data\">\n";
147 echo "<TABLE COLS=2 WIDTH=50 ALIGN=center CELLSPACING=0 BORDER=0>\n";
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";
162 echo " <FONT FACE=\"Arial,Helvetica\">"._("CC").":</FONT>\n";
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]";
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 " <FONT FACE=\"Arial,Helvetica\">";
225 echo " <SMALL><BR></SMALL>"._("Attach:")."</FONT>\n";
226 echo " </td><td width=% ALIGN=left BGCOLOR=\"$color[0]\">\n";
227 // echo " <INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\"\n";
228 // echo " value=\"10000\">\n";
229 echo " <INPUT NAME=\"attachfile\" TYPE=\"file\">\n";
230 echo " &nbsp;&nbsp;<input type=\"submit\" name=\"attach\"\n";
231 echo " value=\"" . _("Add") ."\">\n";
232 echo " </td>\n";
233 echo " </font>\n";
234 echo " </tr>\n";
235 if (isset($attachments) && count($attachments)>0) {
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]\">";
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";
245 echo "</td></tr>";
246 }
247 // End of attachment code
248
249 echo "</TABLE>\n";
250 echo "</FORM>";
251 }
252
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 }
259
260 function checkInput () {
261 global $body, $send_to, $subject;
262
263 if ($body == "") {
264 plain_error_message(_("You have not entered a message body."), $color);
265 return false;
266 } else if ($send_to == "") {
267 displayPageHeader($color, "None");
268 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
269 return false;
270 } else if ($subject == "") {
271 plain_error_message(_("You have not entered a subject."), $color);
272 return false;
273 }
274 return true;
275 } // function checkInput()
276
277 if(isset($send)) {
278 if (checkInput()) {
279 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body);
280 showSentForm();
281 } else {
282 showInputForm();
283 }
284 } else if (isset($attach)) {
285 $localfilename = md5("$attachfile, $attachfile_name, $REMOTE_IP, $REMOTE_PORT, $UNIQUE_ID, and everything else that may add entropy");
286 $localfilename = $localfilename;
287
288 // Put the file in a better place
289 error_reporting(0); // Rename will produce error output if it fails
290 if (!rename($attachfile, $attachment_dir.$localfilename)) {
291 if (!copy($attachfile, $attachment_dir.$localfilename)) {
292 plain_error_message(_("Could not move/copy file. File not attached"));
293 $failed = true;
294 }
295 }
296 // If it still exists, PHP will remove the original file
297
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);
303
304 $attachments[$localfilename] = $attachfile_name;
305 }
306
307 showInputForm();
308 } else if (isset($do_delete)) {
309 while (list($key, $localname) = each($delete)) {
310 array_splice ($attachments, $key, 1);
311 unlink ($attachment_dir.$localname);
312 unlink ($attachment_dir.$localname.".info");
313 }
314
315 showInputForm();
316 } else {
317 $newmail = true;
318 newMail();
319 showInputForm();
320 }
321 ?>