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 */ |
c99c5b31 |
109 | function write822Header ($fp, $t, $c, $b, $subject) { |
a7d75834 |
110 | global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT; |
df15de21 |
111 | global $data_dir, $username, $domain, $version, $useSendmail; |
17ce8467 |
112 | global $default_charset; |
465db5d7 |
113 | |
7b67334e |
114 | // Storing the header to make sure the header is the same |
115 | // everytime the header is printed. |
116 | static $header, $headerlength; |
117 | |
118 | if ($header == "") { |
119 | $to = parseAddrs($t); |
120 | $cc = parseAddrs($c); |
121 | $bcc = parseAddrs($b); |
122 | $reply_to = getPref($data_dir, $username, "reply_to"); |
123 | $from = getPref($data_dir, $username, "full_name"); |
124 | $from_addr = getPref($data_dir, $username, "email_address"); |
125 | |
126 | if ($from_addr == "") |
127 | $from_addr = "$username@$domain"; |
128 | |
129 | $to_list = getLineOfAddrs($to); |
130 | $cc_list = getLineOfAddrs($cc); |
131 | $bcc_list = getLineOfAddrs($bcc); |
132 | |
133 | if ($from == "") |
134 | $from = "<$from_addr>"; |
135 | else |
136 | $from = $from . " <$from_addr>"; |
c3084273 |
137 | |
138 | /* Encoding 8-bit characters */ |
139 | $subject = encodeHeader($subject); |
140 | $from = encodeHeader($from); |
7b67334e |
141 | |
a7d75834 |
142 | /* This creates an RFC 822 date */ |
7b67334e |
143 | $date = date("D, j M Y H:i:s ", mktime()) . timezone(); |
a7d75834 |
144 | |
145 | /* Create a message-id */ |
146 | $message_id = "<" . $REMOTE_PORT . "." . $REMOTE_ADDR . "."; |
147 | $message_id .= time() . "@" . $SERVER_NAME .">"; |
7b67334e |
148 | |
149 | /* Make an RFC822 Received: line */ |
150 | $header = "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; "; |
f8308f16 |
151 | $header .= "$date\r\n"; |
7b67334e |
152 | |
a7d75834 |
153 | /* Insert the rest of the header fields */ |
154 | $header .= "Message-ID: $message_id\r\n"; |
7b67334e |
155 | $header .= "Date: $date\r\n"; |
156 | $header .= "Subject: $subject\r\n"; |
157 | $header .= "From: $from\r\n"; |
158 | $header .= "To: $to_list \r\n"; // Who it's TO |
159 | |
160 | if ($cc_list) { |
161 | $header .= "Cc: $cc_list\r\n"; // Who the CCs are |
df15de21 |
162 | } |
7b67334e |
163 | |
164 | if ($reply_to != "") |
165 | $header .= "Reply-To: $reply_to\r\n"; |
166 | |
167 | if ($useSendmail) { |
168 | if ($bcc_list) { |
169 | // BCCs is removed from header by sendmail |
170 | $header .= "Bcc: $bcc_list\r\n"; |
171 | } |
172 | } |
173 | |
174 | $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail |
175 | |
176 | // Do the MIME-stuff |
f8308f16 |
177 | $header .= "MIME-Version: 1.0\r\n"; |
7b67334e |
178 | |
179 | if (isMultipart()) { |
180 | $header .= "Content-Type: multipart/mixed; boundary=\""; |
181 | $header .= mimeBoundary(); |
182 | $header .= "\"\r\n"; |
183 | } else { |
17ce8467 |
184 | if ($default_charset != "") |
185 | $header .= "Content-Type: text/plain; charset=$default_charset\r\n"; |
186 | else |
187 | $header .= "Content-Type: text/plain;\r\n"; |
7b67334e |
188 | $header .= "Content-Transfer-Encoding: 8bit\r\n"; |
189 | } |
190 | $header .= "\r\n"; // One blank line to separate header and body |
df15de21 |
191 | |
7b67334e |
192 | $headerlength = strlen($header); |
193 | } |
194 | |
195 | // Write the header |
196 | fputs ($fp, $header); |
df15de21 |
197 | |
7b67334e |
198 | return $headerlength; |
199 | } |
df15de21 |
200 | |
7b67334e |
201 | // Send the body |
202 | function writeBody ($fp, $passedBody) { |
17ce8467 |
203 | global $default_charset; |
204 | |
7b67334e |
205 | $attachmentlength = 0; |
206 | |
df15de21 |
207 | if (isMultipart()) { |
7b67334e |
208 | $body = "--".mimeBoundary()."\r\n"; |
17ce8467 |
209 | |
210 | if ($default_charset != "") |
211 | $body .= "Content-Type: text/plain; charset=$default_charset\r\n"; |
212 | else |
213 | $body .= "Content-Type: text/plain\r\n"; |
214 | |
7b67334e |
215 | $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n"; |
216 | $body .= stripslashes($passedBody) . "\r\n"; |
217 | fputs ($fp, $body); |
218 | |
6441f7c6 |
219 | $attachmentlength = attachFiles($fp); |
7b67334e |
220 | |
221 | $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n"; |
222 | fputs ($fp, $postbody); |
df15de21 |
223 | } else { |
7b67334e |
224 | $body = stripslashes($passedBody) . "\r\n"; |
225 | fputs ($fp, $body); |
226 | $postbody = "\r\n"; |
227 | fputs ($fp, $postbody); |
df15de21 |
228 | } |
df15de21 |
229 | |
7b67334e |
230 | return (strlen($body) + strlen($postbody) + $attachmentlength); |
c99c5b31 |
231 | } |
465db5d7 |
232 | |
c99c5b31 |
233 | // Send mail using the sendmail command |
234 | function sendSendmail($t, $c, $b, $subject, $body) { |
235 | global $sendmail_path, $username, $domain; |
7b67334e |
236 | |
c99c5b31 |
237 | // open pipe to sendmail |
7b67334e |
238 | $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w"); |
c99c5b31 |
239 | |
7b67334e |
240 | $headerlength = write822Header ($fp, $t, $c, $b, $subject); |
241 | $bodylength = writeBody($fp, $body); |
465db5d7 |
242 | |
243 | pclose($fp); |
7b67334e |
244 | |
245 | return ($headerlength + $bodylenght); |
465db5d7 |
246 | } |
247 | |
b8ea4ed6 |
248 | function smtpReadData($smtpConnection) { |
249 | $read = fgets($smtpConnection, 1024); |
250 | $counter = 0; |
251 | while ($read) { |
252 | echo $read . "<BR>"; |
253 | $data[$counter] = $read; |
254 | $read = fgets($smtpConnection, 1024); |
255 | $counter++; |
256 | } |
257 | } |
258 | |
c99c5b31 |
259 | function sendSMTP($t, $c, $b, $subject, $body) { |
356d7825 |
260 | global $username, $domain, $version, $smtpServerAddress, $smtpPort, |
28010579 |
261 | $data_dir, $color; |
7831268e |
262 | |
40ee9452 |
263 | $to = parseAddrs($t); |
264 | $cc = parseAddrs($c); |
265 | $bcc = parseAddrs($b); |
356d7825 |
266 | $from_addr = getPref($data_dir, $username, "email_address"); |
267 | |
268 | if ($from_addr == "") |
269 | $from_addr = "$username@$domain"; |
d3cdb279 |
270 | |
c175e2e4 |
271 | $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString); |
b8ea4ed6 |
272 | if (!$smtpConnection) { |
273 | echo "Error connecting to SMTP Server.<br>"; |
274 | echo "$errorNumber : $errorString<br>"; |
275 | exit; |
276 | } |
d3cdb279 |
277 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
278 | errorCheck($tmp); |
b8ea4ed6 |
279 | |
40ee9452 |
280 | $to_list = getLineOfAddrs($to); |
281 | $cc_list = getLineOfAddrs($cc); |
282 | |
283 | /** Lets introduce ourselves */ |
3379d740 |
284 | fputs($smtpConnection, "HELO $domain\r\n"); |
d3cdb279 |
285 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
286 | errorCheck($tmp); |
7831268e |
287 | |
40ee9452 |
288 | /** Ok, who is sending the message? */ |
3379d740 |
289 | fputs($smtpConnection, "MAIL FROM:<$from_addr>\r\n"); |
d3cdb279 |
290 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
291 | errorCheck($tmp); |
b8ea4ed6 |
292 | |
40ee9452 |
293 | /** send who the recipients are */ |
294 | for ($i = 0; $i < count($to); $i++) { |
3379d740 |
295 | fputs($smtpConnection, "RCPT TO:<$to[$i]>\r\n"); |
d3cdb279 |
296 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
297 | errorCheck($tmp); |
40ee9452 |
298 | } |
299 | for ($i = 0; $i < count($cc); $i++) { |
3379d740 |
300 | fputs($smtpConnection, "RCPT TO:<$cc[$i]>\r\n"); |
d3cdb279 |
301 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
302 | errorCheck($tmp); |
40ee9452 |
303 | } |
304 | for ($i = 0; $i < count($bcc); $i++) { |
3379d740 |
305 | fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\r\n"); |
d3cdb279 |
306 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
307 | errorCheck($tmp); |
40ee9452 |
308 | } |
b8ea4ed6 |
309 | |
40ee9452 |
310 | /** Lets start sending the actual message */ |
3379d740 |
311 | fputs($smtpConnection, "DATA\r\n"); |
d3cdb279 |
312 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
313 | errorCheck($tmp); |
7831268e |
314 | |
7b67334e |
315 | // Send the message |
316 | $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject); |
317 | $bodylength = writeBody($smtpConnection, $body); |
7831268e |
318 | |
3379d740 |
319 | fputs($smtpConnection, ".\r\n"); // end the DATA part |
d3cdb279 |
320 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
3021b626 |
321 | $num = errorCheck($tmp); |
322 | if ($num != 250) { |
28010579 |
323 | echo "ERROR<BR>Message not sent!<BR>Reason given: $tmp<BR></BODY></HTML>"; |
3021b626 |
324 | } |
d3cdb279 |
325 | |
3379d740 |
326 | fputs($smtpConnection, "QUIT\r\n"); // log off |
78509c54 |
327 | |
328 | fclose($smtpConnection); |
7b67334e |
329 | |
330 | return ($headerlength + $bodylength); |
b8ea4ed6 |
331 | } |
3021b626 |
332 | |
333 | |
334 | function errorCheck($line) { |
28010579 |
335 | global $color; |
3021b626 |
336 | // Status: 0 = fatal |
337 | // 5 = ok |
338 | |
339 | $err_num = substr($line, 0, strpos($line, " ")); |
340 | switch ($err_num) { |
341 | case 500: $message = "Syntax error; command not recognized"; |
342 | $status = 0; |
343 | break; |
344 | case 501: $message = "Syntax error in parameters or arguments"; |
345 | $status = 0; |
346 | break; |
347 | case 502: $message = "Command not implemented"; |
348 | $status = 0; |
349 | break; |
350 | case 503: $message = "Bad sequence of commands"; |
351 | $status = 0; |
352 | break; |
353 | case 504: $message = "Command parameter not implemented"; |
354 | $status = 0; |
355 | break; |
356 | |
357 | |
358 | case 211: $message = "System status, or system help reply"; |
359 | $status = 5; |
360 | break; |
361 | case 214: $message = "Help message"; |
362 | $status = 5; |
363 | break; |
364 | |
365 | |
366 | case 220: $message = "Service ready"; |
367 | $status = 5; |
368 | break; |
369 | case 221: $message = "Service closing transmission channel"; |
370 | $status = 5; |
371 | break; |
372 | case 421: $message = "Service not available, closing chanel"; |
373 | $status = 0; |
374 | break; |
375 | |
376 | |
377 | case 250: $message = "Requested mail action okay, completed"; |
378 | $status = 5; |
379 | break; |
380 | case 251: $message = "User not local; will forward"; |
381 | $status = 5; |
382 | break; |
383 | case 450: $message = "Requested mail action not taken: mailbox unavailable"; |
384 | $status = 0; |
385 | break; |
386 | case 550: $message = "Requested action not taken: mailbox unavailable"; |
387 | $status = 0; |
388 | break; |
389 | case 451: $message = "Requested action aborted: error in processing"; |
390 | $status = 0; |
391 | break; |
392 | case 551: $message = "User not local; please try forwarding"; |
393 | $status = 0; |
394 | break; |
395 | case 452: $message = "Requested action not taken: insufficient system storage"; |
396 | $status = 0; |
397 | break; |
398 | case 552: $message = "Requested mail action aborted: exceeding storage allocation"; |
399 | $status = 0; |
400 | break; |
401 | case 553: $message = "Requested action not taken: mailbox name not allowed"; |
402 | $status = 0; |
403 | break; |
404 | case 354: $message = "Start mail input; end with ."; |
405 | $status = 5; |
406 | break; |
407 | case 554: $message = "Transaction failed"; |
408 | $status = 0; |
409 | break; |
410 | default: $message = "Unknown response: $line"; |
411 | $status = 0; |
412 | $error_num = "001"; |
413 | break; |
414 | } |
415 | |
416 | if ($status == 0) { |
175e7218 |
417 | echo "<HTML><BODY BGCOLOR=#ffffff>"; |
3021b626 |
418 | echo "<TT>"; |
419 | echo "<BR><B>ERROR</B><BR><BR>"; |
420 | echo " <B>Error Number: </B>$err_num<BR>"; |
421 | echo " <B>Reason: </B>$message<BR>"; |
422 | echo "<B>Server Response: </B>$line<BR>"; |
423 | echo "<BR>MAIL NOT SENT"; |
424 | echo "</TT></BODY></HTML>"; |
425 | exit; |
426 | } |
427 | return $err_num; |
428 | } |
df15de21 |
429 | |
430 | function sendMessage($t, $c, $b, $subject, $body) { |
49b3690b |
431 | global $useSendmail, $msg_id, $is_reply; |
6441f7c6 |
432 | global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort; |
d1b8b679 |
433 | |
df15de21 |
434 | if ($useSendmail==true) { |
7b67334e |
435 | $length = sendSendmail($t, $c, $b, $subject, $body); |
df15de21 |
436 | } else { |
7b67334e |
437 | $length = sendSMTP($t, $c, $b, $subject, $body); |
df15de21 |
438 | } |
d1b8b679 |
439 | |
28010579 |
440 | $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1); |
2966f172 |
441 | if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) { |
442 | sqimap_append ($imap_stream, $sent_folder, $length); |
443 | write822Header ($imap_stream, $t, $c, $b, $subject); |
444 | writeBody ($imap_stream, $body); |
445 | sqimap_append_done ($imap_stream); |
446 | } |
f8308f16 |
447 | |
a7d75834 |
448 | // Delete the files uploaded for attaching (if any). |
449 | deleteAttachments(); |
df15de21 |
450 | } |
465db5d7 |
451 | ?> |