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('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) {
27 $this->authPop($host, '', $user, $pass);
30 $rfc822_header = $message->rfc822_header
;
31 $from = $rfc822_header->from
[0];
32 $to = $rfc822_header->to
;
33 $cc = $rfc822_header->cc
;
34 $bcc = $rfc822_header->bcc
;
36 $stream = fsockopen($host, $port, $errorNumber, $errorString);
38 $this->dlv_msg
= $errorString;
39 $this->dlv_ret_nr
= $errorNumber;
42 $tmp = fgets($stream, 1024);
43 if ($this->errorCheck($tmp, $stream)) {
47 /* Lets introduce ourselves */
48 if (! isset ($use_authenticated_smtp)
49 ||
$use_authenticated_smtp == false) {
50 fputs($stream, "HELO $domain\r\n");
51 $tmp = fgets($stream, 1024);
52 if ($this->errorCheck($tmp, $stream)) {
56 fputs($stream, "EHLO $domain\r\n");
57 $tmp = fgets($stream, 1024);
58 if ($this->errorCheck($tmp, $stream)) {
61 fputs($stream, "AUTH LOGIN\r\n");
62 $tmp = fgets($stream, 1024);
64 if ($this->errorCheck($tmp, $stream)) {
67 fputs($stream, base64_encode ($user) . "\r\n");
68 $tmp = fgets($stream, 1024);
69 if ($this->errorCheck($tmp, $stream)) {
73 fputs($stream, base64_encode($pass) . "\r\n");
74 $tmp = fgets($stream, 1024);
75 if ($this->errorCheck($tmp, $stream)) {
80 /* Ok, who is sending the message? */
81 fputs($stream, 'MAIL FROM: <'.$from->mailbox
.'@'.$from->host
.">\r\n");
82 $tmp = fgets($stream, 1024);
83 if ($this->errorCheck($tmp, $stream)) {
87 /* send who the recipients are */
88 for ($i = 0, $cnt = count($to); $i < $cnt; $i++
) {
89 if (!$to[$i]->host
) $to[$i]->host
= $domain;
90 fputs($stream, 'RCPT TO: <'.$to[$i]->mailbox
.'@'.$to[$i]->host
.">\r\n");
91 $tmp = fgets($stream, 1024);
92 if ($this->errorCheck($tmp, $stream)) {
96 for ($i = 0, $cnt = count($cc); $i < $cnt; $i++
) {
97 if (!$cc[$i]->host
) $cc[$i]->host
= $domain;
98 fputs($stream, 'RCPT TO: <'.$cc[$i]->mailbox
.'@'.$cc[$i]->host
.">\r\n");
99 $tmp = fgets($stream, 1024);
100 if ($this->errorCheck($tmp, $stream)) {
104 for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++
) {
105 if (!$bcc[$i]->host
) $bcc[$i]->host
= $domain;
106 fputs($stream, 'RCPT TO: <'.$bcc[$i]->mailbox
.'@'.$bcc[$i]->host
.">\r\n");
107 $tmp = fgets($stream, 1024);
108 if ($this->errorCheck($tmp, $stream)) {
112 /* Lets start sending the actual message */
113 fputs($stream, "DATA\r\n");
114 $tmp = fgets($stream, 1024);
115 if ($this->errorCheck($tmp, $stream)) {
121 function finalizeStream($stream) {
122 fputs($stream, ".\r\n"); /* end the DATA part */
123 $tmp = fgets($stream, 1024);
124 $this->errorCheck($tmp, $stream);
125 if ($this->dlv_ret_nr
!= 250) {
128 fputs($stream, "QUIT\r\n"); /* log off */
133 function errorCheck($line, $smtpConnection) {
134 global $color, $compose_new_win;
136 /* Read new lines on a multiline response */
138 while(ereg("^[0-9]+-", $line)) {
139 $line = fgets($smtpConnection, 1024);
145 $err_num = substr($line, 0, strpos($line, " "));
147 case 500: $message = 'Syntax error; command not recognized';
150 case 501: $message = 'Syntax error in parameters or arguments';
153 case 502: $message = 'Command not implemented';
156 case 503: $message = 'Bad sequence of commands';
159 case 504: $message = 'Command parameter not implemented';
162 case 211: $message = 'System status, or system help reply';
165 case 214: $message = 'Help message';
168 case 220: $message = 'Service ready';
171 case 221: $message = 'Service closing transmission channel';
174 case 421: $message = 'Service not available, closing chanel';
179 case 250: $message = 'Requested mail action okay, completed';
182 case 251: $message = 'User not local; will forward';
185 case 334: return(5); break;
186 case 450: $message = 'Requested mail action not taken: mailbox unavailable';
189 case 550: $message = 'Requested action not taken: mailbox unavailable';
192 case 451: $message = 'Requested action aborted: error in processing';
195 case 551: $message = 'User not local; please try forwarding';
198 case 452: $message = 'Requested action not taken: insufficient system storage';
201 case 552: $message = 'Requested mail action aborted: exceeding storage allocation';
204 case 553: $message = 'Requested action not taken: mailbox name not allowed';
207 case 354: $message = 'Start mail input; end with .';
210 case 554: $message = 'Transaction failed';
214 case 432: $message = 'A password transition is needed';
217 case 534: $message = 'Authentication mechanism is too weak';
220 case 538: $message = 'Encryption required for requested authentication mechanism';
223 case 454: $message = 'Temmporary authentication failure';
226 case 530: $message = 'Authentication required';
230 default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines));
235 $this->dlv_ret_nr
= $err_num;
236 $this->dlv_msg
= $message;
243 function authPop($pop_server='', $pop_port='', $user, $pass) {
248 $pop_server = 'localhost';
250 $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str);
251 if (!$popConnection) {
252 error_log("Error connecting to POP Server ($pop_server:$pop_port)"
253 . " $err_no : $err_str");
255 $tmp = fgets($popConnection, 1024); /* banner */
256 if (!eregi("^\+OK", $tmp, $regs)) {
259 fputs($popConnection, "USER $user\r\n");
260 $tmp = fgets($popConnection, 1024);
261 if (!eregi("^\+OK", $tmp, $regs)) {
264 fputs($popConnection, 'PASS ' . $pass . "\r\n");
265 $tmp = fgets($popConnection, 1024);
266 if (!eregi("^\+OK", $tmp, $regs)) {
269 fputs($popConnection, "QUIT\r\n"); /* log off */
270 fclose($popConnection);