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