Minor bug that let the user get files from any directory on the server using
[squirrelmail.git] / functions / smtp.php
1 <?
2 /** smtp.php
3 **
4 ** This contains all the functions needed to send messages through
5 ** an smtp server or sendmail.
6 **/
7
8
9 /* These next 2 functions are stub functions for implementations of
10 attachments */
11
12 // Returns true only if this message is multipart
13 function isMultipart () {
14 global $attachments;
15
16 if (count($attachments)>0)
17 return true;
18 else
19 return false;
20 }
21
22 // Attach the files that are due to be attached
23 function attachFiles ($fp) {
24 global $attachments, $attachment_dir;
25
26 while (list($localname, $remotename) = each($attachments)) {
27 // This is to make sure noone is giving a filename in another
28 // directory
29 $localname = ereg_replace ("\\/", "", $localname);
30
31 $fileinfo = fopen ($attachment_dir.$localname.".info", "r");
32 $filetype = fgets ($fileinfo, 8192);
33 fclose ($fileinfo);
34 $filetype = trim ($filetype);
35 if ($filetype=="")
36 $filetype = "application/octet-stream";
37
38 fputs ($fp, "--".mimeBoundary()."\n");
39 fputs ($fp, "Content-Type: $filetype\n");
40 fputs ($fp, "Content-Disposition: attachment; filename=\"$remotename\"\n");
41 fputs ($fp, "Content-Transfer-Encoding: base64\n\n");
42
43 $file = fopen ($attachment_dir.$localname, "r");
44 while ($tmp = fread($file, 57))
45 fputs ($fp, chunk_split(base64_encode($tmp)));
46 fclose ($file);
47
48 unlink ($attachment_dir.$localname);
49 unlink ($attachment_dir.$localname.".info");
50 }
51 }
52
53 // Return a nice MIME-boundary
54 function mimeBoundary () {
55 global $mimeBoundaryString, $version, $REMOTE_ADDR, $SERVER_NAME,
56 $REMOTE_PORT;
57
58 if ($mimeBoundaryString == "") {
59 $temp = "SquirrelMail".$version.$REMOTE_ADDR.$SERVER_NAME.
60 $REMOTE_PORT;
61 $mimeBoundaryString = "=-_+".substr(md5($temp),1,20);
62 }
63
64 return $mimeBoundaryString;
65 }
66
67 /* Time offset for correct timezone */
68 function timezone () {
69 $diff_second = date("Z");
70 if ($diff_second > 0)
71 $sign = "+";
72 else
73 $sign = "-";
74
75 $diff_second = abs($diff_second);
76
77 $diff_hour = floor ($diff_second / 3600);
78 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
79
80 $zonename = "(".strftime("%Z").")";
81 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename);
82 return ($result);
83 }
84
85 /* Print all the needed RFC822 headers */
86 function write822Header ($fp, $t, $c, $b, $subject) {
87 global $REMOTE_ADDR, $SERVER_NAME;
88 global $data_dir, $username, $domain, $version, $useSendmail;
89
90 $to = parseAddrs($t);
91 $cc = parseAddrs($c);
92 $bcc = parseAddrs($b);
93 $from_addr = "$username@$domain";
94 $reply_to = getPref($data_dir, $username, "reply_to");
95 $from = getPref($data_dir, $username, "full_name");
96
97 $to_list = getLineOfAddrs($to);
98 $cc_list = getLineOfAddrs($cc);
99 $bcc_list = getLineOfAddrs($bcc);
100
101 if ($from == "")
102 $from = "<$from_addr>";
103 else
104 $from = $from . " <$from_addr>";
105
106 /* This creates an RFC 822 date showing GMT */
107 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
108
109 /* Make an RFC822 Received: line */
110 fputs ($fp, "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ");
111 fputs ($fp, "$date\n");
112
113 /* The rest of the header */
114 fputs ($fp, "Date: $date\n");
115 fputs ($fp, "Subject: $subject\n"); // Subject
116 fputs ($fp, "From: $from\n"); // Subject
117 fputs ($fp, "To: $to_list\n"); // Who it's TO
118
119 if ($cc_list) {
120 fputs($fp, "Cc: $cc_list\n"); // Who the CCs are
121 }
122
123 if ($reply_to != "")
124 fputs($fp, "Reply-To: $reply_to\n");
125
126 if ($useSendmail) {
127 if ($bcc_list) {
128 // BCCs is removed from header by sendmail
129 fputs($fp, "Bcc: $bcc_list\n");
130 }
131 }
132
133 fputs($fp, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
134
135 // Do the MIME-stuff
136 fputs($fp, "MIME-Version: 1.0\n");
137
138 if (isMultipart()) {
139 fputs ($fp, "Content-Type: multipart/mixed; boundary=\"");
140 fputs ($fp, mimeBoundary());
141 fputs ($fp, "\"\n");
142 } else {
143 fputs($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
144 fputs($fp, "Content-Transfer-Encoding: 8bit\n");
145 }
146 }
147
148 // Send the body
149 function writeBody ($fp, $body) {
150 if (isMultipart()) {
151 fputs ($fp, "--".mimeBoundary()."\n");
152 fputs ($fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
153 fputs ($fp, "Content-Transfer-Encoding: 8bit\n\n");
154 fputs ($fp, stripslashes($body) . "\n");
155 attachFiles($fp);
156 fputs ($fp, "\n--".mimeBoundary()."--\n");
157 } else {
158 fputs ($fp, stripslashes($body) . "\n");
159 }
160 }
161
162 // Send mail using the sendmail command
163 function sendSendmail($t, $c, $b, $subject, $body) {
164 global $sendmail_path, $username, $domain;
165
166 // open pipe to sendmail
167 $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w");
168
169 write822Header ($fp, $t, $c, $b, $subject);
170 writeBody($fp, $body);
171
172 pclose($fp);
173 }
174
175 function smtpReadData($smtpConnection) {
176 $read = fgets($smtpConnection, 1024);
177 $counter = 0;
178 while ($read) {
179 echo $read . "<BR>";
180 $data[$counter] = $read;
181 $read = fgets($smtpConnection, 1024);
182 $counter++;
183 }
184 }
185
186 function sendSMTP($t, $c, $b, $subject, $body) {
187 global $username, $domain, $version, $smtpServerAddress, $smtpPort;
188
189 $to = parseAddrs($t);
190 $cc = parseAddrs($c);
191 $bcc = parseAddrs($b);
192 $from_addr = "$username@$domain";
193
194 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
195 if (!$smtpConnection) {
196 echo "Error connecting to SMTP Server.<br>";
197 echo "$errorNumber : $errorString<br>";
198 exit;
199 }
200 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
201 errorCheck($tmp);
202
203 $to_list = getLineOfAddrs($to);
204 $cc_list = getLineOfAddrs($cc);
205
206 /** Lets introduce ourselves */
207 fputs($smtpConnection, "HELO $domain\n");
208 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
209 errorCheck($tmp);
210
211 /** Ok, who is sending the message? */
212 fputs($smtpConnection, "MAIL FROM:<$from_addr>\n");
213 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
214 errorCheck($tmp);
215
216 /** send who the recipients are */
217 for ($i = 0; $i < count($to); $i++) {
218 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
219 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
220 errorCheck($tmp);
221 }
222 for ($i = 0; $i < count($cc); $i++) {
223 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
224 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
225 errorCheck($tmp);
226 }
227 for ($i = 0; $i < count($bcc); $i++) {
228 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
229 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
230 errorCheck($tmp);
231 }
232
233 /** Lets start sending the actual message */
234 fputs($smtpConnection, "DATA\n");
235 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
236 errorCheck($tmp);
237
238 write822Header ($smtpConnection, $t, $c, $b, $subject);
239
240 writeBody($smtpConnection, $body); // send the body of the message
241
242 fputs($smtpConnection, ".\n"); // end the DATA part
243 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
244 $num = errorCheck($tmp);
245 if ($num != 250) {
246 echo "<HTML><BODY BGCOLOR=FFFFFF>ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>";
247 }
248
249 fputs($smtpConnection, "QUIT\n"); // log off
250
251 fclose($smtpConnection);
252 }
253
254
255 function errorCheck($line) {
256 // Status: 0 = fatal
257 // 5 = ok
258
259 $err_num = substr($line, 0, strpos($line, " "));
260 switch ($err_num) {
261 case 500: $message = "Syntax error; command not recognized";
262 $status = 0;
263 break;
264 case 501: $message = "Syntax error in parameters or arguments";
265 $status = 0;
266 break;
267 case 502: $message = "Command not implemented";
268 $status = 0;
269 break;
270 case 503: $message = "Bad sequence of commands";
271 $status = 0;
272 break;
273 case 504: $message = "Command parameter not implemented";
274 $status = 0;
275 break;
276
277
278 case 211: $message = "System status, or system help reply";
279 $status = 5;
280 break;
281 case 214: $message = "Help message";
282 $status = 5;
283 break;
284
285
286 case 220: $message = "Service ready";
287 $status = 5;
288 break;
289 case 221: $message = "Service closing transmission channel";
290 $status = 5;
291 break;
292 case 421: $message = "Service not available, closing chanel";
293 $status = 0;
294 break;
295
296
297 case 250: $message = "Requested mail action okay, completed";
298 $status = 5;
299 break;
300 case 251: $message = "User not local; will forward";
301 $status = 5;
302 break;
303 case 450: $message = "Requested mail action not taken: mailbox unavailable";
304 $status = 0;
305 break;
306 case 550: $message = "Requested action not taken: mailbox unavailable";
307 $status = 0;
308 break;
309 case 451: $message = "Requested action aborted: error in processing";
310 $status = 0;
311 break;
312 case 551: $message = "User not local; please try forwarding";
313 $status = 0;
314 break;
315 case 452: $message = "Requested action not taken: insufficient system storage";
316 $status = 0;
317 break;
318 case 552: $message = "Requested mail action aborted: exceeding storage allocation";
319 $status = 0;
320 break;
321 case 553: $message = "Requested action not taken: mailbox name not allowed";
322 $status = 0;
323 break;
324 case 354: $message = "Start mail input; end with .";
325 $status = 5;
326 break;
327 case 554: $message = "Transaction failed";
328 $status = 0;
329 break;
330 default: $message = "Unknown response: $line";
331 $status = 0;
332 $error_num = "001";
333 break;
334 }
335
336 if ($status == 0) {
337 echo "<HTML><BODY BGCOLOR=FFFFFF>";
338 echo "<TT>";
339 echo "<BR><B>ERROR</B><BR><BR>";
340 echo "&nbsp;&nbsp;&nbsp;<B>Error Number: </B>$err_num<BR>";
341 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B>Reason: </B>$message<BR>";
342 echo "<B>Server Response: </B>$line<BR>";
343 echo "<BR>MAIL NOT SENT";
344 echo "</TT></BODY></HTML>";
345 exit;
346 }
347 return $err_num;
348 }
349
350 function sendMessage($t, $c, $b, $subject, $body) {
351 global $useSendmail;
352
353 if ($useSendmail==true) {
354 sendSendmail($t, $c, $b, $subject, $body);
355 } else {
356 sendSMTP($t, $c, $b, $subject, $body);
357 }
358
359 }
360
361 ?>