3 * Deliver_SMTP.class.php
5 * Copyright (c) 1999-2003 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * Delivery backend for the Deliver class.
13 require_once(SM_PATH
. 'class/deliver/Deliver.class.php');
15 class Deliver_SMTP
extends Deliver
{
17 function preWriteToStream(&$s) {
19 if ($s{0} == '.') $s = '.' . $s;
20 $s = str_replace("\n.","\n..",$s);
24 function initStream($message, $domain, $length=0, $host='', $port='', $user='', $pass='', $authpop=false) {
25 global $use_smtp_tls,$smtp_auth_mech,$username,$key,$onetimepad;
28 $this->authPop($host, '', $username, $pass);
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
;
37 if (($use_smtp_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) {
38 $stream = fsockopen('tls://' . $host, $port, $errorNumber, $errorString);
40 $stream = fsockopen($host, $port, $errorNumber, $errorString);
44 $this->dlv_msg
= $errorString;
45 $this->dlv_ret_nr
= $errorNumber;
48 $tmp = fgets($stream, 1024);
49 if ($this->errorCheck($tmp, $stream)) {
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
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);
62 $helohost = $HTTP_HOST;
63 } else { // For some reason, HTTP_HOST is not set - revert to old behavior
67 /* Lets introduce ourselves */
68 fputs($stream, "EHLO $helohost\r\n");
69 $tmp = fgets($stream,1024);
70 if ($this->errorCheck($tmp,$stream)) {
74 if (( $smtp_auth_mech == 'cram-md5') or ( $smtp_auth_mech == 'digest-md5' )) {
75 // Doing some form of non-plain auth
76 if ($smtp_auth_mech == 'cram-md5') {
77 fputs($stream, "AUTH CRAM-MD5\r\n");
78 } elseif ($smtp_auth_mech == 'digest-md5') {
79 fputs($stream, "AUTH DIGEST-MD5\r\n");
82 $tmp = fgets($stream,1024);
84 if ($this->errorCheck($tmp,$stream)) {
88 // At this point, $tmp should hold "334 <challenge string>"
89 $chall = substr($tmp,4);
90 // Depending on mechanism, generate response string
91 if ($smtp_auth_mech == 'cram-md5') {
92 $response = cram_md5_response($username,$pass,$chall);
93 } elseif ($smtp_auth_mech == 'digest-md5') {
94 $response = digest_md5_response($username,$pass,$chall,'smtp',$host);
96 fputs($stream, $response);
98 // Let's see what the server had to say about that
99 $tmp = fgets($stream,1024);
100 if ($this->errorCheck($tmp,$stream)) {
104 // CRAM-MD5 is done at this point. If DIGEST-MD5, there's a bit more to go
105 if ($smtp_auth_mech == 'digest-md5')
107 // $tmp contains rspauth, but I don't store that yet. (No need yet)
108 fputs($stream,"\r\n");
109 $tmp = fgets($stream,1024);
111 if ($this->errorCheck($tmp,$stream)) {
115 // CRAM-MD5 and DIGEST-MD5 code ends here
116 } elseif ($smtp_auth_mech == 'none') {
117 // No auth at all, just send helo and then send the mail
118 // We already said hi earlier, nothing more is needed.
119 } elseif ($smtp_auth_mech == 'login') {
121 fputs($stream, "AUTH LOGIN\r\n");
122 $tmp = fgets($stream, 1024);
124 if ($this->errorCheck($tmp, $stream)) {
127 fputs($stream, base64_encode ($username) . "\r\n");
128 $tmp = fgets($stream, 1024);
129 if ($this->errorCheck($tmp, $stream)) {
133 fputs($stream, base64_encode($pass) . "\r\n");
134 $tmp = fgets($stream, 1024);
135 if ($this->errorCheck($tmp, $stream)) {
138 } elseif ($smtp_auth_mech == "plain") {
140 $auth = base64_encode("$username\0$username\0$pass");
142 $query = "AUTH PLAIN\r\n";
143 fputs($stream, $query);
144 $read=fgets($stream, 1024);
146 if (substr($read,0,3) == '334') { // OK so far..
147 fputs($stream, "$auth\r\n");
148 $read = fgets($stream, 1024);
151 $results=explode(" ",$read,3);
152 $response=$results[1];
153 $message=$results[2];
157 /* Right here, they've reached an unsupported auth mechanism.
158 This is the ugliest hack I've ever done, but it'll do till I can fix
159 things up better tomorrow. So tired... */
160 if ($this->errorCheck("535 Unable to use this auth type",$stream)) {
165 /* Ok, who is sending the message? */
166 fputs($stream, 'MAIL FROM:<'.$from->mailbox
.'@'.$from->host
.">\r\n");
167 $tmp = fgets($stream, 1024);
168 if ($this->errorCheck($tmp, $stream)) {
172 /* send who the recipients are */
173 for ($i = 0, $cnt = count($to); $i < $cnt; $i++
) {
174 if (!$to[$i]->host
) $to[$i]->host
= $domain;
175 if ($to[$i]->mailbox
) {
176 fputs($stream, 'RCPT TO:<'.$to[$i]->mailbox
.'@'.$to[$i]->host
.">\r\n");
177 $tmp = fgets($stream, 1024);
178 if ($this->errorCheck($tmp, $stream)) {
184 for ($i = 0, $cnt = count($cc); $i < $cnt; $i++
) {
185 if (!$cc[$i]->host
) $cc[$i]->host
= $domain;
186 if ($cc[$i]->mailbox
) {
187 fputs($stream, 'RCPT TO:<'.$cc[$i]->mailbox
.'@'.$cc[$i]->host
.">\r\n");
188 $tmp = fgets($stream, 1024);
189 if ($this->errorCheck($tmp, $stream)) {
195 for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++
) {
196 if (!$bcc[$i]->host
) $bcc[$i]->host
= $domain;
197 if ($bcc[$i]->mailbox
) {
198 fputs($stream, 'RCPT TO:<'.$bcc[$i]->mailbox
.'@'.$bcc[$i]->host
.">\r\n");
199 $tmp = fgets($stream, 1024);
200 if ($this->errorCheck($tmp, $stream)) {
205 /* Lets start sending the actual message */
206 fputs($stream, "DATA\r\n");
207 $tmp = fgets($stream, 1024);
208 if ($this->errorCheck($tmp, $stream)) {
214 function finalizeStream($stream) {
215 fputs($stream, ".\r\n"); /* end the DATA part */
216 $tmp = fgets($stream, 1024);
217 $this->errorCheck($tmp, $stream);
218 if ($this->dlv_ret_nr
!= 250) {
221 fputs($stream, "QUIT\r\n"); /* log off */
226 /* check if an SMTP reply is an error and set an error message) */
227 function errorCheck($line, $smtpConnection) {
229 $err_num = substr($line, 0, 3);
230 $this->dlv_ret_nr
= $err_num;
231 $server_msg = substr($line, 4);
233 while(substr($line, 0, 4) == ($err_num.'-')) {
234 $line = fgets($smtpConnection, 1024);
235 $server_msg .= substr($line, 4);
238 if ( ((int) $err_num{0}) < 4)
244 case '421': $message = _("Service not available, closing channel");
246 case '432': $message = _("A password transition is needed");
248 case '450': $message = _("Requested mail action not taken: mailbox unavailable");
250 case '451': $message = _("Requested action aborted: error in processing");
252 case '452': $message = _("Requested action not taken: insufficient system storage");
254 case '454': $message = _("Temporary authentication failure");
256 case '500': $message = _("Syntax error; command not recognized");
258 case '501': $message = _("Syntax error in parameters or arguments");
260 case '502': $message = _("Command not implemented");
262 case '503': $message = _("Bad sequence of commands");
264 case '504': $message = _("Command parameter not implemented");
266 case '530': $message = _("Authentication required");
268 case '534': $message = _("Authentication mechanism is too weak");
270 case '535': $message = _("Authentication failed");
272 case '538': $message = _("Encryption required for requested authentication mechanism");
274 case '550': $message = _("Requested action not taken: mailbox unavailable");
276 case '551': $message = _("User not local; please try forwarding");
278 case '552': $message = _("Requested mail action aborted: exceeding storage allocation");
280 case '553': $message = _("Requested action not taken: mailbox name not allowed");
282 case '554': $message = _("Transaction failed");
284 default: $message = _("Unknown response");
288 $this->dlv_msg
= $message;
289 $this->dlv_server_msg
= nl2br(htmlspecialchars($server_msg));
294 function authPop($pop_server='', $pop_port='', $user, $pass) {
299 $pop_server = 'localhost';
301 $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str);
302 if (!$popConnection) {
303 error_log("Error connecting to POP Server ($pop_server:$pop_port)"
304 . " $err_no : $err_str");
306 $tmp = fgets($popConnection, 1024); /* banner */
307 if (substr($tmp, 0, 3) != '+OK') {
310 fputs($popConnection, "USER $user\r\n");
311 $tmp = fgets($popConnection, 1024);
312 if (substr($tmp, 0, 3) != '+OK') {
315 fputs($popConnection, 'PASS ' . $pass . "\r\n");
316 $tmp = fgets($popConnection, 1024);
317 if (substr($tmp, 0, 3) != '+OK') {
320 fputs($popConnection, "QUIT\r\n"); /* log off */
321 fclose($popConnection);