fb58c0319d3fdd2179efab8fa15945e43d30c224
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)) {
53 /* Lets introduce ourselves */
54 if (( $smtp_auth_mech == 'cram-md5') or ( $smtp_auth_mech == 'digest-md5' )) {
55 // Doing some form of non-plain auth
56 fputs($stream, "EHLO $domain\r\n");
57 $tmp = fgets($stream,1024);
58 if ($this->errorCheck($tmp,$stream)) {
61 if ($smtp_auth_mech == 'cram-md5') {
62 fputs($stream, "AUTH CRAM-MD5\r\n");
63 } elseif ($smtp_auth_mech == 'digest-md5') {
64 fputs($stream, "AUTH DIGEST-MD5\r\n");
66 $tmp = fgets($stream,1024);
68 if ($this->errorCheck($tmp,$stream)) {
72 // At this point, $tmp should hold "334 <challenge string>"
73 $chall = substr($tmp,4);
74 // Depending on mechanism, generate response string
75 if ($smtp_auth_mech == 'cram-md5') {
76 $response = cram_md5_response($username,$pass,$chall);
77 } elseif ($smtp_auth_mech == 'digest-md5') {
78 $response = digest_md5_response($username,$pass,$chall,'smtp',$host);
80 fputs($stream, $response);
82 // Let's see what the server had to say about that
83 $tmp = fgets($stream,1024);
84 if ($this->errorCheck($tmp,$stream)) {
88 // CRAM-MD5 is done at this point. If DIGEST-MD5, there's a bit more to go
89 if ($smtp_auth_mech == 'digest-md5')
91 // $tmp contains rspauth, but I don't store that yet. (No need yet)
92 fputs($stream,"\r\n");
93 $tmp = fgets($stream,1024);
95 if ($this->errorCheck($tmp,$stream)) {
99 // CRAM-MD5 and DIGEST-MD5 code ends here
100 } elseif ($smtp_auth_mech == 'none') {
101 // No auth at all, just send helo and then send the mail
102 fputs($stream, "HELO $domain\r\n");
103 $tmp = fgets($stream, 1024);
104 if ($this->errorCheck($tmp, $stream)) {
107 } elseif ($smtp_auth_mech == 'login') {
109 fputs($stream, "EHLO $domain\r\n");
110 $tmp = fgets($stream, 1024);
111 if ($this->errorCheck($tmp, $stream)) {
114 fputs($stream, "AUTH LOGIN\r\n");
115 $tmp = fgets($stream, 1024);
117 if ($this->errorCheck($tmp, $stream)) {
120 fputs($stream, base64_encode ($username) . "\r\n");
121 $tmp = fgets($stream, 1024);
122 if ($this->errorCheck($tmp, $stream)) {
126 fputs($stream, base64_encode($pass) . "\r\n");
127 $tmp = fgets($stream, 1024);
128 if ($this->errorCheck($tmp, $stream)) {
132 /* Right here, they've reached an unsupported auth mechanism.
133 This is the ugliest hack I've ever done, but it'll do till I can fix
134 things up better tomorrow. So tired... */
135 if ($this->errorCheck("535 Unable to use this auth type",$stream)) {
140 /* Ok, who is sending the message? */
141 fputs($stream, 'MAIL FROM: <'.$from->mailbox
.'@'.$from->host
.">\r\n");
142 $tmp = fgets($stream, 1024);
143 if ($this->errorCheck($tmp, $stream)) {
147 /* send who the recipients are */
148 for ($i = 0, $cnt = count($to); $i < $cnt; $i++
) {
149 if (!$to[$i]->host
) $to[$i]->host
= $domain;
150 if ($to[$i]->mailbox
) {
151 fputs($stream, 'RCPT TO: <'.$to[$i]->mailbox
.'@'.$to[$i]->host
.">\r\n");
152 $tmp = fgets($stream, 1024);
153 if ($this->errorCheck($tmp, $stream)) {
159 for ($i = 0, $cnt = count($cc); $i < $cnt; $i++
) {
160 if (!$cc[$i]->host
) $cc[$i]->host
= $domain;
161 if ($cc[$i]->mailbox
) {
162 fputs($stream, 'RCPT TO: <'.$cc[$i]->mailbox
.'@'.$cc[$i]->host
.">\r\n");
163 $tmp = fgets($stream, 1024);
164 if ($this->errorCheck($tmp, $stream)) {
169 for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++
) {
170 if (!$bcc[$i]->host
) $bcc[$i]->host
= $domain;
171 if ($bcc[$i]->mailbox
) {
172 fputs($stream, 'RCPT TO: <'.$bcc[$i]->mailbox
.'@'.$bcc[$i]->host
.">\r\n");
173 $tmp = fgets($stream, 1024);
174 if ($this->errorCheck($tmp, $stream)) {
179 /* Lets start sending the actual message */
180 fputs($stream, "DATA\r\n");
181 $tmp = fgets($stream, 1024);
182 if ($this->errorCheck($tmp, $stream)) {
188 function finalizeStream($stream) {
189 fputs($stream, ".\r\n"); /* end the DATA part */
190 $tmp = fgets($stream, 1024);
191 $this->errorCheck($tmp, $stream);
192 if ($this->dlv_ret_nr
!= 250) {
195 fputs($stream, "QUIT\r\n"); /* log off */
200 function errorCheck($line, $smtpConnection) {
201 global $color, $compose_new_win;
203 /* Read new lines on a multiline response */
205 while(ereg("^[0-9]+-", $line)) {
206 $line = fgets($smtpConnection, 1024);
212 $err_num = substr($line, 0, strpos($line, " "));
214 case 500: $message = 'Syntax error; command not recognized';
217 case 501: $message = 'Syntax error in parameters or arguments';
220 case 502: $message = 'Command not implemented';
223 case 503: $message = 'Bad sequence of commands';
226 case 504: $message = 'Command parameter not implemented';
229 case 211: $message = 'System status, or system help reply';
232 case 214: $message = 'Help message';
235 case 220: $message = 'Service ready';
238 case 221: $message = 'Service closing transmission channel';
241 case 421: $message = 'Service not available, closing channel';
244 case 235: $message = 'Authentication successful';
247 case 250: $message = 'Requested mail action okay, completed';
250 case 251: $message = 'User not local; will forward';
253 case 334: $message = 'OK - continue request';
256 case 450: $message = 'Requested mail action not taken: mailbox unavailable';
259 case 550: $message = 'Requested action not taken: mailbox unavailable';
262 case 451: $message = 'Requested action aborted: error in processing';
265 case 551: $message = 'User not local; please try forwarding';
268 case 452: $message = 'Requested action not taken: insufficient system storage';
271 case 552: $message = 'Requested mail action aborted: exceeding storage allocation';
274 case 553: $message = 'Requested action not taken: mailbox name not allowed';
277 case 354: $message = 'Start mail input; end with .';
280 case 554: $message = 'Transaction failed';
284 case 432: $message = 'A password transition is needed';
287 case 534: $message = 'Authentication mechanism is too weak';
290 case 538: $message = 'Encryption required for requested authentication mechanism';
293 case 454: $message = 'Temmporary authentication failure';
296 case 530: $message = 'Authentication required';
300 case 535: $message = 'Authentication failed';
303 default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines));
308 $this->dlv_ret_nr
= $err_num;
309 $this->dlv_msg
= $message;
316 function authPop($pop_server='', $pop_port='', $user, $pass) {
321 $pop_server = 'localhost';
323 $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str);
324 if (!$popConnection) {
325 error_log("Error connecting to POP Server ($pop_server:$pop_port)"
326 . " $err_no : $err_str");
328 $tmp = fgets($popConnection, 1024); /* banner */
329 if (!eregi("^\+OK", $tmp, $regs)) {
332 fputs($popConnection, "USER $user\r\n");
333 $tmp = fgets($popConnection, 1024);
334 if (!eregi("^\+OK", $tmp, $regs)) {
337 fputs($popConnection, 'PASS ' . $pass . "\r\n");
338 $tmp = fgets($popConnection, 1024);
339 if (!eregi("^\+OK", $tmp, $regs)) {
342 fputs($popConnection, "QUIT\r\n"); /* log off */
343 fclose($popConnection);