e1ee60fe |
1 | <?php |
5b8fd093 |
2 | /** |
3 | * Deliver_SMTP.class.php |
4 | * |
5 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
6 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
7 | * |
8 | * Delivery backend for the Deliver class. |
9 | * |
10 | * $Id$ |
11 | */ |
e1ee60fe |
12 | |
0f85ddf9 |
13 | require_once(SM_PATH . 'class/deliver/Deliver.class.php'); |
e1ee60fe |
14 | |
15 | class Deliver_SMTP extends Deliver { |
16 | |
5fe73b9f |
17 | function preWriteToStream(&$s) { |
18 | if ($s) { |
19 | if ($s{0} == '.') $s = '.' . $s; |
20 | $s = str_replace("\n.","\n..",$s); |
21 | } |
22 | } |
23 | |
24 | function initStream($message, $domain, $length=0, $host='', $port='', $user='', $pass='', $authpop=false) { |
47a29326 |
25 | global $use_smtp_tls,$smtp_auth_mech,$username,$key,$onetimepad; |
26 | |
27 | if ($authpop) { |
28 | $this->authPop($host, '', $username, $pass); |
5fe73b9f |
29 | } |
47a29326 |
30 | |
31 | $rfc822_header = $message->rfc822_header; |
5fe73b9f |
32 | $from = $rfc822_header->from[0]; |
33 | $to = $rfc822_header->to; |
34 | $cc = $rfc822_header->cc; |
47a29326 |
35 | $bcc = $rfc822_header->bcc; |
36 | |
37 | if (($use_smtp_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) { |
38 | $stream = fsockopen('tls://' . $host, $port, $errorNumber, $errorString); |
39 | } else { |
40 | $stream = fsockopen($host, $port, $errorNumber, $errorString); |
41 | } |
5fe73b9f |
42 | |
5fe73b9f |
43 | if (!$stream) { |
44 | $this->dlv_msg = $errorString; |
45 | $this->dlv_ret_nr = $errorNumber; |
46 | return(0); |
47 | } |
48 | $tmp = fgets($stream, 1024); |
49 | if ($this->errorCheck($tmp, $stream)) { |
50 | return(0); |
51 | } |
52 | |
53 | /* Lets introduce ourselves */ |
639c7164 |
54 | if (( $smtp_auth_mech == 'cram-md5') or ( $smtp_auth_mech == 'digest-md5' )) { |
47a29326 |
55 | // Doing some form of non-plain auth |
56 | fputs($stream, "EHLO $domain\r\n"); |
57 | $tmp = fgets($stream,1024); |
58 | if ($this->errorCheck($tmp,$stream)) { |
59 | return(0); |
60 | } |
61 | if ($smtp_auth_mech == 'cram-md5') { |
62 | fputs($stream, "AUTH CRAM-MD5\r\n"); |
63 | } elseif ($smtp_auth_mech == 'digest-md5') { |
64 | fputs($stream, "AUTH DIGEST-MD5\r\n"); |
65 | } |
66 | $tmp = fgets($stream,1024); |
67 | |
68 | if ($this->errorCheck($tmp,$stream)) { |
69 | return(0); |
70 | } |
71 | |
72 | // At this point, $tmp should hold "334 <challenge string>" |
73 | $chall = substr($tmp,4); |
74 | // Depending on mechanism, generate response string |
75 | if ($smtp_auth_mech == 'cram-md5') { |
76 | $response = cram_md5_response($username,$pass,$chall); |
77 | } elseif ($smtp_auth_mech == 'digest-md5') { |
78 | $response = digest_md5_response($username,$pass,$chall,'smtp',$host); |
79 | } |
80 | fputs($stream, $response); |
81 | |
82 | // Let's see what the server had to say about that |
83 | $tmp = fgets($stream,1024); |
84 | if ($this->errorCheck($tmp,$stream)) { |
85 | return(0); |
86 | } |
87 | |
88 | // CRAM-MD5 is done at this point. If DIGEST-MD5, there's a bit more to go |
89 | if ($smtp_auth_mech == 'digest-md5') |
90 | { |
91 | // $tmp contains rspauth, but I don't store that yet. (No need yet) |
92 | fputs($stream,"\r\n"); |
93 | $tmp = fgets($stream,1024); |
94 | |
95 | if ($this->errorCheck($tmp,$stream)) { |
96 | return(0); |
97 | } |
98 | } |
99 | // CRAM-MD5 and DIGEST-MD5 code ends here |
100 | } elseif ($smtp_auth_mech == 'none') { |
101 | // No auth at all, just send helo and then send the mail |
102 | fputs($stream, "HELO $domain\r\n"); |
103 | $tmp = fgets($stream, 1024); |
104 | if ($this->errorCheck($tmp, $stream)) { |
105 | return(0); |
106 | } |
107 | } elseif ($smtp_auth_mech == 'plain') { |
108 | // The plain LOGIN method |
109 | fputs($stream, "EHLO $domain\r\n"); |
110 | $tmp = fgets($stream, 1024); |
111 | if ($this->errorCheck($tmp, $stream)) { |
112 | return(0); |
113 | } |
114 | fputs($stream, "AUTH LOGIN\r\n"); |
115 | $tmp = fgets($stream, 1024); |
5fe73b9f |
116 | |
47a29326 |
117 | if ($this->errorCheck($tmp, $stream)) { |
118 | return(0); |
119 | } |
120 | fputs($stream, base64_encode ($username) . "\r\n"); |
121 | $tmp = fgets($stream, 1024); |
122 | if ($this->errorCheck($tmp, $stream)) { |
123 | return(0); |
124 | } |
5fe73b9f |
125 | |
47a29326 |
126 | fputs($stream, base64_encode($pass) . "\r\n"); |
127 | $tmp = fgets($stream, 1024); |
128 | if ($this->errorCheck($tmp, $stream)) { |
129 | return(0); |
130 | } |
5fe73b9f |
131 | } |
132 | |
133 | /* Ok, who is sending the message? */ |
134 | fputs($stream, 'MAIL FROM: <'.$from->mailbox.'@'.$from->host.">\r\n"); |
135 | $tmp = fgets($stream, 1024); |
136 | if ($this->errorCheck($tmp, $stream)) { |
137 | return(0); |
138 | } |
139 | |
140 | /* send who the recipients are */ |
141 | for ($i = 0, $cnt = count($to); $i < $cnt; $i++) { |
142 | if (!$to[$i]->host) $to[$i]->host = $domain; |
d1825c3c |
143 | if ($to[$i]->mailbox) { |
144 | fputs($stream, 'RCPT TO: <'.$to[$i]->mailbox.'@'.$to[$i]->host.">\r\n"); |
145 | $tmp = fgets($stream, 1024); |
146 | if ($this->errorCheck($tmp, $stream)) { |
147 | return(0); |
148 | } |
5fe73b9f |
149 | } |
150 | } |
d1825c3c |
151 | |
5fe73b9f |
152 | for ($i = 0, $cnt = count($cc); $i < $cnt; $i++) { |
d1825c3c |
153 | if (!$cc[$i]->host) $cc[$i]->host = $domain; |
154 | if ($cc[$i]->mailbox) { |
155 | fputs($stream, 'RCPT TO: <'.$cc[$i]->mailbox.'@'.$cc[$i]->host.">\r\n"); |
156 | $tmp = fgets($stream, 1024); |
157 | if ($this->errorCheck($tmp, $stream)) { |
158 | return(0); |
159 | } |
5fe73b9f |
160 | } |
161 | } |
162 | for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++) { |
d1825c3c |
163 | if (!$bcc[$i]->host) $bcc[$i]->host = $domain; |
164 | if ($bcc[$i]->mailbox) { |
165 | fputs($stream, 'RCPT TO: <'.$bcc[$i]->mailbox.'@'.$bcc[$i]->host.">\r\n"); |
166 | $tmp = fgets($stream, 1024); |
167 | if ($this->errorCheck($tmp, $stream)) { |
168 | return(0); |
169 | } |
5fe73b9f |
170 | } |
171 | } |
172 | /* Lets start sending the actual message */ |
173 | fputs($stream, "DATA\r\n"); |
174 | $tmp = fgets($stream, 1024); |
175 | if ($this->errorCheck($tmp, $stream)) { |
176 | return(0); |
177 | } |
178 | return $stream; |
179 | } |
180 | |
181 | function finalizeStream($stream) { |
182 | fputs($stream, ".\r\n"); /* end the DATA part */ |
183 | $tmp = fgets($stream, 1024); |
184 | $this->errorCheck($tmp, $stream); |
185 | if ($this->dlv_ret_nr != 250) { |
186 | return(0); |
187 | } |
188 | fputs($stream, "QUIT\r\n"); /* log off */ |
189 | fclose($stream); |
190 | return true; |
191 | } |
192 | |
193 | function errorCheck($line, $smtpConnection) { |
194 | global $color, $compose_new_win; |
47a29326 |
195 | |
5fe73b9f |
196 | /* Read new lines on a multiline response */ |
197 | $lines = $line; |
198 | while(ereg("^[0-9]+-", $line)) { |
199 | $line = fgets($smtpConnection, 1024); |
200 | $lines .= $line; |
201 | } |
202 | /* Status: 0 = fatal |
203 | * 5 = ok |
204 | */ |
205 | $err_num = substr($line, 0, strpos($line, " ")); |
206 | switch ($err_num) { |
207 | case 500: $message = 'Syntax error; command not recognized'; |
208 | $status = 0; |
209 | break; |
210 | case 501: $message = 'Syntax error in parameters or arguments'; |
211 | $status = 0; |
212 | break; |
213 | case 502: $message = 'Command not implemented'; |
214 | $status = 0; |
215 | break; |
216 | case 503: $message = 'Bad sequence of commands'; |
217 | $status = 0; |
218 | break; |
219 | case 504: $message = 'Command parameter not implemented'; |
220 | $status = 0; |
221 | break; |
222 | case 211: $message = 'System status, or system help reply'; |
223 | $status = 5; |
224 | break; |
225 | case 214: $message = 'Help message'; |
226 | $status = 5; |
227 | break; |
228 | case 220: $message = 'Service ready'; |
229 | $status = 5; |
230 | break; |
231 | case 221: $message = 'Service closing transmission channel'; |
232 | $status = 5; |
233 | break; |
47a29326 |
234 | case 421: $message = 'Service not available, closing channel'; |
5fe73b9f |
235 | $status = 0; |
236 | break; |
47a29326 |
237 | case 235: $message = 'Authentication successful'; |
238 | $status = 5; |
239 | break; |
5fe73b9f |
240 | case 250: $message = 'Requested mail action okay, completed'; |
241 | $status = 5; |
242 | break; |
243 | case 251: $message = 'User not local; will forward'; |
244 | $status = 5; |
245 | break; |
47a29326 |
246 | case 334: $message = 'OK - continue request'; |
247 | $status = 5; |
248 | break; |
5fe73b9f |
249 | case 450: $message = 'Requested mail action not taken: mailbox unavailable'; |
250 | $status = 0; |
251 | break; |
252 | case 550: $message = 'Requested action not taken: mailbox unavailable'; |
253 | $status = 0; |
254 | break; |
255 | case 451: $message = 'Requested action aborted: error in processing'; |
256 | $status = 0; |
257 | break; |
258 | case 551: $message = 'User not local; please try forwarding'; |
259 | $status = 0; |
260 | break; |
261 | case 452: $message = 'Requested action not taken: insufficient system storage'; |
262 | $status = 0; |
263 | break; |
264 | case 552: $message = 'Requested mail action aborted: exceeding storage allocation'; |
265 | $status = 0; |
266 | break; |
267 | case 553: $message = 'Requested action not taken: mailbox name not allowed'; |
268 | $status = 0; |
269 | break; |
270 | case 354: $message = 'Start mail input; end with .'; |
271 | $status = 5; |
272 | break; |
273 | case 554: $message = 'Transaction failed'; |
274 | $status = 0; |
275 | break; |
276 | /* RFC 2554 */ |
277 | case 432: $message = 'A password transition is needed'; |
278 | $status = 0; |
279 | break; |
280 | case 534: $message = 'Authentication mechanism is too weak'; |
281 | $status = 0; |
282 | break; |
283 | case 538: $message = 'Encryption required for requested authentication mechanism'; |
284 | $status = 0; |
285 | break; |
286 | case 454: $message = 'Temmporary authentication failure'; |
287 | $status = 0; |
288 | break; |
289 | case 530: $message = 'Authentication required'; |
290 | $status = 0; |
291 | break; |
292 | /* end RFC2554 */ |
47a29326 |
293 | case 535: $message = 'Authentication failed'; |
294 | $status = 0; |
295 | break; |
5fe73b9f |
296 | default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines)); |
297 | $status = 0; |
298 | $err_num = '001'; |
299 | break; |
300 | } |
301 | $this->dlv_ret_nr = $err_num; |
302 | $this->dlv_msg = $message; |
303 | if ($status == 5) { |
304 | return false; |
305 | } |
306 | return true; |
307 | } |
308 | |
309 | function authPop($pop_server='', $pop_port='', $user, $pass) { |
310 | if (!$pop_port) { |
311 | $pop_port = 110; |
312 | } |
313 | if (!$pop_server) { |
314 | $pop_server = 'localhost'; |
315 | } |
316 | $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str); |
317 | if (!$popConnection) { |
318 | error_log("Error connecting to POP Server ($pop_server:$pop_port)" |
319 | . " $err_no : $err_str"); |
320 | } else { |
321 | $tmp = fgets($popConnection, 1024); /* banner */ |
322 | if (!eregi("^\+OK", $tmp, $regs)) { |
323 | return(0); |
324 | } |
325 | fputs($popConnection, "USER $user\r\n"); |
326 | $tmp = fgets($popConnection, 1024); |
327 | if (!eregi("^\+OK", $tmp, $regs)) { |
328 | return(0); |
329 | } |
330 | fputs($popConnection, 'PASS ' . $pass . "\r\n"); |
331 | $tmp = fgets($popConnection, 1024); |
332 | if (!eregi("^\+OK", $tmp, $regs)) { |
333 | return(0); |
334 | } |
335 | fputs($popConnection, "QUIT\r\n"); /* log off */ |
336 | fclose($popConnection); |
337 | } |
338 | } |
e1ee60fe |
339 | } |
5fe73b9f |
340 | |
e1ee60fe |
341 | ?> |