bf2cb2ec877944c3da2c5d95b6db31c06630116d
[squirrelmail.git] / class / deliver / Deliver_SMTP.class.php
1 <?php
2 /**
3 * Deliver_SMTP.class.php
4 *
5 * Copyright (c) 1999-2004 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 * @package squirrelmail
12 */
13
14 /** This of course depends upon Deliver */
15 require_once(SM_PATH . 'class/deliver/Deliver.class.php');
16
17 /**
18 * Deliver messages using SMTP
19 * @package squirrelmail
20 */
21 class Deliver_SMTP extends Deliver {
22
23 function preWriteToStream(&$s) {
24 if ($s) {
25 if ($s{0} == '.') $s = '.' . $s;
26 $s = str_replace("\n.","\n..",$s);
27 }
28 }
29
30 function initStream($message, $domain, $length=0, $host='', $port='', $user='', $pass='', $authpop=false) {
31 global $use_smtp_tls,$smtp_auth_mech;
32
33 if ($authpop) {
34 $this->authPop($host, '', $user, $pass);
35 }
36
37 $rfc822_header = $message->rfc822_header;
38 $from = $rfc822_header->from[0];
39 $to = $rfc822_header->to;
40 $cc = $rfc822_header->cc;
41 $bcc = $rfc822_header->bcc;
42
43 if (($use_smtp_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) {
44 $stream = fsockopen('tls://' . $host, $port, $errorNumber, $errorString);
45 } else {
46 $stream = fsockopen($host, $port, $errorNumber, $errorString);
47 }
48
49 if (!$stream) {
50 $this->dlv_msg = $errorString;
51 $this->dlv_ret_nr = $errorNumber;
52 return(0);
53 }
54 $tmp = fgets($stream, 1024);
55 if ($this->errorCheck($tmp, $stream)) {
56 return(0);
57 }
58
59 /*
60 * If $_SERVER['HTTP_HOST'] is set, use that in our HELO to the SMTP
61 * server. This should fix the DNS issues some people have had
62 */
63 if (sqgetGlobalVar('HTTP_HOST', $HTTP_HOST, SQ_SERVER)) { // HTTP_HOST is set
64 // optionally trim off port number
65 if($p = strrpos($HTTP_HOST, ':')) {
66 $HTTP_HOST = substr($HTTP_HOST, 0, $p);
67 }
68 $helohost = $HTTP_HOST;
69 } else { // For some reason, HTTP_HOST is not set - revert to old behavior
70 $helohost = $domain;
71 }
72
73 /* Lets introduce ourselves */
74 fputs($stream, "EHLO $helohost\r\n");
75 $tmp = fgets($stream,1024);
76 if ($this->errorCheck($tmp,$stream)) {
77 return(0);
78 }
79
80 if (( $smtp_auth_mech == 'cram-md5') or ( $smtp_auth_mech == 'digest-md5' )) {
81 // Doing some form of non-plain auth
82 if ($smtp_auth_mech == 'cram-md5') {
83 fputs($stream, "AUTH CRAM-MD5\r\n");
84 } elseif ($smtp_auth_mech == 'digest-md5') {
85 fputs($stream, "AUTH DIGEST-MD5\r\n");
86 }
87
88 $tmp = fgets($stream,1024);
89
90 if ($this->errorCheck($tmp,$stream)) {
91 return(0);
92 }
93
94 // At this point, $tmp should hold "334 <challenge string>"
95 $chall = substr($tmp,4);
96 // Depending on mechanism, generate response string
97 if ($smtp_auth_mech == 'cram-md5') {
98 $response = cram_md5_response($user,$pass,$chall);
99 } elseif ($smtp_auth_mech == 'digest-md5') {
100 $response = digest_md5_response($user,$pass,$chall,'smtp',$host);
101 }
102 fputs($stream, $response);
103
104 // Let's see what the server had to say about that
105 $tmp = fgets($stream,1024);
106 if ($this->errorCheck($tmp,$stream)) {
107 return(0);
108 }
109
110 // CRAM-MD5 is done at this point. If DIGEST-MD5, there's a bit more to go
111 if ($smtp_auth_mech == 'digest-md5')
112 {
113 // $tmp contains rspauth, but I don't store that yet. (No need yet)
114 fputs($stream,"\r\n");
115 $tmp = fgets($stream,1024);
116
117 if ($this->errorCheck($tmp,$stream)) {
118 return(0);
119 }
120 }
121 // CRAM-MD5 and DIGEST-MD5 code ends here
122 } elseif ($smtp_auth_mech == 'none') {
123 // No auth at all, just send helo and then send the mail
124 // We already said hi earlier, nothing more is needed.
125 } elseif ($smtp_auth_mech == 'login') {
126 // The LOGIN method
127 fputs($stream, "AUTH LOGIN\r\n");
128 $tmp = fgets($stream, 1024);
129
130 if ($this->errorCheck($tmp, $stream)) {
131 return(0);
132 }
133 fputs($stream, base64_encode ($user) . "\r\n");
134 $tmp = fgets($stream, 1024);
135 if ($this->errorCheck($tmp, $stream)) {
136 return(0);
137 }
138
139 fputs($stream, base64_encode($pass) . "\r\n");
140 $tmp = fgets($stream, 1024);
141 if ($this->errorCheck($tmp, $stream)) {
142 return(0);
143 }
144 } elseif ($smtp_auth_mech == "plain") {
145 /* SASL Plain */
146 $auth = base64_encode("$user\0$user\0$pass");
147
148 $query = "AUTH PLAIN\r\n";
149 fputs($stream, $query);
150 $read=fgets($stream, 1024);
151
152 if (substr($read,0,3) == '334') { // OK so far..
153 fputs($stream, "$auth\r\n");
154 $read = fgets($stream, 1024);
155 }
156
157 $results=explode(" ",$read,3);
158 $response=$results[1];
159 $message=$results[2];
160
161
162 } else {
163 /* Right here, they've reached an unsupported auth mechanism.
164 This is the ugliest hack I've ever done, but it'll do till I can fix
165 things up better tomorrow. So tired... */
166 if ($this->errorCheck("535 Unable to use this auth type",$stream)) {
167 return(0);
168 }
169 }
170
171 /* Ok, who is sending the message? */
172 fputs($stream, 'MAIL FROM:<'.$from->mailbox.'@'.$from->host.">\r\n");
173 $tmp = fgets($stream, 1024);
174 if ($this->errorCheck($tmp, $stream)) {
175 return(0);
176 }
177
178 /* send who the recipients are */
179 for ($i = 0, $cnt = count($to); $i < $cnt; $i++) {
180 if (!$to[$i]->host) $to[$i]->host = $domain;
181 if ($to[$i]->mailbox) {
182 fputs($stream, 'RCPT TO:<'.$to[$i]->mailbox.'@'.$to[$i]->host.">\r\n");
183 $tmp = fgets($stream, 1024);
184 if ($this->errorCheck($tmp, $stream)) {
185 return(0);
186 }
187 }
188 }
189
190 for ($i = 0, $cnt = count($cc); $i < $cnt; $i++) {
191 if (!$cc[$i]->host) $cc[$i]->host = $domain;
192 if ($cc[$i]->mailbox) {
193 fputs($stream, 'RCPT TO:<'.$cc[$i]->mailbox.'@'.$cc[$i]->host.">\r\n");
194 $tmp = fgets($stream, 1024);
195 if ($this->errorCheck($tmp, $stream)) {
196 return(0);
197 }
198 }
199 }
200
201 for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++) {
202 if (!$bcc[$i]->host) $bcc[$i]->host = $domain;
203 if ($bcc[$i]->mailbox) {
204 fputs($stream, 'RCPT TO:<'.$bcc[$i]->mailbox.'@'.$bcc[$i]->host.">\r\n");
205 $tmp = fgets($stream, 1024);
206 if ($this->errorCheck($tmp, $stream)) {
207 return(0);
208 }
209 }
210 }
211 /* Lets start sending the actual message */
212 fputs($stream, "DATA\r\n");
213 $tmp = fgets($stream, 1024);
214 if ($this->errorCheck($tmp, $stream)) {
215 return(0);
216 }
217 return $stream;
218 }
219
220 function finalizeStream($stream) {
221 fputs($stream, ".\r\n"); /* end the DATA part */
222 $tmp = fgets($stream, 1024);
223 $this->errorCheck($tmp, $stream);
224 if ($this->dlv_ret_nr != 250) {
225 return(0);
226 }
227 fputs($stream, "QUIT\r\n"); /* log off */
228 fclose($stream);
229 return true;
230 }
231
232 /* check if an SMTP reply is an error and set an error message) */
233 function errorCheck($line, $smtpConnection) {
234
235 $err_num = substr($line, 0, 3);
236 $this->dlv_ret_nr = $err_num;
237 $server_msg = substr($line, 4);
238
239 while(substr($line, 0, 4) == ($err_num.'-')) {
240 $line = fgets($smtpConnection, 1024);
241 $server_msg .= substr($line, 4);
242 }
243
244 if ( ((int) $err_num{0}) < 4)
245 {
246 return false;
247 }
248
249 switch ($err_num) {
250 case '421': $message = _("Service not available, closing channel");
251 break;
252 case '432': $message = _("A password transition is needed");
253 break;
254 case '450': $message = _("Requested mail action not taken: mailbox unavailable");
255 break;
256 case '451': $message = _("Requested action aborted: error in processing");
257 break;
258 case '452': $message = _("Requested action not taken: insufficient system storage");
259 break;
260 case '454': $message = _("Temporary authentication failure");
261 break;
262 case '500': $message = _("Syntax error; command not recognized");
263 break;
264 case '501': $message = _("Syntax error in parameters or arguments");
265 break;
266 case '502': $message = _("Command not implemented");
267 break;
268 case '503': $message = _("Bad sequence of commands");
269 break;
270 case '504': $message = _("Command parameter not implemented");
271 break;
272 case '530': $message = _("Authentication required");
273 break;
274 case '534': $message = _("Authentication mechanism is too weak");
275 break;
276 case '535': $message = _("Authentication failed");
277 break;
278 case '538': $message = _("Encryption required for requested authentication mechanism");
279 break;
280 case '550': $message = _("Requested action not taken: mailbox unavailable");
281 break;
282 case '551': $message = _("User not local; please try forwarding");
283 break;
284 case '552': $message = _("Requested mail action aborted: exceeding storage allocation");
285 break;
286 case '553': $message = _("Requested action not taken: mailbox name not allowed");
287 break;
288 case '554': $message = _("Transaction failed");
289 break;
290 default: $message = _("Unknown response");
291 break;
292 }
293
294 $this->dlv_msg = $message;
295 $this->dlv_server_msg = nl2br(htmlspecialchars($server_msg));
296
297 return true;
298 }
299
300 function authPop($pop_server='', $pop_port='', $user, $pass) {
301 if (!$pop_port) {
302 $pop_port = 110;
303 }
304 if (!$pop_server) {
305 $pop_server = 'localhost';
306 }
307 $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str);
308 if (!$popConnection) {
309 error_log("Error connecting to POP Server ($pop_server:$pop_port)"
310 . " $err_no : $err_str");
311 } else {
312 $tmp = fgets($popConnection, 1024); /* banner */
313 if (substr($tmp, 0, 3) != '+OK') {
314 return(0);
315 }
316 fputs($popConnection, "USER $user\r\n");
317 $tmp = fgets($popConnection, 1024);
318 if (substr($tmp, 0, 3) != '+OK') {
319 return(0);
320 }
321 fputs($popConnection, 'PASS ' . $pass . "\r\n");
322 $tmp = fgets($popConnection, 1024);
323 if (substr($tmp, 0, 3) != '+OK') {
324 return(0);
325 }
326 fputs($popConnection, "QUIT\r\n"); /* log off */
327 fclose($popConnection);
328 }
329 }
330 }
331
332 ?>