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