More SM_PATH fixes. This time, everything in class/.
[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(SM_PATH . 'class/deliver/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 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)) {
94 return(0);
95 }
96 }
97 }
98
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)) {
105 return(0);
106 }
107 }
108 }
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)) {
115 return(0);
116 }
117 }
118 }
119 /* Lets start sending the actual message */
120 fputs($stream, "DATA\r\n");
121 $tmp = fgets($stream, 1024);
122 if ($this->errorCheck($tmp, $stream)) {
123 return(0);
124 }
125 return $stream;
126 }
127
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) {
133 return(0);
134 }
135 fputs($stream, "QUIT\r\n"); /* log off */
136 fclose($stream);
137 return true;
138 }
139
140 function errorCheck($line, $smtpConnection) {
141 global $color, $compose_new_win;
142
143 /* Read new lines on a multiline response */
144 $lines = $line;
145 while(ereg("^[0-9]+-", $line)) {
146 $line = fgets($smtpConnection, 1024);
147 $lines .= $line;
148 }
149 /* Status: 0 = fatal
150 * 5 = ok
151 */
152 $err_num = substr($line, 0, strpos($line, " "));
153 switch ($err_num) {
154 case 500: $message = 'Syntax error; command not recognized';
155 $status = 0;
156 break;
157 case 501: $message = 'Syntax error in parameters or arguments';
158 $status = 0;
159 break;
160 case 502: $message = 'Command not implemented';
161 $status = 0;
162 break;
163 case 503: $message = 'Bad sequence of commands';
164 $status = 0;
165 break;
166 case 504: $message = 'Command parameter not implemented';
167 $status = 0;
168 break;
169 case 211: $message = 'System status, or system help reply';
170 $status = 5;
171 break;
172 case 214: $message = 'Help message';
173 $status = 5;
174 break;
175 case 220: $message = 'Service ready';
176 $status = 5;
177 break;
178 case 221: $message = 'Service closing transmission channel';
179 $status = 5;
180 break;
181 case 421: $message = 'Service not available, closing chanel';
182 $status = 0;
183 break;
184 case 235: return(5);
185 break;
186 case 250: $message = 'Requested mail action okay, completed';
187 $status = 5;
188 break;
189 case 251: $message = 'User not local; will forward';
190 $status = 5;
191 break;
192 case 334: return(5); break;
193 case 450: $message = 'Requested mail action not taken: mailbox unavailable';
194 $status = 0;
195 break;
196 case 550: $message = 'Requested action not taken: mailbox unavailable';
197 $status = 0;
198 break;
199 case 451: $message = 'Requested action aborted: error in processing';
200 $status = 0;
201 break;
202 case 551: $message = 'User not local; please try forwarding';
203 $status = 0;
204 break;
205 case 452: $message = 'Requested action not taken: insufficient system storage';
206 $status = 0;
207 break;
208 case 552: $message = 'Requested mail action aborted: exceeding storage allocation';
209 $status = 0;
210 break;
211 case 553: $message = 'Requested action not taken: mailbox name not allowed';
212 $status = 0;
213 break;
214 case 354: $message = 'Start mail input; end with .';
215 $status = 5;
216 break;
217 case 554: $message = 'Transaction failed';
218 $status = 0;
219 break;
220 /* RFC 2554 */
221 case 432: $message = 'A password transition is needed';
222 $status = 0;
223 break;
224 case 534: $message = 'Authentication mechanism is too weak';
225 $status = 0;
226 break;
227 case 538: $message = 'Encryption required for requested authentication mechanism';
228 $status = 0;
229 break;
230 case 454: $message = 'Temmporary authentication failure';
231 $status = 0;
232 break;
233 case 530: $message = 'Authentication required';
234 $status = 0;
235 break;
236 /* end RFC2554 */
237 default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines));
238 $status = 0;
239 $err_num = '001';
240 break;
241 }
242 $this->dlv_ret_nr = $err_num;
243 $this->dlv_msg = $message;
244 if ($status == 5) {
245 return false;
246 }
247 return true;
248 }
249
250 function authPop($pop_server='', $pop_port='', $user, $pass) {
251 if (!$pop_port) {
252 $pop_port = 110;
253 }
254 if (!$pop_server) {
255 $pop_server = 'localhost';
256 }
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");
261 } else {
262 $tmp = fgets($popConnection, 1024); /* banner */
263 if (!eregi("^\+OK", $tmp, $regs)) {
264 return(0);
265 }
266 fputs($popConnection, "USER $user\r\n");
267 $tmp = fgets($popConnection, 1024);
268 if (!eregi("^\+OK", $tmp, $regs)) {
269 return(0);
270 }
271 fputs($popConnection, 'PASS ' . $pass . "\r\n");
272 $tmp = fgets($popConnection, 1024);
273 if (!eregi("^\+OK", $tmp, $regs)) {
274 return(0);
275 }
276 fputs($popConnection, "QUIT\r\n"); /* log off */
277 fclose($popConnection);
278 }
279 }
280 }
281
282 ?>