added info
[squirrelmail.git] / class / deliver / Deliver_SMTP.class.php
1 <?php
2 /**
3 * Deliver_SMTP.class.php
4 *
5 * Copyright (c) 1999-2002 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * Delivery backend for the Deliver class.
9 *
10 * $Id$
11 */
12
13 require_once('Deliver.class.php');
14
15 class Deliver_SMTP extends Deliver {
16
17 function preWriteToStream(&$s) {
18 if ($s) {
19 if ($s{0} == '.') $s = '.' . $s;
20 $s = str_replace("\n.","\n..",$s);
21 }
22 }
23
24 function initStream($message, $domain, $length=0, $host='', $port='', $user='', $pass='', $authpop=false) {
25
26 if ($authpop) {
27 $this->authPop($host, '', $user, $pass);
28 }
29
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;
35
36 $stream = fsockopen($host, $port, $errorNumber, $errorString);
37 if (!$stream) {
38 $this->dlv_msg = $errorString;
39 $this->dlv_ret_nr = $errorNumber;
40 return(0);
41 }
42 $tmp = fgets($stream, 1024);
43 if ($this->errorCheck($tmp, $stream)) {
44 return(0);
45 }
46
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)) {
53 return(0);
54 }
55 } else {
56 fputs($stream, "EHLO $domain\r\n");
57 $tmp = fgets($stream, 1024);
58 if ($this->errorCheck($tmp, $stream)) {
59 return(0);
60 }
61 fputs($stream, "AUTH LOGIN\r\n");
62 $tmp = fgets($stream, 1024);
63
64 if ($this->errorCheck($tmp, $stream)) {
65 return(0);
66 }
67 fputs($stream, base64_encode ($user) . "\r\n");
68 $tmp = fgets($stream, 1024);
69 if ($this->errorCheck($tmp, $stream)) {
70 return(0);
71 }
72
73 fputs($stream, base64_encode($pass) . "\r\n");
74 $tmp = fgets($stream, 1024);
75 if ($this->errorCheck($tmp, $stream)) {
76 return(0);
77 }
78 }
79
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)) {
84 return(0);
85 }
86
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)) {
93 return(0);
94 }
95 }
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)) {
101 return(0);
102 }
103 }
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)) {
109 return(0);
110 }
111 }
112 /* Lets start sending the actual message */
113 fputs($stream, "DATA\r\n");
114 $tmp = fgets($stream, 1024);
115 if ($this->errorCheck($tmp, $stream)) {
116 return(0);
117 }
118 return $stream;
119 }
120
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) {
126 return(0);
127 }
128 fputs($stream, "QUIT\r\n"); /* log off */
129 fclose($stream);
130 return true;
131 }
132
133 function errorCheck($line, $smtpConnection) {
134 global $color, $compose_new_win;
135
136 /* Read new lines on a multiline response */
137 $lines = $line;
138 while(ereg("^[0-9]+-", $line)) {
139 $line = fgets($smtpConnection, 1024);
140 $lines .= $line;
141 }
142 /* Status: 0 = fatal
143 * 5 = ok
144 */
145 $err_num = substr($line, 0, strpos($line, " "));
146 switch ($err_num) {
147 case 500: $message = 'Syntax error; command not recognized';
148 $status = 0;
149 break;
150 case 501: $message = 'Syntax error in parameters or arguments';
151 $status = 0;
152 break;
153 case 502: $message = 'Command not implemented';
154 $status = 0;
155 break;
156 case 503: $message = 'Bad sequence of commands';
157 $status = 0;
158 break;
159 case 504: $message = 'Command parameter not implemented';
160 $status = 0;
161 break;
162 case 211: $message = 'System status, or system help reply';
163 $status = 5;
164 break;
165 case 214: $message = 'Help message';
166 $status = 5;
167 break;
168 case 220: $message = 'Service ready';
169 $status = 5;
170 break;
171 case 221: $message = 'Service closing transmission channel';
172 $status = 5;
173 break;
174 case 421: $message = 'Service not available, closing chanel';
175 $status = 0;
176 break;
177 case 235: return(5);
178 break;
179 case 250: $message = 'Requested mail action okay, completed';
180 $status = 5;
181 break;
182 case 251: $message = 'User not local; will forward';
183 $status = 5;
184 break;
185 case 334: return(5); break;
186 case 450: $message = 'Requested mail action not taken: mailbox unavailable';
187 $status = 0;
188 break;
189 case 550: $message = 'Requested action not taken: mailbox unavailable';
190 $status = 0;
191 break;
192 case 451: $message = 'Requested action aborted: error in processing';
193 $status = 0;
194 break;
195 case 551: $message = 'User not local; please try forwarding';
196 $status = 0;
197 break;
198 case 452: $message = 'Requested action not taken: insufficient system storage';
199 $status = 0;
200 break;
201 case 552: $message = 'Requested mail action aborted: exceeding storage allocation';
202 $status = 0;
203 break;
204 case 553: $message = 'Requested action not taken: mailbox name not allowed';
205 $status = 0;
206 break;
207 case 354: $message = 'Start mail input; end with .';
208 $status = 5;
209 break;
210 case 554: $message = 'Transaction failed';
211 $status = 0;
212 break;
213 /* RFC 2554 */
214 case 432: $message = 'A password transition is needed';
215 $status = 0;
216 break;
217 case 534: $message = 'Authentication mechanism is too weak';
218 $status = 0;
219 break;
220 case 538: $message = 'Encryption required for requested authentication mechanism';
221 $status = 0;
222 break;
223 case 454: $message = 'Temmporary authentication failure';
224 $status = 0;
225 break;
226 case 530: $message = 'Authentication required';
227 $status = 0;
228 break;
229 /* end RFC2554 */
230 default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines));
231 $status = 0;
232 $err_num = '001';
233 break;
234 }
235 $this->dlv_ret_nr = $err_num;
236 $this->dlv_msg = $message;
237 if ($status == 5) {
238 return false;
239 }
240 return true;
241 }
242
243 function authPop($pop_server='', $pop_port='', $user, $pass) {
244 if (!$pop_port) {
245 $pop_port = 110;
246 }
247 if (!$pop_server) {
248 $pop_server = 'localhost';
249 }
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");
254 } else {
255 $tmp = fgets($popConnection, 1024); /* banner */
256 if (!eregi("^\+OK", $tmp, $regs)) {
257 return(0);
258 }
259 fputs($popConnection, "USER $user\r\n");
260 $tmp = fgets($popConnection, 1024);
261 if (!eregi("^\+OK", $tmp, $regs)) {
262 return(0);
263 }
264 fputs($popConnection, 'PASS ' . $pass . "\r\n");
265 $tmp = fgets($popConnection, 1024);
266 if (!eregi("^\+OK", $tmp, $regs)) {
267 return(0);
268 }
269 fputs($popConnection, "QUIT\r\n"); /* log off */
270 fclose($popConnection);
271 }
272 }
273 }
274
275 ?>