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
;
38 $from = $rfc822_header->from
[0];
39 $to = $rfc822_header->to
;
40 $cc = $rfc822_header->cc
;
41 $bcc = $rfc822_header->bcc
;
43 if (($use_smtp_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) {
44 $stream = fsockopen('tls://' . $host, $port, $errorNumber, $errorString);
46 $stream = fsockopen($host, $port, $errorNumber, $errorString);
50 $this->dlv_msg
= $errorString;
51 $this->dlv_ret_nr
= $errorNumber;
54 $tmp = fgets($stream, 1024);
55 if ($this->errorCheck($tmp, $stream)) {
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
63 if (sqgetGlobalVar('HTTP_HOST', $HTTP_HOST, SQ_SERVER
)) { // HTTP_HOST is set
64 // optionally trim off port number
65 if($p = strrpos($HTTP_HOST, ':')) {
66 $HTTP_HOST = substr($HTTP_HOST, 0, $p);
68 $helohost = $HTTP_HOST;
69 } else { // For some reason, HTTP_HOST is not set - revert to old behavior
73 /* Lets introduce ourselves */
74 fputs($stream, "EHLO $helohost\r\n");
75 $tmp = fgets($stream,1024);
76 if ($this->errorCheck($tmp,$stream)) {
80 if (( $smtp_auth_mech == 'cram-md5') or ( $smtp_auth_mech == 'digest-md5' )) {
81 // Doing some form of non-plain auth
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");
88 $tmp = fgets($stream,1024);
90 if ($this->errorCheck($tmp,$stream)) {
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') {
98 $response = cram_md5_response($user,$pass,$chall);
99 } elseif ($smtp_auth_mech == 'digest-md5') {
100 $response = digest_md5_response($user,$pass,$chall,'smtp',$host);
102 fputs($stream, $response);
104 // Let's see what the server had to say about that
105 $tmp = fgets($stream,1024);
106 if ($this->errorCheck($tmp,$stream)) {
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')
113 // $tmp contains rspauth, but I don't store that yet. (No need yet)
114 fputs($stream,"\r\n");
115 $tmp = fgets($stream,1024);
117 if ($this->errorCheck($tmp,$stream)) {
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
124 // We already said hi earlier, nothing more is needed.
125 } elseif ($smtp_auth_mech == 'login') {
127 fputs($stream, "AUTH LOGIN\r\n");
128 $tmp = fgets($stream, 1024);
130 if ($this->errorCheck($tmp, $stream)) {
133 fputs($stream, base64_encode ($user) . "\r\n");
134 $tmp = fgets($stream, 1024);
135 if ($this->errorCheck($tmp, $stream)) {
139 fputs($stream, base64_encode($pass) . "\r\n");
140 $tmp = fgets($stream, 1024);
141 if ($this->errorCheck($tmp, $stream)) {
144 } elseif ($smtp_auth_mech == "plain") {
146 $auth = base64_encode("$user\0$user\0$pass");
148 $query = "AUTH PLAIN\r\n";
149 fputs($stream, $query);
150 $read=fgets($stream, 1024);
152 if (substr($read,0,3) == '334') { // OK so far..
153 fputs($stream, "$auth\r\n");
154 $read = fgets($stream, 1024);
157 $results=explode(" ",$read,3);
158 $response=$results[1];
159 $message=$results[2];
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)) {
171 /* Ok, who is sending the message? */
172 fputs($stream, 'MAIL FROM:<'.$from->mailbox
.'@'.$from->host
.">\r\n");
173 $tmp = fgets($stream, 1024);
174 if ($this->errorCheck($tmp, $stream)) {
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;
181 if ($to[$i]->mailbox
) {
182 fputs($stream, 'RCPT TO:<'.$to[$i]->mailbox
.'@'.$to[$i]->host
.">\r\n");
183 $tmp = fgets($stream, 1024);
184 if ($this->errorCheck($tmp, $stream)) {
190 for ($i = 0, $cnt = count($cc); $i < $cnt; $i++
) {
191 if (!$cc[$i]->host
) $cc[$i]->host
= $domain;
192 if ($cc[$i]->mailbox
) {
193 fputs($stream, 'RCPT TO:<'.$cc[$i]->mailbox
.'@'.$cc[$i]->host
.">\r\n");
194 $tmp = fgets($stream, 1024);
195 if ($this->errorCheck($tmp, $stream)) {
201 for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++
) {
202 if (!$bcc[$i]->host
) $bcc[$i]->host
= $domain;
203 if ($bcc[$i]->mailbox
) {
204 fputs($stream, 'RCPT TO:<'.$bcc[$i]->mailbox
.'@'.$bcc[$i]->host
.">\r\n");
205 $tmp = fgets($stream, 1024);
206 if ($this->errorCheck($tmp, $stream)) {
211 /* Lets start sending the actual message */
212 fputs($stream, "DATA\r\n");
213 $tmp = fgets($stream, 1024);
214 if ($this->errorCheck($tmp, $stream)) {
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) {
227 fputs($stream, "QUIT\r\n"); /* log off */
232 /* check if an SMTP reply is an error and set an error message) */
233 function errorCheck($line, $smtpConnection) {
235 $err_num = substr($line, 0, 3);
236 $this->dlv_ret_nr
= $err_num;
237 $server_msg = substr($line, 4);
239 while(substr($line, 0, 4) == ($err_num.'-')) {
240 $line = fgets($smtpConnection, 1024);
241 $server_msg .= substr($line, 4);
244 if ( ((int) $err_num{0}) < 4)
250 case '421': $message = _("Service not available, closing channel");
252 case '432': $message = _("A password transition is needed");
254 case '450': $message = _("Requested mail action not taken: mailbox unavailable");
256 case '451': $message = _("Requested action aborted: error in processing");
258 case '452': $message = _("Requested action not taken: insufficient system storage");
260 case '454': $message = _("Temporary authentication failure");
262 case '500': $message = _("Syntax error; command not recognized");
264 case '501': $message = _("Syntax error in parameters or arguments");
266 case '502': $message = _("Command not implemented");
268 case '503': $message = _("Bad sequence of commands");
270 case '504': $message = _("Command parameter not implemented");
272 case '530': $message = _("Authentication required");
274 case '534': $message = _("Authentication mechanism is too weak");
276 case '535': $message = _("Authentication failed");
278 case '538': $message = _("Encryption required for requested authentication mechanism");
280 case '550': $message = _("Requested action not taken: mailbox unavailable");
282 case '551': $message = _("User not local; please try forwarding");
284 case '552': $message = _("Requested mail action aborted: exceeding storage allocation");
286 case '553': $message = _("Requested action not taken: mailbox name not allowed");
288 case '554': $message = _("Transaction failed");
290 default: $message = _("Unknown response");
294 $this->dlv_msg
= $message;
295 $this->dlv_server_msg
= nl2br(htmlspecialchars($server_msg));
300 function authPop($pop_server='', $pop_port='', $user, $pass) {
305 $pop_server = 'localhost';
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");
312 $tmp = fgets($popConnection, 1024); /* banner */
313 if (substr($tmp, 0, 3) != '+OK') {
316 fputs($popConnection, "USER $user\r\n");
317 $tmp = fgets($popConnection, 1024);
318 if (substr($tmp, 0, 3) != '+OK') {
321 fputs($popConnection, 'PASS ' . $pass . "\r\n");
322 $tmp = fgets($popConnection, 1024);
323 if (substr($tmp, 0, 3) != '+OK') {
326 fputs($popConnection, "QUIT\r\n"); /* log off */
327 fclose($popConnection);