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