4c8e49a0eac09cbd9ffb7a6f1b4381145562e956
3 * Deliver_SMTP.class.php
5 * Copyright (c) 1999-2002 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' )) and (extension_loaded('mhash')))
56 // Doing some form of non-plain auth
57 fputs($stream, "EHLO $domain\r\n");
58 $tmp = fgets($stream,1024);
59 if ($this->errorCheck($tmp,$stream)) {
62 if ($smtp_auth_mech == 'cram-md5') {
63 fputs($stream, "AUTH CRAM-MD5\r\n");
64 } elseif ($smtp_auth_mech == 'digest-md5') {
65 fputs($stream, "AUTH DIGEST-MD5\r\n");
67 $tmp = fgets($stream,1024);
69 if ($this->errorCheck($tmp,$stream)) {
73 // At this point, $tmp should hold "334 <challenge string>"
74 $chall = substr($tmp,4);
75 // Depending on mechanism, generate response string
76 if ($smtp_auth_mech == 'cram-md5') {
77 $response = cram_md5_response($username,$pass,$chall);
78 } elseif ($smtp_auth_mech == 'digest-md5') {
79 $response = digest_md5_response($username,$pass,$chall,'smtp',$host);
81 fputs($stream, $response);
83 // Let's see what the server had to say about that
84 $tmp = fgets($stream,1024);
85 if ($this->errorCheck($tmp,$stream)) {
89 // CRAM-MD5 is done at this point. If DIGEST-MD5, there's a bit more to go
90 if ($smtp_auth_mech == 'digest-md5')
92 // $tmp contains rspauth, but I don't store that yet. (No need yet)
93 fputs($stream,"\r\n");
94 $tmp = fgets($stream,1024);
96 if ($this->errorCheck($tmp,$stream)) {
100 // CRAM-MD5 and DIGEST-MD5 code ends here
101 } elseif ($smtp_auth_mech == 'none') {
102 // No auth at all, just send helo and then send the mail
103 fputs($stream, "HELO $domain\r\n");
104 $tmp = fgets($stream, 1024);
105 if ($this->errorCheck($tmp, $stream)) {
108 } elseif ($smtp_auth_mech == 'plain') {
109 // The plain LOGIN method
110 fputs($stream, "EHLO $domain\r\n");
111 $tmp = fgets($stream, 1024);
112 if ($this->errorCheck($tmp, $stream)) {
115 fputs($stream, "AUTH LOGIN\r\n");
116 $tmp = fgets($stream, 1024);
118 if ($this->errorCheck($tmp, $stream)) {
121 fputs($stream, base64_encode ($username) . "\r\n");
122 $tmp = fgets($stream, 1024);
123 if ($this->errorCheck($tmp, $stream)) {
127 fputs($stream, base64_encode($pass) . "\r\n");
128 $tmp = fgets($stream, 1024);
129 if ($this->errorCheck($tmp, $stream)) {
134 /* Ok, who is sending the message? */
135 fputs($stream, 'MAIL FROM: <'.$from->mailbox
.'@'.$from->host
.">\r\n");
136 $tmp = fgets($stream, 1024);
137 if ($this->errorCheck($tmp, $stream)) {
141 /* send who the recipients are */
142 for ($i = 0, $cnt = count($to); $i < $cnt; $i++
) {
143 if (!$to[$i]->host
) $to[$i]->host
= $domain;
144 if ($to[$i]->mailbox
) {
145 fputs($stream, 'RCPT TO: <'.$to[$i]->mailbox
.'@'.$to[$i]->host
.">\r\n");
146 $tmp = fgets($stream, 1024);
147 if ($this->errorCheck($tmp, $stream)) {
153 for ($i = 0, $cnt = count($cc); $i < $cnt; $i++
) {
154 if (!$cc[$i]->host
) $cc[$i]->host
= $domain;
155 if ($cc[$i]->mailbox
) {
156 fputs($stream, 'RCPT TO: <'.$cc[$i]->mailbox
.'@'.$cc[$i]->host
.">\r\n");
157 $tmp = fgets($stream, 1024);
158 if ($this->errorCheck($tmp, $stream)) {
163 for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++
) {
164 if (!$bcc[$i]->host
) $bcc[$i]->host
= $domain;
165 if ($bcc[$i]->mailbox
) {
166 fputs($stream, 'RCPT TO: <'.$bcc[$i]->mailbox
.'@'.$bcc[$i]->host
.">\r\n");
167 $tmp = fgets($stream, 1024);
168 if ($this->errorCheck($tmp, $stream)) {
173 /* Lets start sending the actual message */
174 fputs($stream, "DATA\r\n");
175 $tmp = fgets($stream, 1024);
176 if ($this->errorCheck($tmp, $stream)) {
182 function finalizeStream($stream) {
183 fputs($stream, ".\r\n"); /* end the DATA part */
184 $tmp = fgets($stream, 1024);
185 $this->errorCheck($tmp, $stream);
186 if ($this->dlv_ret_nr
!= 250) {
189 fputs($stream, "QUIT\r\n"); /* log off */
194 function errorCheck($line, $smtpConnection) {
195 global $color, $compose_new_win;
197 /* Read new lines on a multiline response */
199 while(ereg("^[0-9]+-", $line)) {
200 $line = fgets($smtpConnection, 1024);
206 $err_num = substr($line, 0, strpos($line, " "));
208 case 500: $message = 'Syntax error; command not recognized';
211 case 501: $message = 'Syntax error in parameters or arguments';
214 case 502: $message = 'Command not implemented';
217 case 503: $message = 'Bad sequence of commands';
220 case 504: $message = 'Command parameter not implemented';
223 case 211: $message = 'System status, or system help reply';
226 case 214: $message = 'Help message';
229 case 220: $message = 'Service ready';
232 case 221: $message = 'Service closing transmission channel';
235 case 421: $message = 'Service not available, closing channel';
238 case 235: $message = 'Authentication successful';
241 case 250: $message = 'Requested mail action okay, completed';
244 case 251: $message = 'User not local; will forward';
247 case 334: $message = 'OK - continue request';
250 case 450: $message = 'Requested mail action not taken: mailbox unavailable';
253 case 550: $message = 'Requested action not taken: mailbox unavailable';
256 case 451: $message = 'Requested action aborted: error in processing';
259 case 551: $message = 'User not local; please try forwarding';
262 case 452: $message = 'Requested action not taken: insufficient system storage';
265 case 552: $message = 'Requested mail action aborted: exceeding storage allocation';
268 case 553: $message = 'Requested action not taken: mailbox name not allowed';
271 case 354: $message = 'Start mail input; end with .';
274 case 554: $message = 'Transaction failed';
278 case 432: $message = 'A password transition is needed';
281 case 534: $message = 'Authentication mechanism is too weak';
284 case 538: $message = 'Encryption required for requested authentication mechanism';
287 case 454: $message = 'Temmporary authentication failure';
290 case 530: $message = 'Authentication required';
294 case 535: $message = 'Authentication failed';
297 default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines));
302 $this->dlv_ret_nr
= $err_num;
303 $this->dlv_msg
= $message;
310 function authPop($pop_server='', $pop_port='', $user, $pass) {
315 $pop_server = 'localhost';
317 $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str);
318 if (!$popConnection) {
319 error_log("Error connecting to POP Server ($pop_server:$pop_port)"
320 . " $err_no : $err_str");
322 $tmp = fgets($popConnection, 1024); /* banner */
323 if (!eregi("^\+OK", $tmp, $regs)) {
326 fputs($popConnection, "USER $user\r\n");
327 $tmp = fgets($popConnection, 1024);
328 if (!eregi("^\+OK", $tmp, $regs)) {
331 fputs($popConnection, 'PASS ' . $pass . "\r\n");
332 $tmp = fgets($popConnection, 1024);
333 if (!eregi("^\+OK", $tmp, $regs)) {
336 fputs($popConnection, "QUIT\r\n"); /* log off */
337 fclose($popConnection);