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