Added use of signature separator.
[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";
57101712 150 echo "<TABLE COLS=2 WIDTH=50 ALIGN=center CELLSPACING=0 BORDER=0>\n";
df15de21 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]";
57101712 204 echo " <INPUT TYPE=TEXT NAME=subject SIZE=50 VALUE=\"$forward_subj\">";
df15de21 205 } else {
57101712 206 echo " <INPUT TYPE=TEXT NAME=subject VALUE=\"$subject\" SIZE=50>";
31f3d7c0 207 }
57101712 208 echo "&nbsp;&nbsp;<INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">";
df15de21 209 echo " </TD>\n";
210 echo " </TR>\n";
4ba45d11 211
e5b23ff2 212 echo " <TR>\n";
213 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
214 if ($use_signature == true)
36c5a7ec 215 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>". $body . "\n\n-- \n".$signature."</TEXTAREA><BR>";
e5b23ff2 216 else
217 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>".$body."</TEXTAREA><BR>\n";
218 echo " </TD>\n";
219 echo " </TR>\n";
220 echo " <TR><TD COLSPAN=2 ALIGN=CENTER><INPUT TYPE=SUBMIT NAME=send VALUE=\"";
221 echo _("Send");
222 echo "\"></TD></TR>\n";
223
4ba45d11 224 // This code is for attachments
225 echo " <tr>\n";
e5b23ff2 226 echo " <TD WIDTH=50 BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
4ba45d11 227 echo " <FONT FACE=\"Arial,Helvetica\">";
469eb37b 228 echo " <SMALL><BR></SMALL>"._("Attach:")."</FONT>\n";
e5b23ff2 229 echo " </td><td width=% ALIGN=left BGCOLOR=\"$color[0]\">\n";
4ba45d11 230 // echo " <INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\"\n";
231 // echo " value=\"10000\">\n";
232 echo " <INPUT NAME=\"attachfile\" TYPE=\"file\">\n";
469eb37b 233 echo " &nbsp;&nbsp;<input type=\"submit\" name=\"attach\"\n";
4ba45d11 234 echo " value=\"" . _("Add") ."\">\n";
469eb37b 235 echo " </td>\n";
236 echo " </font>\n";
237 echo " </tr>\n";
4ba45d11 238 if (isset($attachments) && count($attachments)>0) {
e5b23ff2 239 echo "</tr><tr><td width=50 bgcolor=\"$color[0]\" align=right>\n";
240 echo "&nbsp;";
241 echo "</td><td width=% align=left bgcolor=\"$color[0]\">";
4ba45d11 242 while (list($localname, $remotename) = each($attachments)) {
243 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
244 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
245 }
246
247 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
469eb37b 248 echo "</td></tr>";
4ba45d11 249 }
4ba45d11 250 // End of attachment code
251
ffc2ccbc 252 echo "</TABLE>\n";
df15de21 253 echo "</FORM>";
31f3d7c0 254 }
8467bf00 255
df15de21 256 function showSentForm () {
257 echo "<FONT FACE=\"Arial,Helvetica\">";
258 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
259 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
260 echo "</CENTER></FONT>";
261 }
b278172f 262
df15de21 263 function checkInput () {
264 global $body, $send_to, $subject;
b278172f 265
df15de21 266 if ($body == "") {
4ba45d11 267 plain_error_message(_("You have not entered a message body."), $color);
df15de21 268 return false;
269 } else if ($send_to == "") {
270 displayPageHeader($color, "None");
4ba45d11 271 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
df15de21 272 return false;
273 } else if ($subject == "") {
4ba45d11 274 plain_error_message(_("You have not entered a subject."), $color);
df15de21 275 return false;
b278172f 276 }
df15de21 277 return true;
278 } // function checkInput()
279
4ba45d11 280 if(isset($send)) {
df15de21 281 if (checkInput()) {
282 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body);
283 showSentForm();
284 } else {
285 showInputForm();
7c6cb7ca 286 }
4ba45d11 287 } else if (isset($attach)) {
288 $localfilename = md5("$attachfile, $attachfile_name, $REMOTE_IP, $REMOTE_PORT, $UNIQUE_ID, and everything else that may add entropy");
c3c37167 289 $localfilename = $localfilename;
4ba45d11 290
291 // Put the file in a better place
292 error_reporting(0); // Rename will produce error output if it fails
c3c37167 293 if (!rename($attachfile, $attachment_dir.$localfilename)) {
294 if (!copy($attachfile, $attachment_dir.$localfilename)) {
4ba45d11 295 plain_error_message(_("Could not move/copy file. File not attached"));
c3c37167 296 $failed = true;
4ba45d11 297 }
298 }
299 // If it still exists, PHP will remove the original file
300
c3c37167 301 if (!$failed) {
302 // Write information about the file
303 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
304 fputs ($fp, "$attachfile_type\n$attachfile_name\n");
305 fclose ($fp);
4ba45d11 306
c3c37167 307 $attachments[$localfilename] = $attachfile_name;
308 }
4ba45d11 309
310 showInputForm();
311 } else if (isset($do_delete)) {
312 while (list($key, $localname) = each($delete)) {
313 array_splice ($attachments, $localname, 1);
c3c37167 314 unlink ($attachment_dir.$localname);
315 unlink ($attachment_dir.$localname.".info");
4ba45d11 316 }
4bfed9f3 317
4ba45d11 318 showInputForm();
319 } else {
320 Newmail();
321 showInputForm();
322 }
da79853a 323?>