2c4ca6388c3a9fc0ce2983c76748065f241f3b5e
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('../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) {
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 if ($to[$i]->mailbox
) {
91 fputs($stream, 'RCPT TO: <'.$to[$i]->mailbox
.'@'.$to[$i]->host
.">\r\n");
92 $tmp = fgets($stream, 1024);
93 if ($this->errorCheck($tmp, $stream)) {
99 for ($i = 0, $cnt = count($cc); $i < $cnt; $i++
) {
100 if (!$cc[$i]->host
) $cc[$i]->host
= $domain;
101 if ($cc[$i]->mailbox
) {
102 fputs($stream, 'RCPT TO: <'.$cc[$i]->mailbox
.'@'.$cc[$i]->host
.">\r\n");
103 $tmp = fgets($stream, 1024);
104 if ($this->errorCheck($tmp, $stream)) {
109 for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++
) {
110 if (!$bcc[$i]->host
) $bcc[$i]->host
= $domain;
111 if ($bcc[$i]->mailbox
) {
112 fputs($stream, 'RCPT TO: <'.$bcc[$i]->mailbox
.'@'.$bcc[$i]->host
.">\r\n");
113 $tmp = fgets($stream, 1024);
114 if ($this->errorCheck($tmp, $stream)) {
119 /* Lets start sending the actual message */
120 fputs($stream, "DATA\r\n");
121 $tmp = fgets($stream, 1024);
122 if ($this->errorCheck($tmp, $stream)) {
128 function finalizeStream($stream) {
129 fputs($stream, ".\r\n"); /* end the DATA part */
130 $tmp = fgets($stream, 1024);
131 $this->errorCheck($tmp, $stream);
132 if ($this->dlv_ret_nr
!= 250) {
135 fputs($stream, "QUIT\r\n"); /* log off */
140 function errorCheck($line, $smtpConnection) {
141 global $color, $compose_new_win;
143 /* Read new lines on a multiline response */
145 while(ereg("^[0-9]+-", $line)) {
146 $line = fgets($smtpConnection, 1024);
152 $err_num = substr($line, 0, strpos($line, " "));
154 case 500: $message = 'Syntax error; command not recognized';
157 case 501: $message = 'Syntax error in parameters or arguments';
160 case 502: $message = 'Command not implemented';
163 case 503: $message = 'Bad sequence of commands';
166 case 504: $message = 'Command parameter not implemented';
169 case 211: $message = 'System status, or system help reply';
172 case 214: $message = 'Help message';
175 case 220: $message = 'Service ready';
178 case 221: $message = 'Service closing transmission channel';
181 case 421: $message = 'Service not available, closing chanel';
186 case 250: $message = 'Requested mail action okay, completed';
189 case 251: $message = 'User not local; will forward';
192 case 334: return(5); break;
193 case 450: $message = 'Requested mail action not taken: mailbox unavailable';
196 case 550: $message = 'Requested action not taken: mailbox unavailable';
199 case 451: $message = 'Requested action aborted: error in processing';
202 case 551: $message = 'User not local; please try forwarding';
205 case 452: $message = 'Requested action not taken: insufficient system storage';
208 case 552: $message = 'Requested mail action aborted: exceeding storage allocation';
211 case 553: $message = 'Requested action not taken: mailbox name not allowed';
214 case 354: $message = 'Start mail input; end with .';
217 case 554: $message = 'Transaction failed';
221 case 432: $message = 'A password transition is needed';
224 case 534: $message = 'Authentication mechanism is too weak';
227 case 538: $message = 'Encryption required for requested authentication mechanism';
230 case 454: $message = 'Temmporary authentication failure';
233 case 530: $message = 'Authentication required';
237 default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines));
242 $this->dlv_ret_nr
= $err_num;
243 $this->dlv_msg
= $message;
250 function authPop($pop_server='', $pop_port='', $user, $pass) {
255 $pop_server = 'localhost';
257 $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str);
258 if (!$popConnection) {
259 error_log("Error connecting to POP Server ($pop_server:$pop_port)"
260 . " $err_no : $err_str");
262 $tmp = fgets($popConnection, 1024); /* banner */
263 if (!eregi("^\+OK", $tmp, $regs)) {
266 fputs($popConnection, "USER $user\r\n");
267 $tmp = fgets($popConnection, 1024);
268 if (!eregi("^\+OK", $tmp, $regs)) {
271 fputs($popConnection, 'PASS ' . $pass . "\r\n");
272 $tmp = fgets($popConnection, 1024);
273 if (!eregi("^\+OK", $tmp, $regs)) {
276 fputs($popConnection, "QUIT\r\n"); /* log off */
277 fclose($popConnection);