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