59177427 |
1 | <?php |
b8ea4ed6 |
2 | /** smtp.php |
3 | ** |
4 | ** This contains all the functions needed to send messages through |
36fa79c8 |
5 | ** an smtp server or sendmail. |
b8ea4ed6 |
6 | **/ |
7 | |
d068c0ec |
8 | $smtp_php = true; |
465db5d7 |
9 | |
df15de21 |
10 | // Returns true only if this message is multipart |
11 | function isMultipart () { |
4ba45d11 |
12 | global $attachments; |
13 | |
14 | if (count($attachments)>0) |
15 | return true; |
16 | else |
17 | return false; |
df15de21 |
18 | } |
19 | |
20 | // Attach the files that are due to be attached |
4ba45d11 |
21 | function attachFiles ($fp) { |
c3c37167 |
22 | global $attachments, $attachment_dir; |
4ba45d11 |
23 | |
7b67334e |
24 | $length = 0; |
25 | |
a7d75834 |
26 | if (isMultipart()) { |
27 | reset($attachments); |
28 | while (list($localname, $remotename) = each($attachments)) { |
29 | // This is to make sure noone is giving a filename in another |
30 | // directory |
31 | $localname = ereg_replace ("\\/", "", $localname); |
32 | |
33 | $fileinfo = fopen ($attachment_dir.$localname.".info", "r"); |
34 | $filetype = fgets ($fileinfo, 8192); |
35 | fclose ($fileinfo); |
36 | $filetype = trim ($filetype); |
37 | if ($filetype=="") |
38 | $filetype = "application/octet-stream"; |
39 | |
40 | $header = "--".mimeBoundary()."\r\n"; |
f8308f16 |
41 | $header .= "Content-Type: $filetype\r\n"; |
a7d75834 |
42 | $header .= "Content-Disposition: attachment; filename=\"$remotename\"\r\n"; |
43 | $header .= "Content-Transfer-Encoding: base64\r\n\r\n"; |
44 | fputs ($fp, $header); |
45 | $length += strlen($header); |
46 | |
47 | $file = fopen ($attachment_dir.$localname, "r"); |
48 | while ($tmp = fread($file, 570)) { |
49 | $encoded = chunk_split(base64_encode($tmp)); |
50 | $length += strlen($encoded); |
51 | fputs ($fp, $encoded); |
52 | } |
53 | fclose ($file); |
7b67334e |
54 | } |
4ba45d11 |
55 | } |
7b67334e |
56 | |
57 | return $length; |
df15de21 |
58 | } |
59 | |
a7d75834 |
60 | // Delete files that are uploaded for attaching |
61 | function deleteAttachments() { |
62 | global $attachments, $attachment_dir; |
63 | |
64 | if (isMultipart()) { |
65 | reset($attachments); |
66 | while (list($localname, $remotename) = each($attachments)) { |
67 | if (!ereg ("\\/", $localname)) { |
68 | unlink ($attachment_dir.$localname); |
69 | unlink ($attachment_dir.$localname.".info"); |
70 | } |
71 | } |
72 | } |
73 | } |
74 | |
df15de21 |
75 | // Return a nice MIME-boundary |
76 | function mimeBoundary () { |
7b67334e |
77 | global $version, $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT; |
78 | |
79 | static $mimeBoundaryString; |
df15de21 |
80 | |
81 | if ($mimeBoundaryString == "") { |
82 | $temp = "SquirrelMail".$version.$REMOTE_ADDR.$SERVER_NAME. |
83 | $REMOTE_PORT; |
4ba45d11 |
84 | $mimeBoundaryString = "=-_+".substr(md5($temp),1,20); |
465db5d7 |
85 | } |
df15de21 |
86 | |
87 | return $mimeBoundaryString; |
465db5d7 |
88 | } |
89 | |
24397423 |
90 | /* Time offset for correct timezone */ |
91 | function timezone () { |
92 | $diff_second = date("Z"); |
93 | if ($diff_second > 0) |
94 | $sign = "+"; |
95 | else |
96 | $sign = "-"; |
97 | |
98 | $diff_second = abs($diff_second); |
99 | |
100 | $diff_hour = floor ($diff_second / 3600); |
101 | $diff_minute = floor (($diff_second-3600*$diff_hour) / 60); |
102 | |
103 | $zonename = "(".strftime("%Z").")"; |
104 | $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename); |
105 | return ($result); |
106 | } |
107 | |
df15de21 |
108 | /* Print all the needed RFC822 headers */ |
60994e13 |
109 | function write822Header ($fp, $t, $c, $b, $subject, $more_headers) { |
a7d75834 |
110 | global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT; |
df15de21 |
111 | global $data_dir, $username, $domain, $version, $useSendmail; |
9949206d |
112 | global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR; |
a5363806 |
113 | global $REMOTE_HOST; |
465db5d7 |
114 | |
7b67334e |
115 | // Storing the header to make sure the header is the same |
116 | // everytime the header is printed. |
117 | static $header, $headerlength; |
118 | |
119 | if ($header == "") { |
120 | $to = parseAddrs($t); |
121 | $cc = parseAddrs($c); |
122 | $bcc = parseAddrs($b); |
123 | $reply_to = getPref($data_dir, $username, "reply_to"); |
124 | $from = getPref($data_dir, $username, "full_name"); |
125 | $from_addr = getPref($data_dir, $username, "email_address"); |
126 | |
127 | if ($from_addr == "") |
128 | $from_addr = "$username@$domain"; |
129 | |
130 | $to_list = getLineOfAddrs($to); |
131 | $cc_list = getLineOfAddrs($cc); |
132 | $bcc_list = getLineOfAddrs($bcc); |
133 | |
78e230b7 |
134 | /* Encoding 8-bit characters and making from line */ |
135 | $subject = encodeHeader($subject); |
7b67334e |
136 | if ($from == "") |
137 | $from = "<$from_addr>"; |
138 | else |
78e230b7 |
139 | $from = encodeHeader($from) . " <$from_addr>"; |
7b67334e |
140 | |
a7d75834 |
141 | /* This creates an RFC 822 date */ |
7b67334e |
142 | $date = date("D, j M Y H:i:s ", mktime()) . timezone(); |
a7d75834 |
143 | |
144 | /* Create a message-id */ |
145 | $message_id = "<" . $REMOTE_PORT . "." . $REMOTE_ADDR . "."; |
a5363806 |
146 | $message_id .= time() . ".squirrel@" . $SERVER_NAME .">"; |
7b67334e |
147 | |
148 | /* Make an RFC822 Received: line */ |
a5363806 |
149 | if (isset($REMOTE_HOST)) |
8a2848f0 |
150 | $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])"; |
a5363806 |
151 | else |
8a2848f0 |
152 | $received_from = $REMOTE_ADDR; |
a5363806 |
153 | |
9949206d |
154 | if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) { |
155 | if ($HTTP_X_FORWARDED_FOR == "") |
156 | $HTTP_X_FORWARDED_FOR = "unknown"; |
157 | $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)"; |
158 | } |
a5363806 |
159 | |
8a2848f0 |
160 | $header = "Received: from $received_from\r\n"; |
161 | $header .= " (SquirrelMail authenticated user $username)\r\n"; |
162 | $header .= " by $SERVER_NAME with HTTP;\r\n"; |
163 | $header .= " $date\r\n"; |
7b67334e |
164 | |
a7d75834 |
165 | /* Insert the rest of the header fields */ |
166 | $header .= "Message-ID: $message_id\r\n"; |
7b67334e |
167 | $header .= "Date: $date\r\n"; |
168 | $header .= "Subject: $subject\r\n"; |
169 | $header .= "From: $from\r\n"; |
170 | $header .= "To: $to_list \r\n"; // Who it's TO |
60994e13 |
171 | |
172 | /* Insert headers from the $more_headers array */ |
173 | if(is_array($more_headers)) { |
174 | reset($more_headers); |
175 | while(list($h_name, $h_val) = each($more_headers)) { |
176 | $header .= sprintf("%s: %s\r\n", $h_name, $h_val); |
177 | } |
178 | } |
179 | |
7b67334e |
180 | if ($cc_list) { |
181 | $header .= "Cc: $cc_list\r\n"; // Who the CCs are |
df15de21 |
182 | } |
7b67334e |
183 | |
184 | if ($reply_to != "") |
185 | $header .= "Reply-To: $reply_to\r\n"; |
186 | |
187 | if ($useSendmail) { |
188 | if ($bcc_list) { |
189 | // BCCs is removed from header by sendmail |
190 | $header .= "Bcc: $bcc_list\r\n"; |
191 | } |
192 | } |
193 | |
194 | $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail |
195 | |
196 | // Do the MIME-stuff |
f8308f16 |
197 | $header .= "MIME-Version: 1.0\r\n"; |
7b67334e |
198 | |
199 | if (isMultipart()) { |
200 | $header .= "Content-Type: multipart/mixed; boundary=\""; |
201 | $header .= mimeBoundary(); |
202 | $header .= "\"\r\n"; |
203 | } else { |
17ce8467 |
204 | if ($default_charset != "") |
205 | $header .= "Content-Type: text/plain; charset=$default_charset\r\n"; |
206 | else |
207 | $header .= "Content-Type: text/plain;\r\n"; |
7b67334e |
208 | $header .= "Content-Transfer-Encoding: 8bit\r\n"; |
209 | } |
210 | $header .= "\r\n"; // One blank line to separate header and body |
df15de21 |
211 | |
7b67334e |
212 | $headerlength = strlen($header); |
213 | } |
214 | |
215 | // Write the header |
216 | fputs ($fp, $header); |
df15de21 |
217 | |
7b67334e |
218 | return $headerlength; |
219 | } |
df15de21 |
220 | |
7b67334e |
221 | // Send the body |
222 | function writeBody ($fp, $passedBody) { |
17ce8467 |
223 | global $default_charset; |
224 | |
7b67334e |
225 | $attachmentlength = 0; |
226 | |
df15de21 |
227 | if (isMultipart()) { |
7b67334e |
228 | $body = "--".mimeBoundary()."\r\n"; |
17ce8467 |
229 | |
230 | if ($default_charset != "") |
231 | $body .= "Content-Type: text/plain; charset=$default_charset\r\n"; |
232 | else |
233 | $body .= "Content-Type: text/plain\r\n"; |
234 | |
7b67334e |
235 | $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n"; |
236 | $body .= stripslashes($passedBody) . "\r\n"; |
237 | fputs ($fp, $body); |
238 | |
6441f7c6 |
239 | $attachmentlength = attachFiles($fp); |
7b67334e |
240 | |
241 | $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n"; |
242 | fputs ($fp, $postbody); |
df15de21 |
243 | } else { |
7b67334e |
244 | $body = stripslashes($passedBody) . "\r\n"; |
245 | fputs ($fp, $body); |
246 | $postbody = "\r\n"; |
247 | fputs ($fp, $postbody); |
df15de21 |
248 | } |
df15de21 |
249 | |
7b67334e |
250 | return (strlen($body) + strlen($postbody) + $attachmentlength); |
c99c5b31 |
251 | } |
465db5d7 |
252 | |
c99c5b31 |
253 | // Send mail using the sendmail command |
60994e13 |
254 | function sendSendmail($t, $c, $b, $subject, $body, $more_headers) { |
c99c5b31 |
255 | global $sendmail_path, $username, $domain; |
7b67334e |
256 | |
c99c5b31 |
257 | // open pipe to sendmail |
7b67334e |
258 | $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w"); |
c99c5b31 |
259 | |
60994e13 |
260 | $headerlength = write822Header ($fp, $t, $c, $b, $subject, $more_headers); |
7b67334e |
261 | $bodylength = writeBody($fp, $body); |
465db5d7 |
262 | |
263 | pclose($fp); |
7b67334e |
264 | |
0775b17f |
265 | return ($headerlength + $bodylength); |
465db5d7 |
266 | } |
267 | |
b8ea4ed6 |
268 | function smtpReadData($smtpConnection) { |
269 | $read = fgets($smtpConnection, 1024); |
270 | $counter = 0; |
271 | while ($read) { |
272 | echo $read . "<BR>"; |
273 | $data[$counter] = $read; |
274 | $read = fgets($smtpConnection, 1024); |
275 | $counter++; |
276 | } |
277 | } |
278 | |
60994e13 |
279 | function sendSMTP($t, $c, $b, $subject, $body, $more_headers) { |
356d7825 |
280 | global $username, $domain, $version, $smtpServerAddress, $smtpPort, |
28010579 |
281 | $data_dir, $color; |
7831268e |
282 | |
40ee9452 |
283 | $to = parseAddrs($t); |
284 | $cc = parseAddrs($c); |
285 | $bcc = parseAddrs($b); |
356d7825 |
286 | $from_addr = getPref($data_dir, $username, "email_address"); |
287 | |
288 | if ($from_addr == "") |
289 | $from_addr = "$username@$domain"; |
d3cdb279 |
290 | |
c175e2e4 |
291 | $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString); |
b8ea4ed6 |
292 | if (!$smtpConnection) { |
293 | echo "Error connecting to SMTP Server.<br>"; |
294 | echo "$errorNumber : $errorString<br>"; |
295 | exit; |
296 | } |
d3cdb279 |
297 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
298 | errorCheck($tmp); |
b8ea4ed6 |
299 | |
40ee9452 |
300 | $to_list = getLineOfAddrs($to); |
301 | $cc_list = getLineOfAddrs($cc); |
302 | |
303 | /** Lets introduce ourselves */ |
3379d740 |
304 | fputs($smtpConnection, "HELO $domain\r\n"); |
d3cdb279 |
305 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
306 | errorCheck($tmp); |
7831268e |
307 | |
40ee9452 |
308 | /** Ok, who is sending the message? */ |
3379d740 |
309 | fputs($smtpConnection, "MAIL FROM:<$from_addr>\r\n"); |
d3cdb279 |
310 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
311 | errorCheck($tmp); |
b8ea4ed6 |
312 | |
40ee9452 |
313 | /** send who the recipients are */ |
314 | for ($i = 0; $i < count($to); $i++) { |
3379d740 |
315 | fputs($smtpConnection, "RCPT TO:<$to[$i]>\r\n"); |
d3cdb279 |
316 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
317 | errorCheck($tmp); |
40ee9452 |
318 | } |
319 | for ($i = 0; $i < count($cc); $i++) { |
3379d740 |
320 | fputs($smtpConnection, "RCPT TO:<$cc[$i]>\r\n"); |
d3cdb279 |
321 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
322 | errorCheck($tmp); |
40ee9452 |
323 | } |
324 | for ($i = 0; $i < count($bcc); $i++) { |
3379d740 |
325 | fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\r\n"); |
d3cdb279 |
326 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
327 | errorCheck($tmp); |
40ee9452 |
328 | } |
b8ea4ed6 |
329 | |
40ee9452 |
330 | /** Lets start sending the actual message */ |
3379d740 |
331 | fputs($smtpConnection, "DATA\r\n"); |
d3cdb279 |
332 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
333 | errorCheck($tmp); |
7831268e |
334 | |
7b67334e |
335 | // Send the message |
60994e13 |
336 | $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject, $more_headers); |
7b67334e |
337 | $bodylength = writeBody($smtpConnection, $body); |
7831268e |
338 | |
3379d740 |
339 | fputs($smtpConnection, ".\r\n"); // end the DATA part |
d3cdb279 |
340 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
341 | $num = errorCheck($tmp); |
342 | if ($num != 250) { |
28010579 |
343 | echo "ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>"; |
3021b626 |
344 | } |
d3cdb279 |
345 | |
3379d740 |
346 | fputs($smtpConnection, "QUIT\r\n"); // log off |
78509c54 |
347 | |
348 | fclose($smtpConnection); |
7b67334e |
349 | |
350 | return ($headerlength + $bodylength); |
b8ea4ed6 |
351 | } |
3021b626 |
352 | |
353 | |
354 | function errorCheck($line) { |
776c7431 |
355 | global $page_header_php; |
28010579 |
356 | global $color; |
776c7431 |
357 | if (!isset($page_header_php)) { |
358 | include "../functions/page_header.php"; |
359 | } |
360 | |
3021b626 |
361 | // Status: 0 = fatal |
362 | // 5 = ok |
363 | |
364 | $err_num = substr($line, 0, strpos($line, " ")); |
365 | switch ($err_num) { |
366 | case 500: $message = "Syntax error; command not recognized"; |
367 | $status = 0; |
368 | break; |
369 | case 501: $message = "Syntax error in parameters or arguments"; |
370 | $status = 0; |
371 | break; |
372 | case 502: $message = "Command not implemented"; |
373 | $status = 0; |
374 | break; |
375 | case 503: $message = "Bad sequence of commands"; |
376 | $status = 0; |
377 | break; |
378 | case 504: $message = "Command parameter not implemented"; |
379 | $status = 0; |
380 | break; |
381 | |
382 | |
383 | case 211: $message = "System status, or system help reply"; |
384 | $status = 5; |
385 | break; |
386 | case 214: $message = "Help message"; |
387 | $status = 5; |
388 | break; |
389 | |
390 | |
391 | case 220: $message = "Service ready"; |
392 | $status = 5; |
393 | break; |
394 | case 221: $message = "Service closing transmission channel"; |
395 | $status = 5; |
396 | break; |
397 | case 421: $message = "Service not available, closing chanel"; |
398 | $status = 0; |
399 | break; |
400 | |
401 | |
402 | case 250: $message = "Requested mail action okay, completed"; |
403 | $status = 5; |
404 | break; |
405 | case 251: $message = "User not local; will forward"; |
406 | $status = 5; |
407 | break; |
408 | case 450: $message = "Requested mail action not taken: mailbox unavailable"; |
409 | $status = 0; |
410 | break; |
411 | case 550: $message = "Requested action not taken: mailbox unavailable"; |
412 | $status = 0; |
413 | break; |
414 | case 451: $message = "Requested action aborted: error in processing"; |
415 | $status = 0; |
416 | break; |
417 | case 551: $message = "User not local; please try forwarding"; |
418 | $status = 0; |
419 | break; |
420 | case 452: $message = "Requested action not taken: insufficient system storage"; |
421 | $status = 0; |
422 | break; |
423 | case 552: $message = "Requested mail action aborted: exceeding storage allocation"; |
424 | $status = 0; |
425 | break; |
426 | case 553: $message = "Requested action not taken: mailbox name not allowed"; |
427 | $status = 0; |
428 | break; |
429 | case 354: $message = "Start mail input; end with ."; |
430 | $status = 5; |
431 | break; |
432 | case 554: $message = "Transaction failed"; |
433 | $status = 0; |
434 | break; |
435 | default: $message = "Unknown response: $line"; |
436 | $status = 0; |
437 | $error_num = "001"; |
438 | break; |
439 | } |
440 | |
441 | if ($status == 0) { |
776c7431 |
442 | displayPageHeader($color, "None"); |
3021b626 |
443 | echo "<TT>"; |
776c7431 |
444 | echo "<br><b><font color=\"$color[1]\">ERROR</font></b><br><br>"; |
3021b626 |
445 | echo " <B>Error Number: </B>$err_num<BR>"; |
446 | echo " <B>Reason: </B>$message<BR>"; |
447 | echo "<B>Server Response: </B>$line<BR>"; |
448 | echo "<BR>MAIL NOT SENT"; |
449 | echo "</TT></BODY></HTML>"; |
450 | exit; |
451 | } |
452 | return $err_num; |
453 | } |
df15de21 |
454 | |
966286ae |
455 | function sendMessage($t, $c, $b, $subject, $body, $reply_id) { |
456 | global $useSendmail, $msg_id, $is_reply, $mailbox; |
6441f7c6 |
457 | global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort; |
60994e13 |
458 | $more_headers = Array(); |
d1b8b679 |
459 | |
966286ae |
460 | $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1); |
60994e13 |
461 | |
966286ae |
462 | if ($reply_id) { |
463 | sqimap_mailbox_select ($imap_stream, $mailbox); |
464 | sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, "Answered"); |
60994e13 |
465 | |
466 | // Insert In-Reply-To and References headers if the |
467 | // message-id of the message we reply to is set (longer than "<>") |
468 | // The References header should really be the old Referenced header |
469 | // with the message ID appended, but it can be only the message ID too. |
470 | $hdr = sqimap_get_small_header ($imap_stream, $reply_id, false); |
471 | if(strlen($hdr->message_id) > 2) { |
472 | $more_headers["In-Reply-To"] = $hdr->message_id; |
473 | $more_headers["References"] = $hdr->message_id; |
474 | } |
966286ae |
475 | } |
476 | |
df15de21 |
477 | if ($useSendmail==true) { |
60994e13 |
478 | $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers); |
df15de21 |
479 | } else { |
60994e13 |
480 | $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers); |
df15de21 |
481 | } |
d1b8b679 |
482 | |
2966f172 |
483 | if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) { |
484 | sqimap_append ($imap_stream, $sent_folder, $length); |
60994e13 |
485 | write822Header ($imap_stream, $t, $c, $b, $subject, $more_headers); |
2966f172 |
486 | writeBody ($imap_stream, $body); |
487 | sqimap_append_done ($imap_stream); |
488 | } |
1195c340 |
489 | sqimap_logout($imap_stream); |
a7d75834 |
490 | // Delete the files uploaded for attaching (if any). |
491 | deleteAttachments(); |
df15de21 |
492 | } |
465db5d7 |
493 | ?> |