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