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