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$ |
2b646597 |
11 | * @package squirrelmail |
5b8fd093 |
12 | */ |
e1ee60fe |
13 | |
2b646597 |
14 | /** This of course depends upon Deliver */ |
0f85ddf9 |
15 | require_once(SM_PATH . 'class/deliver/Deliver.class.php'); |
e1ee60fe |
16 | |
2b646597 |
17 | /** |
18 | * Deliver messages using SMTP |
19 | * @package squirrelmail |
20 | */ |
e1ee60fe |
21 | class Deliver_SMTP extends Deliver { |
22 | |
5fe73b9f |
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) { |
9bd3b1e6 |
31 | global $use_smtp_tls,$smtp_auth_mech; |
32 | |
47a29326 |
33 | if ($authpop) { |
9bd3b1e6 |
34 | $this->authPop($host, '', $user, $pass); |
5fe73b9f |
35 | } |
9bd3b1e6 |
36 | |
47a29326 |
37 | $rfc822_header = $message->rfc822_header; |
5fe73b9f |
38 | $from = $rfc822_header->from[0]; |
39 | $to = $rfc822_header->to; |
40 | $cc = $rfc822_header->cc; |
47a29326 |
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 | } |
5fe73b9f |
48 | |
5fe73b9f |
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 | } |
0c0e6024 |
58 | |
22bd1baf |
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 | */ |
0c0e6024 |
63 | if (sqgetGlobalVar('HTTP_HOST', $HTTP_HOST, SQ_SERVER)) { // HTTP_HOST is set |
22bd1baf |
64 | // optionally trim off port number |
65 | if($p = strrpos($HTTP_HOST, ':')) { |
66 | $HTTP_HOST = substr($HTTP_HOST, 0, $p); |
67 | } |
0c0e6024 |
68 | $helohost = $HTTP_HOST; |
69 | } else { // For some reason, HTTP_HOST is not set - revert to old behavior |
70 | $helohost = $domain; |
71 | } |
72 | |
5fe73b9f |
73 | /* Lets introduce ourselves */ |
33feaaec |
74 | fputs($stream, "EHLO $helohost\r\n"); |
75 | $tmp = fgets($stream,1024); |
76 | if ($this->errorCheck($tmp,$stream)) { |
77 | return(0); |
78 | } |
79 | |
639c7164 |
80 | if (( $smtp_auth_mech == 'cram-md5') or ( $smtp_auth_mech == 'digest-md5' )) { |
47a29326 |
81 | // Doing some form of non-plain auth |
47a29326 |
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 | } |
22bd1baf |
87 | |
47a29326 |
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') { |
9bd3b1e6 |
98 | $response = cram_md5_response($user,$pass,$chall); |
47a29326 |
99 | } elseif ($smtp_auth_mech == 'digest-md5') { |
9bd3b1e6 |
100 | $response = digest_md5_response($user,$pass,$chall,'smtp',$host); |
47a29326 |
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 |
09c2c717 |
124 | // We already said hi earlier, nothing more is needed. |
fe0b18b3 |
125 | } elseif ($smtp_auth_mech == 'login') { |
126 | // The LOGIN method |
47a29326 |
127 | fputs($stream, "AUTH LOGIN\r\n"); |
128 | $tmp = fgets($stream, 1024); |
5fe73b9f |
129 | |
47a29326 |
130 | if ($this->errorCheck($tmp, $stream)) { |
131 | return(0); |
132 | } |
9bd3b1e6 |
133 | fputs($stream, base64_encode ($user) . "\r\n"); |
47a29326 |
134 | $tmp = fgets($stream, 1024); |
135 | if ($this->errorCheck($tmp, $stream)) { |
136 | return(0); |
137 | } |
5fe73b9f |
138 | |
47a29326 |
139 | fputs($stream, base64_encode($pass) . "\r\n"); |
140 | $tmp = fgets($stream, 1024); |
141 | if ($this->errorCheck($tmp, $stream)) { |
142 | return(0); |
143 | } |
33feaaec |
144 | } elseif ($smtp_auth_mech == "plain") { |
145 | /* SASL Plain */ |
9bd3b1e6 |
146 | $auth = base64_encode("$user\0$user\0$pass"); |
33feaaec |
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 { |
aa358b7e |
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 | } |
5fe73b9f |
170 | |
171 | /* Ok, who is sending the message? */ |
064206f5 |
172 | fputs($stream, 'MAIL FROM:<'.$from->mailbox.'@'.$from->host.">\r\n"); |
5fe73b9f |
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; |
d1825c3c |
181 | if ($to[$i]->mailbox) { |
064206f5 |
182 | fputs($stream, 'RCPT TO:<'.$to[$i]->mailbox.'@'.$to[$i]->host.">\r\n"); |
d1825c3c |
183 | $tmp = fgets($stream, 1024); |
184 | if ($this->errorCheck($tmp, $stream)) { |
185 | return(0); |
186 | } |
5fe73b9f |
187 | } |
188 | } |
d1825c3c |
189 | |
5fe73b9f |
190 | for ($i = 0, $cnt = count($cc); $i < $cnt; $i++) { |
d1825c3c |
191 | if (!$cc[$i]->host) $cc[$i]->host = $domain; |
192 | if ($cc[$i]->mailbox) { |
064206f5 |
193 | fputs($stream, 'RCPT TO:<'.$cc[$i]->mailbox.'@'.$cc[$i]->host.">\r\n"); |
d1825c3c |
194 | $tmp = fgets($stream, 1024); |
195 | if ($this->errorCheck($tmp, $stream)) { |
196 | return(0); |
197 | } |
5fe73b9f |
198 | } |
199 | } |
22bd1baf |
200 | |
5fe73b9f |
201 | for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++) { |
d1825c3c |
202 | if (!$bcc[$i]->host) $bcc[$i]->host = $domain; |
203 | if ($bcc[$i]->mailbox) { |
064206f5 |
204 | fputs($stream, 'RCPT TO:<'.$bcc[$i]->mailbox.'@'.$bcc[$i]->host.">\r\n"); |
d1825c3c |
205 | $tmp = fgets($stream, 1024); |
206 | if ($this->errorCheck($tmp, $stream)) { |
207 | return(0); |
208 | } |
5fe73b9f |
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 | |
ca71b2db |
232 | /* check if an SMTP reply is an error and set an error message) */ |
5fe73b9f |
233 | function errorCheck($line, $smtpConnection) { |
ca71b2db |
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 | |
5fe73b9f |
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 */ |
22bd1baf |
313 | if (substr($tmp, 0, 3) != '+OK') { |
5fe73b9f |
314 | return(0); |
315 | } |
316 | fputs($popConnection, "USER $user\r\n"); |
317 | $tmp = fgets($popConnection, 1024); |
22bd1baf |
318 | if (substr($tmp, 0, 3) != '+OK') { |
5fe73b9f |
319 | return(0); |
320 | } |
321 | fputs($popConnection, 'PASS ' . $pass . "\r\n"); |
322 | $tmp = fgets($popConnection, 1024); |
22bd1baf |
323 | if (substr($tmp, 0, 3) != '+OK') { |
5fe73b9f |
324 | return(0); |
325 | } |
326 | fputs($popConnection, "QUIT\r\n"); /* log off */ |
327 | fclose($popConnection); |
328 | } |
329 | } |
e1ee60fe |
330 | } |
5fe73b9f |
331 | |
e1ee60fe |
332 | ?> |