Rewrite of errorCheck function, to make it: correct, more detailed and
[squirrelmail.git] / class / deliver / Deliver_SMTP.class.php
1 <?php
2 /**
3 * Deliver_SMTP.class.php
4 *
5 * Copyright (c) 1999-2003 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' )) {
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 == 'login') {
108 // The 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);
116
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 }
125
126 fputs($stream, base64_encode($pass) . "\r\n");
127 $tmp = fgets($stream, 1024);
128 if ($this->errorCheck($tmp, $stream)) {
129 return(0);
130 }
131 } else {
132 /* Right here, they've reached an unsupported auth mechanism.
133 This is the ugliest hack I've ever done, but it'll do till I can fix
134 things up better tomorrow. So tired... */
135 if ($this->errorCheck("535 Unable to use this auth type",$stream)) {
136 return(0);
137 }
138 }
139
140 /* Ok, who is sending the message? */
141 fputs($stream, 'MAIL FROM: <'.$from->mailbox.'@'.$from->host.">\r\n");
142 $tmp = fgets($stream, 1024);
143 if ($this->errorCheck($tmp, $stream)) {
144 return(0);
145 }
146
147 /* send who the recipients are */
148 for ($i = 0, $cnt = count($to); $i < $cnt; $i++) {
149 if (!$to[$i]->host) $to[$i]->host = $domain;
150 if ($to[$i]->mailbox) {
151 fputs($stream, 'RCPT TO: <'.$to[$i]->mailbox.'@'.$to[$i]->host.">\r\n");
152 $tmp = fgets($stream, 1024);
153 if ($this->errorCheck($tmp, $stream)) {
154 return(0);
155 }
156 }
157 }
158
159 for ($i = 0, $cnt = count($cc); $i < $cnt; $i++) {
160 if (!$cc[$i]->host) $cc[$i]->host = $domain;
161 if ($cc[$i]->mailbox) {
162 fputs($stream, 'RCPT TO: <'.$cc[$i]->mailbox.'@'.$cc[$i]->host.">\r\n");
163 $tmp = fgets($stream, 1024);
164 if ($this->errorCheck($tmp, $stream)) {
165 return(0);
166 }
167 }
168 }
169 for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++) {
170 if (!$bcc[$i]->host) $bcc[$i]->host = $domain;
171 if ($bcc[$i]->mailbox) {
172 fputs($stream, 'RCPT TO: <'.$bcc[$i]->mailbox.'@'.$bcc[$i]->host.">\r\n");
173 $tmp = fgets($stream, 1024);
174 if ($this->errorCheck($tmp, $stream)) {
175 return(0);
176 }
177 }
178 }
179 /* Lets start sending the actual message */
180 fputs($stream, "DATA\r\n");
181 $tmp = fgets($stream, 1024);
182 if ($this->errorCheck($tmp, $stream)) {
183 return(0);
184 }
185 return $stream;
186 }
187
188 function finalizeStream($stream) {
189 fputs($stream, ".\r\n"); /* end the DATA part */
190 $tmp = fgets($stream, 1024);
191 $this->errorCheck($tmp, $stream);
192 if ($this->dlv_ret_nr != 250) {
193 return(0);
194 }
195 fputs($stream, "QUIT\r\n"); /* log off */
196 fclose($stream);
197 return true;
198 }
199
200 /* check if an SMTP reply is an error and set an error message) */
201 function errorCheck($line, $smtpConnection) {
202
203 $err_num = substr($line, 0, 3);
204 $this->dlv_ret_nr = $err_num;
205 $server_msg = substr($line, 4);
206
207 while(substr($line, 0, 4) == ($err_num.'-')) {
208 $line = fgets($smtpConnection, 1024);
209 $server_msg .= substr($line, 4);
210 }
211
212 if ( ((int) $err_num{0}) < 4)
213 {
214 return false;
215 }
216
217 switch ($err_num) {
218 case '421': $message = _("Service not available, closing channel");
219 break;
220 case '432': $message = _("A password transition is needed");
221 break;
222 case '450': $message = _("Requested mail action not taken: mailbox unavailable");
223 break;
224 case '451': $message = _("Requested action aborted: error in processing");
225 break;
226 case '452': $message = _("Requested action not taken: insufficient system storage");
227 break;
228 case '454': $message = _("Temporary authentication failure");
229 break;
230 case '500': $message = _("Syntax error; command not recognized");
231 break;
232 case '501': $message = _("Syntax error in parameters or arguments");
233 break;
234 case '502': $message = _("Command not implemented");
235 break;
236 case '503': $message = _("Bad sequence of commands");
237 break;
238 case '504': $message = _("Command parameter not implemented");
239 break;
240 case '530': $message = _("Authentication required");
241 break;
242 case '534': $message = _("Authentication mechanism is too weak");
243 break;
244 case '535': $message = _("Authentication failed");
245 break;
246 case '538': $message = _("Encryption required for requested authentication mechanism");
247 break;
248 case '550': $message = _("Requested action not taken: mailbox unavailable");
249 break;
250 case '551': $message = _("User not local; please try forwarding");
251 break;
252 case '552': $message = _("Requested mail action aborted: exceeding storage allocation");
253 break;
254 case '553': $message = _("Requested action not taken: mailbox name not allowed");
255 break;
256 case '554': $message = _("Transaction failed");
257 break;
258 default: $message = _("Unknown response");
259 break;
260 }
261
262 $this->dlv_msg = $message;
263 $this->dlv_server_msg = nl2br(htmlspecialchars($server_msg));
264
265 return true;
266 }
267
268 function authPop($pop_server='', $pop_port='', $user, $pass) {
269 if (!$pop_port) {
270 $pop_port = 110;
271 }
272 if (!$pop_server) {
273 $pop_server = 'localhost';
274 }
275 $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str);
276 if (!$popConnection) {
277 error_log("Error connecting to POP Server ($pop_server:$pop_port)"
278 . " $err_no : $err_str");
279 } else {
280 $tmp = fgets($popConnection, 1024); /* banner */
281 if (!eregi("^\+OK", $tmp, $regs)) {
282 return(0);
283 }
284 fputs($popConnection, "USER $user\r\n");
285 $tmp = fgets($popConnection, 1024);
286 if (!eregi("^\+OK", $tmp, $regs)) {
287 return(0);
288 }
289 fputs($popConnection, 'PASS ' . $pass . "\r\n");
290 $tmp = fgets($popConnection, 1024);
291 if (!eregi("^\+OK", $tmp, $regs)) {
292 return(0);
293 }
294 fputs($popConnection, "QUIT\r\n"); /* log off */
295 fclose($popConnection);
296 }
297 }
298 }
299
300 ?>