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