e1ee60fe |
1 | <?php |
2 | |
3 | require_once('Deliver.class.php'); |
4 | |
5 | class Deliver_SMTP extends Deliver { |
6 | |
5fe73b9f |
7 | function preWriteToStream(&$s) { |
8 | if ($s) { |
9 | if ($s{0} == '.') $s = '.' . $s; |
10 | $s = str_replace("\n.","\n..",$s); |
11 | } |
12 | } |
13 | |
14 | function initStream($message, $domain, $length=0, $host='', $port='', $user='', $pass='', $authpop=false) { |
15 | |
16 | if ($authpop) { |
17 | $this->authPop($host, '', $user, $pass); |
18 | } |
19 | |
20 | $rfc822_header = $message->rfc822_header; |
21 | $from = $rfc822_header->from[0]; |
22 | $to = $rfc822_header->to; |
23 | $cc = $rfc822_header->cc; |
24 | $bcc = $rfc822_header->bcc; |
25 | |
26 | $stream = fsockopen($host, $port, $errorNumber, $errorString); |
27 | if (!$stream) { |
28 | $this->dlv_msg = $errorString; |
29 | $this->dlv_ret_nr = $errorNumber; |
30 | return(0); |
31 | } |
32 | $tmp = fgets($stream, 1024); |
33 | if ($this->errorCheck($tmp, $stream)) { |
34 | return(0); |
35 | } |
36 | |
37 | /* Lets introduce ourselves */ |
38 | if (! isset ($use_authenticated_smtp) |
39 | || $use_authenticated_smtp == false) { |
40 | fputs($stream, "HELO $domain\r\n"); |
41 | $tmp = fgets($stream, 1024); |
42 | if ($this->errorCheck($tmp, $stream)) { |
43 | return(0); |
44 | } |
45 | } else { |
46 | fputs($stream, "EHLO $domain\r\n"); |
47 | $tmp = fgets($stream, 1024); |
48 | if ($this->errorCheck($tmp, $stream)) { |
49 | return(0); |
50 | } |
51 | fputs($stream, "AUTH LOGIN\r\n"); |
52 | $tmp = fgets($stream, 1024); |
53 | |
54 | if ($this->errorCheck($tmp, $stream)) { |
55 | return(0); |
56 | } |
57 | fputs($stream, base64_encode ($user) . "\r\n"); |
58 | $tmp = fgets($stream, 1024); |
59 | if ($this->errorCheck($tmp, $stream)) { |
60 | return(0); |
61 | } |
62 | |
63 | fputs($stream, base64_encode($pass) . "\r\n"); |
64 | $tmp = fgets($stream, 1024); |
65 | if ($this->errorCheck($tmp, $stream)) { |
66 | return(0); |
67 | } |
68 | } |
69 | |
70 | /* Ok, who is sending the message? */ |
71 | fputs($stream, 'MAIL FROM: <'.$from->mailbox.'@'.$from->host.">\r\n"); |
72 | $tmp = fgets($stream, 1024); |
73 | if ($this->errorCheck($tmp, $stream)) { |
74 | return(0); |
75 | } |
76 | |
77 | /* send who the recipients are */ |
78 | for ($i = 0, $cnt = count($to); $i < $cnt; $i++) { |
79 | if (!$to[$i]->host) $to[$i]->host = $domain; |
80 | fputs($stream, 'RCPT TO: <'.$to[$i]->mailbox.'@'.$to[$i]->host.">\r\n"); |
81 | $tmp = fgets($stream, 1024); |
82 | if ($this->errorCheck($tmp, $stream)) { |
83 | return(0); |
84 | } |
85 | } |
86 | for ($i = 0, $cnt = count($cc); $i < $cnt; $i++) { |
87 | if (!$cc[$i]->host) $cc[$i]->host = $domain; |
88 | fputs($stream, 'RCPT TO: <'.$cc[$i]->mailbox.'@'.$cc[$i]->host.">\r\n"); |
89 | $tmp = fgets($stream, 1024); |
90 | if ($this->errorCheck($tmp, $stream)) { |
91 | return(0); |
92 | } |
93 | } |
94 | for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++) { |
95 | if (!$bcc[$i]->host) $bcc[$i]->host = $domain; |
96 | fputs($stream, 'RCPT TO: <'.$bcc[$i]->mailbox.'@'.$bcc[$i]->host.">\r\n"); |
97 | $tmp = fgets($stream, 1024); |
98 | if ($this->errorCheck($tmp, $stream)) { |
99 | return(0); |
100 | } |
101 | } |
102 | /* Lets start sending the actual message */ |
103 | fputs($stream, "DATA\r\n"); |
104 | $tmp = fgets($stream, 1024); |
105 | if ($this->errorCheck($tmp, $stream)) { |
106 | return(0); |
107 | } |
108 | return $stream; |
109 | } |
110 | |
111 | function finalizeStream($stream) { |
112 | fputs($stream, ".\r\n"); /* end the DATA part */ |
113 | $tmp = fgets($stream, 1024); |
114 | $this->errorCheck($tmp, $stream); |
115 | if ($this->dlv_ret_nr != 250) { |
116 | return(0); |
117 | } |
118 | fputs($stream, "QUIT\r\n"); /* log off */ |
119 | fclose($stream); |
120 | return true; |
121 | } |
122 | |
123 | function errorCheck($line, $smtpConnection) { |
124 | global $color, $compose_new_win; |
125 | |
126 | /* Read new lines on a multiline response */ |
127 | $lines = $line; |
128 | while(ereg("^[0-9]+-", $line)) { |
129 | $line = fgets($smtpConnection, 1024); |
130 | $lines .= $line; |
131 | } |
132 | /* Status: 0 = fatal |
133 | * 5 = ok |
134 | */ |
135 | $err_num = substr($line, 0, strpos($line, " ")); |
136 | switch ($err_num) { |
137 | case 500: $message = 'Syntax error; command not recognized'; |
138 | $status = 0; |
139 | break; |
140 | case 501: $message = 'Syntax error in parameters or arguments'; |
141 | $status = 0; |
142 | break; |
143 | case 502: $message = 'Command not implemented'; |
144 | $status = 0; |
145 | break; |
146 | case 503: $message = 'Bad sequence of commands'; |
147 | $status = 0; |
148 | break; |
149 | case 504: $message = 'Command parameter not implemented'; |
150 | $status = 0; |
151 | break; |
152 | case 211: $message = 'System status, or system help reply'; |
153 | $status = 5; |
154 | break; |
155 | case 214: $message = 'Help message'; |
156 | $status = 5; |
157 | break; |
158 | case 220: $message = 'Service ready'; |
159 | $status = 5; |
160 | break; |
161 | case 221: $message = 'Service closing transmission channel'; |
162 | $status = 5; |
163 | break; |
164 | case 421: $message = 'Service not available, closing chanel'; |
165 | $status = 0; |
166 | break; |
167 | case 235: return(5); |
168 | break; |
169 | case 250: $message = 'Requested mail action okay, completed'; |
170 | $status = 5; |
171 | break; |
172 | case 251: $message = 'User not local; will forward'; |
173 | $status = 5; |
174 | break; |
175 | case 334: return(5); break; |
176 | case 450: $message = 'Requested mail action not taken: mailbox unavailable'; |
177 | $status = 0; |
178 | break; |
179 | case 550: $message = 'Requested action not taken: mailbox unavailable'; |
180 | $status = 0; |
181 | break; |
182 | case 451: $message = 'Requested action aborted: error in processing'; |
183 | $status = 0; |
184 | break; |
185 | case 551: $message = 'User not local; please try forwarding'; |
186 | $status = 0; |
187 | break; |
188 | case 452: $message = 'Requested action not taken: insufficient system storage'; |
189 | $status = 0; |
190 | break; |
191 | case 552: $message = 'Requested mail action aborted: exceeding storage allocation'; |
192 | $status = 0; |
193 | break; |
194 | case 553: $message = 'Requested action not taken: mailbox name not allowed'; |
195 | $status = 0; |
196 | break; |
197 | case 354: $message = 'Start mail input; end with .'; |
198 | $status = 5; |
199 | break; |
200 | case 554: $message = 'Transaction failed'; |
201 | $status = 0; |
202 | break; |
203 | /* RFC 2554 */ |
204 | case 432: $message = 'A password transition is needed'; |
205 | $status = 0; |
206 | break; |
207 | case 534: $message = 'Authentication mechanism is too weak'; |
208 | $status = 0; |
209 | break; |
210 | case 538: $message = 'Encryption required for requested authentication mechanism'; |
211 | $status = 0; |
212 | break; |
213 | case 454: $message = 'Temmporary authentication failure'; |
214 | $status = 0; |
215 | break; |
216 | case 530: $message = 'Authentication required'; |
217 | $status = 0; |
218 | break; |
219 | /* end RFC2554 */ |
220 | default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines)); |
221 | $status = 0; |
222 | $err_num = '001'; |
223 | break; |
224 | } |
225 | $this->dlv_ret_nr = $err_num; |
226 | $this->dlv_msg = $message; |
227 | if ($status == 5) { |
228 | return false; |
229 | } |
230 | return true; |
231 | } |
232 | |
233 | function authPop($pop_server='', $pop_port='', $user, $pass) { |
234 | if (!$pop_port) { |
235 | $pop_port = 110; |
236 | } |
237 | if (!$pop_server) { |
238 | $pop_server = 'localhost'; |
239 | } |
240 | $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str); |
241 | if (!$popConnection) { |
242 | error_log("Error connecting to POP Server ($pop_server:$pop_port)" |
243 | . " $err_no : $err_str"); |
244 | } else { |
245 | $tmp = fgets($popConnection, 1024); /* banner */ |
246 | if (!eregi("^\+OK", $tmp, $regs)) { |
247 | return(0); |
248 | } |
249 | fputs($popConnection, "USER $user\r\n"); |
250 | $tmp = fgets($popConnection, 1024); |
251 | if (!eregi("^\+OK", $tmp, $regs)) { |
252 | return(0); |
253 | } |
254 | fputs($popConnection, 'PASS ' . $pass . "\r\n"); |
255 | $tmp = fgets($popConnection, 1024); |
256 | if (!eregi("^\+OK", $tmp, $regs)) { |
257 | return(0); |
258 | } |
259 | fputs($popConnection, "QUIT\r\n"); /* log off */ |
260 | fclose($popConnection); |
261 | } |
262 | } |
e1ee60fe |
263 | } |
5fe73b9f |
264 | |
e1ee60fe |
265 | ?> |