f2e980ee6fbddcec4848571cd115d406d2223911
3 * Deliver_SMTP.class.php
5 * Copyright (c) 1999-2004 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;
65 $tmp = fgets($stream, 1024);
66 if ($this->errorCheck($tmp, $stream)) {
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
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);
79 $helohost = $HTTP_HOST;
80 } else { // For some reason, HTTP_HOST is not set - revert to old behavior
84 /* Lets introduce ourselves */
85 fputs($stream, "EHLO $helohost\r\n");
86 $tmp = fgets($stream,1024);
87 if ($this->errorCheck($tmp,$stream)) {
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");
99 $tmp = fgets($stream,1024);
101 if ($this->errorCheck($tmp,$stream)) {
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);
113 fputs($stream, $response);
115 // Let's see what the server had to say about that
116 $tmp = fgets($stream,1024);
117 if ($this->errorCheck($tmp,$stream)) {
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);
127 if ($this->errorCheck($tmp,$stream)) {
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') {
137 fputs($stream, "AUTH LOGIN\r\n");
138 $tmp = fgets($stream, 1024);
140 if ($this->errorCheck($tmp, $stream)) {
143 fputs($stream, base64_encode ($user) . "\r\n");
144 $tmp = fgets($stream, 1024);
145 if ($this->errorCheck($tmp, $stream)) {
149 fputs($stream, base64_encode($pass) . "\r\n");
150 $tmp = fgets($stream, 1024);
151 if ($this->errorCheck($tmp, $stream)) {
154 } elseif ($smtp_auth_mech == "plain") {
156 $auth = base64_encode("$user\0$user\0$pass");
158 $query = "AUTH PLAIN\r\n";
159 fputs($stream, $query);
160 $read=fgets($stream, 1024);
162 if (substr($read,0,3) == '334') { // OK so far..
163 fputs($stream, "$auth\r\n");
164 $read = fgets($stream, 1024);
167 $results=explode(" ",$read,3);
168 $response=$results[1];
169 $message=$results[2];
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)) {
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)) {
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)) {
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)) {
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)) {
221 /* Lets start sending the actual message */
222 fputs($stream, "DATA\r\n");
223 $tmp = fgets($stream, 1024);
224 if ($this->errorCheck($tmp, $stream)) {
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) {
237 fputs($stream, "QUIT\r\n"); /* log off */
242 /* check if an SMTP reply is an error and set an error message) */
243 function errorCheck($line, $smtpConnection) {
245 $err_num = substr($line, 0, 3);
246 $this->dlv_ret_nr
= $err_num;
247 $server_msg = substr($line, 4);
249 while(substr($line, 0, 4) == ($err_num.'-')) {
250 $line = fgets($smtpConnection, 1024);
251 $server_msg .= substr($line, 4);
254 if ( ((int) $err_num{0}) < 4) {
259 case '421': $message = _("Service not available, closing channel");
261 case '432': $message = _("A password transition is needed");
263 case '450': $message = _("Requested mail action not taken: mailbox unavailable");
265 case '451': $message = _("Requested action aborted: error in processing");
267 case '452': $message = _("Requested action not taken: insufficient system storage");
269 case '454': $message = _("Temporary authentication failure");
271 case '500': $message = _("Syntax error; command not recognized");
273 case '501': $message = _("Syntax error in parameters or arguments");
275 case '502': $message = _("Command not implemented");
277 case '503': $message = _("Bad sequence of commands");
279 case '504': $message = _("Command parameter not implemented");
281 case '530': $message = _("Authentication required");
283 case '534': $message = _("Authentication mechanism is too weak");
285 case '535': $message = _("Authentication failed");
287 case '538': $message = _("Encryption required for requested authentication mechanism");
289 case '550': $message = _("Requested action not taken: mailbox unavailable");
291 case '551': $message = _("User not local; please try forwarding");
293 case '552': $message = _("Requested mail action aborted: exceeding storage allocation");
295 case '553': $message = _("Requested action not taken: mailbox name not allowed");
297 case '554': $message = _("Transaction failed");
299 default: $message = _("Unknown response");
303 $this->dlv_msg
= $message;
304 $this->dlv_server_msg
= nl2br(htmlspecialchars($server_msg));
309 function authPop($pop_server='', $pop_port='', $user, $pass) {
314 $pop_server = 'localhost';
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");
321 $tmp = fgets($popConnection, 1024); /* banner */
322 if (substr($tmp, 0, 3) != '+OK') {
325 fputs($popConnection, "USER $user\r\n");
326 $tmp = fgets($popConnection, 1024);
327 if (substr($tmp, 0, 3) != '+OK') {
330 fputs($popConnection, 'PASS ' . $pass . "\r\n");
331 $tmp = fgets($popConnection, 1024);
332 if (substr($tmp, 0, 3) != '+OK') {
335 fputs($popConnection, "QUIT\r\n"); /* log off */
336 fclose($popConnection);