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