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