3 * Deliver_SMTP.class.php
5 * Copyright (c) 1999-2005 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * Delivery backend for the Deliver class.
11 * @package squirrelmail
14 /** This of course depends upon Deliver */
15 require_once(SM_PATH
. 'class/deliver/Deliver.class.php');
18 * Deliver messages using SMTP
19 * @package squirrelmail
21 class Deliver_SMTP
extends Deliver
{
23 function preWriteToStream(&$s) {
25 if ($s{0} == '.') $s = '.' . $s;
26 $s = str_replace("\n.","\n..",$s);
30 function initStream($message, $domain, $length=0, $host='', $port='', $user='', $pass='', $authpop=false) {
31 global $use_smtp_tls,$smtp_auth_mech;
34 $this->authPop($host, '', $user, $pass);
37 $rfc822_header = $message->rfc822_header
;
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
;
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') {
54 if (($use_smtp_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) {
55 $stream = @fsockopen
('tls://' . $host, $port, $errorNumber, $errorString);
57 $stream = @fsockopen
($host, $port, $errorNumber, $errorString);
61 $this->dlv_msg
= $errorString;
62 $this->dlv_ret_nr
= $errorNumber;
63 $this->dlv_server_msg
= '';
66 $tmp = fgets($stream, 1024);
67 if ($this->errorCheck($tmp, $stream)) {
72 * If $_SERVER['HTTP_HOST'] is set, use that in our HELO to the SMTP
73 * server. This should fix the DNS issues some people have had
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);
80 $helohost = $HTTP_HOST;
81 } else { // For some reason, HTTP_HOST is not set - revert to old behavior
85 /* Lets introduce ourselves */
86 fputs($stream, "EHLO $helohost\r\n");
87 $tmp = fgets($stream,1024);
88 if ($this->errorCheck($tmp,$stream)) {
89 // fall back to HELO if EHLO is not supported
90 if ($this->dlv_ret_nr
== '500') {
91 fputs($stream, "HELO $helohost\r\n");
92 $tmp = fgets($stream,1024);
93 if ($this->errorCheck($tmp,$stream)) {
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");
109 $tmp = fgets($stream,1024);
111 if ($this->errorCheck($tmp,$stream)) {
115 // At this point, $tmp should hold "334 <challenge string>"
116 $chall = substr($tmp,4);
117 // Depending on mechanism, generate response string
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);
123 fputs($stream, $response);
125 // Let's see what the server had to say about that
126 $tmp = fgets($stream,1024);
127 if ($this->errorCheck($tmp,$stream)) {
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);
137 if ($this->errorCheck($tmp,$stream)) {
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') {
147 fputs($stream, "AUTH LOGIN\r\n");
148 $tmp = fgets($stream, 1024);
150 if ($this->errorCheck($tmp, $stream)) {
153 fputs($stream, base64_encode ($user) . "\r\n");
154 $tmp = fgets($stream, 1024);
155 if ($this->errorCheck($tmp, $stream)) {
159 fputs($stream, base64_encode($pass) . "\r\n");
160 $tmp = fgets($stream, 1024);
161 if ($this->errorCheck($tmp, $stream)) {
164 } elseif ($smtp_auth_mech == "plain") {
166 $auth = base64_encode("$user\0$user\0$pass");
168 $query = "AUTH PLAIN\r\n";
169 fputs($stream, $query);
170 $read=fgets($stream, 1024);
172 if (substr($read,0,3) == '334') { // OK so far..
173 fputs($stream, "$auth\r\n");
174 $read = fgets($stream, 1024);
177 $results=explode(" ",$read,3);
178 $response=$results[1];
179 $message=$results[2];
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)) {
189 /* Ok, who is sending the message? */
190 $fromaddress = ($from->mailbox
&& $from->host
) ?
191 $from->mailbox
.'@'.$from->host
: '';
192 fputs($stream, 'MAIL FROM:<'.$fromaddress.">\r\n");
193 $tmp = fgets($stream, 1024);
194 if ($this->errorCheck($tmp, $stream)) {
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)) {
210 for ($i = 0, $cnt = count($cc); $i < $cnt; $i++
) {
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)) {
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)) {
231 /* Lets start sending the actual message */
232 fputs($stream, "DATA\r\n");
233 $tmp = fgets($stream, 1024);
234 if ($this->errorCheck($tmp, $stream)) {
240 function finalizeStream($stream) {
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) {
247 fputs($stream, "QUIT\r\n"); /* log off */
252 /* check if an SMTP reply is an error and set an error message) */
253 function errorCheck($line, $smtpConnection) {
255 $err_num = substr($line, 0, 3);
256 $this->dlv_ret_nr
= $err_num;
257 $server_msg = substr($line, 4);
259 while(substr($line, 0, 4) == ($err_num.'-')) {
260 $line = fgets($smtpConnection, 1024);
261 $server_msg .= substr($line, 4);
264 if ( ((int) $err_num{0}) < 4) {
269 case '421': $message = _("Service not available, closing channel");
271 case '432': $message = _("A password transition is needed");
273 case '450': $message = _("Requested mail action not taken: mailbox unavailable");
275 case '451': $message = _("Requested action aborted: error in processing");
277 case '452': $message = _("Requested action not taken: insufficient system storage");
279 case '454': $message = _("Temporary authentication failure");
281 case '500': $message = _("Syntax error; command not recognized");
283 case '501': $message = _("Syntax error in parameters or arguments");
285 case '502': $message = _("Command not implemented");
287 case '503': $message = _("Bad sequence of commands");
289 case '504': $message = _("Command parameter not implemented");
291 case '530': $message = _("Authentication required");
293 case '534': $message = _("Authentication mechanism is too weak");
295 case '535': $message = _("Authentication failed");
297 case '538': $message = _("Encryption required for requested authentication mechanism");
299 case '550': $message = _("Requested action not taken: mailbox unavailable");
301 case '551': $message = _("User not local; please try forwarding");
303 case '552': $message = _("Requested mail action aborted: exceeding storage allocation");
305 case '553': $message = _("Requested action not taken: mailbox name not allowed");
307 case '554': $message = _("Transaction failed");
309 default: $message = _("Unknown response");
313 $this->dlv_msg
= $message;
314 $this->dlv_server_msg
= nl2br(htmlspecialchars($server_msg));
319 function authPop($pop_server='', $pop_port='', $user, $pass) {
324 $pop_server = 'localhost';
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)"
329 . " $err_no : $err_str");
331 $tmp = fgets($popConnection, 1024); /* banner */
332 if (substr($tmp, 0, 3) != '+OK') {
335 fputs($popConnection, "USER $user\r\n");
336 $tmp = fgets($popConnection, 1024);
337 if (substr($tmp, 0, 3) != '+OK') {
340 fputs($popConnection, 'PASS ' . $pass . "\r\n");
341 $tmp = fgets($popConnection, 1024);
342 if (substr($tmp, 0, 3) != '+OK') {
345 fputs($popConnection, "QUIT\r\n"); /* log off */
346 fclose($popConnection);