59177427 |
1 | <?php |
b8ea4ed6 |
2 | /** smtp.php |
3 | ** |
4 | ** This contains all the functions needed to send messages through |
36fa79c8 |
5 | ** an smtp server or sendmail. |
245a6892 |
6 | ** |
7 | ** $Id$ |
b8ea4ed6 |
8 | **/ |
9 | |
f435778e |
10 | if (defined('smtp_php')) |
11 | return; |
12 | define('smtp_php', true); |
13 | |
14 | include('../functions/addressbook.php'); |
15 | |
16 | global $username, $popuser, $domain; |
465db5d7 |
17 | |
31afdefb |
18 | // This should most probably go to some initialization... |
19 | if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) { |
20 | $popuser = $usernamedata[1]; |
21 | $domain = $usernamedata[2]; |
22 | unset($usernamedata); |
23 | } else { |
24 | $popuser = $username; |
25 | } |
26 | // We need domain for smtp |
27 | if (!$domain) |
ec9f1c41 |
28 | $domain = getenv('HOSTNAME'); |
31afdefb |
29 | |
df15de21 |
30 | // Returns true only if this message is multipart |
31 | function isMultipart () { |
4ba45d11 |
32 | global $attachments; |
33 | |
34 | if (count($attachments)>0) |
35 | return true; |
36 | else |
37 | return false; |
df15de21 |
38 | } |
39 | |
a4102fd7 |
40 | // looks up aliases in the addressbook and expands them to |
41 | // the full address. |
cb13511f |
42 | // Adds @$domain if it wasn't in the address book and if it |
43 | // doesn't have an @ symbol in it |
a4102fd7 |
44 | function expandAddrs ($array) { |
cb13511f |
45 | global $domain; |
46 | |
a4102fd7 |
47 | $abook = addressbook_init(); |
48 | for ($i=0; $i < count($array); $i++) { |
49 | $result = $abook->lookup($array[$i]); |
50 | $ret = ""; |
51 | if (isset($result['email'])) { |
52 | if (isset($result['name'])) { |
53 | $ret = '"'.$result['name'].'" '; |
54 | } |
55 | $ret .= '<'.$result['email'].'>'; |
56 | $array[$i] = $ret; |
57 | } |
18e206dd |
58 | else |
59 | { |
60 | $array[$i] = '<' . $array[$i] . '>'; |
61 | } |
cb13511f |
62 | |
63 | if (strpos($array[$i], '@') === false) { |
64 | $array[$i] .= '@' . $domain; |
65 | } |
a4102fd7 |
66 | } |
67 | return $array; |
68 | } |
69 | |
df15de21 |
70 | // Attach the files that are due to be attached |
4ba45d11 |
71 | function attachFiles ($fp) { |
c3c37167 |
72 | global $attachments, $attachment_dir; |
4ba45d11 |
73 | |
7b67334e |
74 | $length = 0; |
75 | |
a7d75834 |
76 | if (isMultipart()) { |
f972eb46 |
77 | foreach ($attachments as $info) |
78 | { |
7d50f13c |
79 | if (isset($info['type'])) |
80 | $filetype = $info['type']; |
81 | else |
ec9f1c41 |
82 | $filetype = 'application/octet-stream'; |
a7d75834 |
83 | |
ec9f1c41 |
84 | $header = '--'.mimeBoundary()."\r\n"; |
f972eb46 |
85 | $header .= "Content-Type: $filetype; name=\"" . |
86 | $info['remotefilename'] . "\"\r\n"; |
87 | $header .= "Content-Disposition: attachment; filename=\"" . |
88 | $info['remotefilename'] . "\"\r\n"; |
a7d75834 |
89 | $header .= "Content-Transfer-Encoding: base64\r\n\r\n"; |
90 | fputs ($fp, $header); |
91 | $length += strlen($header); |
92 | |
f972eb46 |
93 | $file = fopen ($attachment_dir . $info['localfilename'], 'r'); |
a7d75834 |
94 | while ($tmp = fread($file, 570)) { |
95 | $encoded = chunk_split(base64_encode($tmp)); |
96 | $length += strlen($encoded); |
97 | fputs ($fp, $encoded); |
98 | } |
99 | fclose ($file); |
7b67334e |
100 | } |
4ba45d11 |
101 | } |
7b67334e |
102 | |
103 | return $length; |
df15de21 |
104 | } |
105 | |
a7d75834 |
106 | // Delete files that are uploaded for attaching |
107 | function deleteAttachments() { |
108 | global $attachments, $attachment_dir; |
109 | |
110 | if (isMultipart()) { |
111 | reset($attachments); |
112 | while (list($localname, $remotename) = each($attachments)) { |
113 | if (!ereg ("\\/", $localname)) { |
114 | unlink ($attachment_dir.$localname); |
ec9f1c41 |
115 | unlink ($attachment_dir.$localname.'.info'); |
a7d75834 |
116 | } |
117 | } |
118 | } |
119 | } |
120 | |
df15de21 |
121 | // Return a nice MIME-boundary |
122 | function mimeBoundary () { |
7b67334e |
123 | static $mimeBoundaryString; |
df15de21 |
124 | |
125 | if ($mimeBoundaryString == "") { |
ed865adf |
126 | $mimeBoundaryString = "----=_" . |
fe53c3b9 |
127 | GenerateRandomString(60, '\'()+,-./:=?_', 7); |
465db5d7 |
128 | } |
df15de21 |
129 | |
130 | return $mimeBoundaryString; |
465db5d7 |
131 | } |
132 | |
24397423 |
133 | /* Time offset for correct timezone */ |
134 | function timezone () { |
b0d1b51e |
135 | global $invert_time; |
136 | |
ec9f1c41 |
137 | $diff_second = date('Z'); |
d47b2518 |
138 | if ($invert_time) |
139 | $diff_second = - $diff_second; |
24397423 |
140 | if ($diff_second > 0) |
ec9f1c41 |
141 | $sign = '+'; |
24397423 |
142 | else |
ec9f1c41 |
143 | $sign = '-'; |
24397423 |
144 | |
145 | $diff_second = abs($diff_second); |
146 | |
147 | $diff_hour = floor ($diff_second / 3600); |
148 | $diff_minute = floor (($diff_second-3600*$diff_hour) / 60); |
149 | |
ec9f1c41 |
150 | $zonename = '('.strftime('%Z').')'; |
24397423 |
151 | $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, $zonename); |
152 | return ($result); |
153 | } |
154 | |
df15de21 |
155 | /* Print all the needed RFC822 headers */ |
60994e13 |
156 | function write822Header ($fp, $t, $c, $b, $subject, $more_headers) { |
a7d75834 |
157 | global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT; |
31afdefb |
158 | global $data_dir, $username, $popuser, $domain, $version, $useSendmail; |
9949206d |
159 | global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR; |
a5363806 |
160 | global $REMOTE_HOST; |
465db5d7 |
161 | |
7b67334e |
162 | // Storing the header to make sure the header is the same |
163 | // everytime the header is printed. |
164 | static $header, $headerlength; |
165 | |
ec9f1c41 |
166 | if ($header == '') { |
a4102fd7 |
167 | $to = expandAddrs(parseAddrs($t)); |
168 | $cc = expandAddrs(parseAddrs($c)); |
169 | $bcc = expandAddrs(parseAddrs($b)); |
ec9f1c41 |
170 | $reply_to = getPref($data_dir, $username, 'reply_to'); |
171 | $from = getPref($data_dir, $username, 'full_name'); |
172 | $from_addr = getPref($data_dir, $username, 'email_address'); |
31afdefb |
173 | |
ec9f1c41 |
174 | if ($from_addr == '') |
175 | $from_addr = $popuser.'@'.$domain; |
7b67334e |
176 | |
177 | $to_list = getLineOfAddrs($to); |
178 | $cc_list = getLineOfAddrs($cc); |
179 | $bcc_list = getLineOfAddrs($bcc); |
8ae331c2 |
180 | |
78e230b7 |
181 | /* Encoding 8-bit characters and making from line */ |
d51894be |
182 | $subject = encodeHeader($subject); |
ec9f1c41 |
183 | if ($from == '') |
7b67334e |
184 | $from = "<$from_addr>"; |
185 | else |
ec9f1c41 |
186 | $from = '"' . encodeHeader($from) . "\" <$from_addr>"; |
7b67334e |
187 | |
a7d75834 |
188 | /* This creates an RFC 822 date */ |
7b67334e |
189 | $date = date("D, j M Y H:i:s ", mktime()) . timezone(); |
a7d75834 |
190 | |
191 | /* Create a message-id */ |
ec9f1c41 |
192 | $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.'; |
193 | $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>'; |
7b67334e |
194 | |
195 | /* Make an RFC822 Received: line */ |
a5363806 |
196 | if (isset($REMOTE_HOST)) |
8a2848f0 |
197 | $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])"; |
a5363806 |
198 | else |
8a2848f0 |
199 | $received_from = $REMOTE_ADDR; |
a5363806 |
200 | |
9949206d |
201 | if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) { |
ec9f1c41 |
202 | if ($HTTP_X_FORWARDED_FOR == '') |
203 | $HTTP_X_FORWARDED_FOR = 'unknown'; |
9949206d |
204 | $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)"; |
205 | } |
a5363806 |
206 | |
8a2848f0 |
207 | $header = "Received: from $received_from\r\n"; |
208 | $header .= " (SquirrelMail authenticated user $username)\r\n"; |
209 | $header .= " by $SERVER_NAME with HTTP;\r\n"; |
210 | $header .= " $date\r\n"; |
7b67334e |
211 | |
a7d75834 |
212 | /* Insert the rest of the header fields */ |
213 | $header .= "Message-ID: $message_id\r\n"; |
7b67334e |
214 | $header .= "Date: $date\r\n"; |
215 | $header .= "Subject: $subject\r\n"; |
216 | $header .= "From: $from\r\n"; |
18e206dd |
217 | $header .= "To: $to_list\r\n"; // Who it's TO |
60994e13 |
218 | |
219 | /* Insert headers from the $more_headers array */ |
220 | if(is_array($more_headers)) { |
221 | reset($more_headers); |
222 | while(list($h_name, $h_val) = each($more_headers)) { |
223 | $header .= sprintf("%s: %s\r\n", $h_name, $h_val); |
224 | } |
225 | } |
226 | |
7b67334e |
227 | if ($cc_list) { |
228 | $header .= "Cc: $cc_list\r\n"; // Who the CCs are |
df15de21 |
229 | } |
7b67334e |
230 | |
ec9f1c41 |
231 | if ($reply_to != '') |
7b67334e |
232 | $header .= "Reply-To: $reply_to\r\n"; |
233 | |
234 | if ($useSendmail) { |
235 | if ($bcc_list) { |
236 | // BCCs is removed from header by sendmail |
237 | $header .= "Bcc: $bcc_list\r\n"; |
238 | } |
239 | } |
240 | |
241 | $header .= "X-Mailer: SquirrelMail (version $version)\r\n"; // Identify SquirrelMail |
242 | |
243 | // Do the MIME-stuff |
f8308f16 |
244 | $header .= "MIME-Version: 1.0\r\n"; |
7b67334e |
245 | |
246 | if (isMultipart()) { |
ec9f1c41 |
247 | $header .= 'Content-Type: multipart/mixed; boundary="'; |
7b67334e |
248 | $header .= mimeBoundary(); |
249 | $header .= "\"\r\n"; |
250 | } else { |
ec9f1c41 |
251 | if ($default_charset != '') |
17ce8467 |
252 | $header .= "Content-Type: text/plain; charset=$default_charset\r\n"; |
253 | else |
254 | $header .= "Content-Type: text/plain;\r\n"; |
7b67334e |
255 | $header .= "Content-Transfer-Encoding: 8bit\r\n"; |
256 | } |
257 | $header .= "\r\n"; // One blank line to separate header and body |
df15de21 |
258 | |
7b67334e |
259 | $headerlength = strlen($header); |
260 | } |
261 | |
262 | // Write the header |
263 | fputs ($fp, $header); |
df15de21 |
264 | |
7b67334e |
265 | return $headerlength; |
266 | } |
df15de21 |
267 | |
7b67334e |
268 | // Send the body |
269 | function writeBody ($fp, $passedBody) { |
17ce8467 |
270 | global $default_charset; |
271 | |
7b67334e |
272 | $attachmentlength = 0; |
273 | |
df15de21 |
274 | if (isMultipart()) { |
ec9f1c41 |
275 | $body = '--'.mimeBoundary()."\r\n"; |
17ce8467 |
276 | |
277 | if ($default_charset != "") |
278 | $body .= "Content-Type: text/plain; charset=$default_charset\r\n"; |
279 | else |
280 | $body .= "Content-Type: text/plain\r\n"; |
281 | |
7b67334e |
282 | $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n"; |
d51894be |
283 | $body .= $passedBody . "\r\n\r\n"; |
7b67334e |
284 | fputs ($fp, $body); |
285 | |
6441f7c6 |
286 | $attachmentlength = attachFiles($fp); |
7b67334e |
287 | |
f8cfeb5c |
288 | if (!isset($postbody)) $postbody = ""; |
7b67334e |
289 | $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n"; |
290 | fputs ($fp, $postbody); |
df15de21 |
291 | } else { |
d51894be |
292 | $body = $passedBody . "\r\n"; |
7b67334e |
293 | fputs ($fp, $body); |
294 | $postbody = "\r\n"; |
295 | fputs ($fp, $postbody); |
df15de21 |
296 | } |
df15de21 |
297 | |
7b67334e |
298 | return (strlen($body) + strlen($postbody) + $attachmentlength); |
c99c5b31 |
299 | } |
465db5d7 |
300 | |
c99c5b31 |
301 | // Send mail using the sendmail command |
60994e13 |
302 | function sendSendmail($t, $c, $b, $subject, $body, $more_headers) { |
31afdefb |
303 | global $sendmail_path, $popuser, $username, $domain; |
7b67334e |
304 | |
6ebf8b30 |
305 | // Build envelope sender address. Make sure it doesn't contain |
306 | // spaces or other "weird" chars that would allow a user to |
307 | // exploit the shell/pipe it is used in. |
31afdefb |
308 | $envelopefrom = "$popuser@$domain"; |
ec9f1c41 |
309 | $envelopefrom = ereg_replace("[[:blank:]]",'', $envelopefrom); |
310 | $envelopefrom = ereg_replace("[[:space:]]",'', $envelopefrom); |
311 | $envelopefrom = ereg_replace("[[:cntrl:]]",'', $envelopefrom); |
6ebf8b30 |
312 | |
c99c5b31 |
313 | // open pipe to sendmail |
ec9f1c41 |
314 | $fp = popen (escapeshellcmd("$sendmail_path -t -f$envelopefrom"), 'w'); |
c99c5b31 |
315 | |
60994e13 |
316 | $headerlength = write822Header ($fp, $t, $c, $b, $subject, $more_headers); |
7b67334e |
317 | $bodylength = writeBody($fp, $body); |
465db5d7 |
318 | |
319 | pclose($fp); |
7b67334e |
320 | |
0775b17f |
321 | return ($headerlength + $bodylength); |
465db5d7 |
322 | } |
323 | |
b8ea4ed6 |
324 | function smtpReadData($smtpConnection) { |
325 | $read = fgets($smtpConnection, 1024); |
326 | $counter = 0; |
327 | while ($read) { |
ec9f1c41 |
328 | echo $read . '<BR>'; |
b8ea4ed6 |
329 | $data[$counter] = $read; |
330 | $read = fgets($smtpConnection, 1024); |
331 | $counter++; |
332 | } |
333 | } |
334 | |
60994e13 |
335 | function sendSMTP($t, $c, $b, $subject, $body, $more_headers) { |
31afdefb |
336 | global $username, $popuser, $domain, $version, $smtpServerAddress, $smtpPort, |
14c7eedb |
337 | $data_dir, $color, $use_authenticated_smtp; |
7831268e |
338 | |
a4102fd7 |
339 | $to = expandAddrs(parseAddrs($t)); |
340 | $cc = expandAddrs(parseAddrs($c)); |
341 | $bcc = expandAddrs(parseAddrs($b)); |
ec9f1c41 |
342 | $from_addr = getPref($data_dir, $username, 'email_address'); |
356d7825 |
343 | |
31afdefb |
344 | if (!$from_addr) |
345 | $from_addr = "$popuser@$domain"; |
d3cdb279 |
346 | |
c175e2e4 |
347 | $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString); |
b8ea4ed6 |
348 | if (!$smtpConnection) { |
ec9f1c41 |
349 | echo 'Error connecting to SMTP Server.<br>'; |
b8ea4ed6 |
350 | echo "$errorNumber : $errorString<br>"; |
351 | exit; |
352 | } |
f8a9394d |
353 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
354 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
b8ea4ed6 |
355 | |
40ee9452 |
356 | $to_list = getLineOfAddrs($to); |
357 | $cc_list = getLineOfAddrs($cc); |
358 | |
359 | /** Lets introduce ourselves */ |
d402e33e |
360 | if (! isset ($use_authenticated_smtp) || $use_authenticated_smtp == false) { |
d2f4c914 |
361 | fputs($smtpConnection, "HELO $domain\r\n"); |
362 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
363 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
364 | } else { |
365 | fputs($smtpConnection, "EHLO $domain\r\n"); |
366 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
367 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
368 | |
369 | fputs($smtpConnection, "AUTH LOGIN\r\n"); |
370 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
371 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
372 | |
373 | fputs($smtpConnection, base64_encode ($username) . "\r\n"); |
374 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
375 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
376 | |
377 | fputs($smtpConnection, base64_encode ($OneTimePadDecrypt($key, $onetimepad)) . "\r\n"); |
378 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
379 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
d2f4c914 |
380 | } |
7831268e |
381 | |
40ee9452 |
382 | /** Ok, who is sending the message? */ |
f89627f3 |
383 | fputs($smtpConnection, "MAIL FROM: <$from_addr>\r\n"); |
f8a9394d |
384 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
385 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
b8ea4ed6 |
386 | |
40ee9452 |
387 | /** send who the recipients are */ |
388 | for ($i = 0; $i < count($to); $i++) { |
18e206dd |
389 | fputs($smtpConnection, "RCPT TO: $to[$i]\r\n"); |
f8a9394d |
390 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
391 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
40ee9452 |
392 | } |
393 | for ($i = 0; $i < count($cc); $i++) { |
b4338e67 |
394 | fputs($smtpConnection, "RCPT TO: $cc[$i]\r\n"); |
f8a9394d |
395 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
396 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
40ee9452 |
397 | } |
398 | for ($i = 0; $i < count($bcc); $i++) { |
b4338e67 |
399 | fputs($smtpConnection, "RCPT TO: $bcc[$i]\r\n"); |
f8a9394d |
400 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
401 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
40ee9452 |
402 | } |
b8ea4ed6 |
403 | |
40ee9452 |
404 | /** Lets start sending the actual message */ |
3379d740 |
405 | fputs($smtpConnection, "DATA\r\n"); |
f8a9394d |
406 | $tmp = fgets($smtpConnection, 1024); |
d5f34863 |
407 | if (errorCheck($tmp, $smtpConnection)!=5) return(0); |
7831268e |
408 | |
7b67334e |
409 | // Send the message |
60994e13 |
410 | $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject, $more_headers); |
7b67334e |
411 | $bodylength = writeBody($smtpConnection, $body); |
7831268e |
412 | |
3379d740 |
413 | fputs($smtpConnection, ".\r\n"); // end the DATA part |
f8a9394d |
414 | $tmp = fgets($smtpConnection, 1024); |
354eb441 |
415 | $num = errorCheck($tmp, $smtpConnection, true); |
3021b626 |
416 | if ($num != 250) { |
d5f34863 |
417 | $tmp = nl2br(htmlspecialchars($tmp)); |
418 | displayPageHeader($color, 'None'); |
419 | include ("../functions/display_messages.php"); |
d5f34863 |
420 | $msg = "Message not sent!<br>\nReason given: $tmp"; |
421 | plain_error_message($msg, $color); |
422 | return(0); |
3021b626 |
423 | } |
d3cdb279 |
424 | |
3379d740 |
425 | fputs($smtpConnection, "QUIT\r\n"); // log off |
78509c54 |
426 | |
427 | fclose($smtpConnection); |
7b67334e |
428 | |
429 | return ($headerlength + $bodylength); |
b8ea4ed6 |
430 | } |
3021b626 |
431 | |
432 | |
354eb441 |
433 | function errorCheck($line, $smtpConnection, $verbose = false) { |
28010579 |
434 | global $color; |
f435778e |
435 | include '../functions/page_header.php'; |
776c7431 |
436 | |
f8a9394d |
437 | // Read new lines on a multiline response |
438 | $lines = $line; |
439 | while(ereg("^[0-9]+-", $line)) { |
440 | $line = fgets($smtpConnection, 1024); |
441 | $lines .= $line; |
442 | } |
443 | |
3021b626 |
444 | // Status: 0 = fatal |
445 | // 5 = ok |
446 | |
447 | $err_num = substr($line, 0, strpos($line, " ")); |
448 | switch ($err_num) { |
ec9f1c41 |
449 | case 500: $message = 'Syntax error; command not recognized'; |
3021b626 |
450 | $status = 0; |
451 | break; |
ec9f1c41 |
452 | case 501: $message = 'Syntax error in parameters or arguments'; |
3021b626 |
453 | $status = 0; |
454 | break; |
ec9f1c41 |
455 | case 502: $message = 'Command not implemented'; |
3021b626 |
456 | $status = 0; |
457 | break; |
ec9f1c41 |
458 | case 503: $message = 'Bad sequence of commands'; |
3021b626 |
459 | $status = 0; |
460 | break; |
ec9f1c41 |
461 | case 504: $message = 'Command parameter not implemented'; |
3021b626 |
462 | $status = 0; |
463 | break; |
464 | |
465 | |
ec9f1c41 |
466 | case 211: $message = 'System status, or system help reply'; |
3021b626 |
467 | $status = 5; |
468 | break; |
ec9f1c41 |
469 | case 214: $message = 'Help message'; |
3021b626 |
470 | $status = 5; |
471 | break; |
472 | |
473 | |
ec9f1c41 |
474 | case 220: $message = 'Service ready'; |
3021b626 |
475 | $status = 5; |
476 | break; |
ec9f1c41 |
477 | case 221: $message = 'Service closing transmission channel'; |
3021b626 |
478 | $status = 5; |
479 | break; |
ec9f1c41 |
480 | case 421: $message = 'Service not available, closing chanel'; |
3021b626 |
481 | $status = 0; |
482 | break; |
483 | |
d5f34863 |
484 | case 235: return(5); break; |
ec9f1c41 |
485 | case 250: $message = 'Requested mail action okay, completed'; |
3021b626 |
486 | $status = 5; |
487 | break; |
ec9f1c41 |
488 | case 251: $message = 'User not local; will forward'; |
3021b626 |
489 | $status = 5; |
490 | break; |
d5f34863 |
491 | case 334: return(5); break; |
ec9f1c41 |
492 | case 450: $message = 'Requested mail action not taken: mailbox unavailable'; |
3021b626 |
493 | $status = 0; |
494 | break; |
ec9f1c41 |
495 | case 550: $message = 'Requested action not taken: mailbox unavailable'; |
3021b626 |
496 | $status = 0; |
497 | break; |
ec9f1c41 |
498 | case 451: $message = 'Requested action aborted: error in processing'; |
3021b626 |
499 | $status = 0; |
500 | break; |
ec9f1c41 |
501 | case 551: $message = 'User not local; please try forwarding'; |
3021b626 |
502 | $status = 0; |
503 | break; |
ec9f1c41 |
504 | case 452: $message = 'Requested action not taken: insufficient system storage'; |
3021b626 |
505 | $status = 0; |
506 | break; |
ec9f1c41 |
507 | case 552: $message = 'Requested mail action aborted: exceeding storage allocation'; |
3021b626 |
508 | $status = 0; |
509 | break; |
ec9f1c41 |
510 | case 553: $message = 'Requested action not taken: mailbox name not allowed'; |
3021b626 |
511 | $status = 0; |
512 | break; |
ec9f1c41 |
513 | case 354: $message = 'Start mail input; end with .'; |
3021b626 |
514 | $status = 5; |
515 | break; |
ec9f1c41 |
516 | case 554: $message = 'Transaction failed'; |
3021b626 |
517 | $status = 0; |
518 | break; |
434666c7 |
519 | default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines)); |
3021b626 |
520 | $status = 0; |
ec9f1c41 |
521 | $error_num = '001'; |
3021b626 |
522 | break; |
523 | } |
524 | |
525 | if ($status == 0) { |
ec9f1c41 |
526 | displayPageHeader($color, 'None'); |
d5f34863 |
527 | include ("../functions/display_messages.php"); |
f8a9394d |
528 | $lines = nl2br(htmlspecialchars($lines)); |
d5f34863 |
529 | $msg = $message . "<br>\nServer replied: $lines"; |
530 | plain_error_message($msg, $color); |
3021b626 |
531 | } |
354eb441 |
532 | if (! $verbose) return $status; |
3021b626 |
533 | return $err_num; |
534 | } |
df15de21 |
535 | |
966286ae |
536 | function sendMessage($t, $c, $b, $subject, $body, $reply_id) { |
d2f4c914 |
537 | global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad; |
6441f7c6 |
538 | global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort; |
60994e13 |
539 | $more_headers = Array(); |
d1b8b679 |
540 | |
966286ae |
541 | $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1); |
60994e13 |
542 | |
214e1343 |
543 | if (isset($reply_id) && $reply_id) { |
966286ae |
544 | sqimap_mailbox_select ($imap_stream, $mailbox); |
ec9f1c41 |
545 | sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, 'Answered'); |
60994e13 |
546 | |
c95df380 |
547 | // Insert In-Reply-To and References headers if the |
548 | // message-id of the message we reply to is set (longer than "<>") |
549 | // The References header should really be the old Referenced header |
550 | // with the message ID appended, but it can be only the message ID too. |
551 | $hdr = sqimap_get_small_header ($imap_stream, $reply_id, false); |
552 | if(strlen($hdr->message_id) > 2) { |
ec9f1c41 |
553 | $more_headers['In-Reply-To'] = $hdr->message_id; |
554 | $more_headers['References'] = $hdr->message_id; |
c95df380 |
555 | } |
966286ae |
556 | } |
8ae331c2 |
557 | |
af8eba43 |
558 | // In order to remove the problem of users not able to create |
559 | // messages with "." on a blank line, RFC821 has made provision |
560 | // in section 4.5.2 (Transparency). |
146e0c45 |
561 | $body = ereg_replace("\n\\.", "\n..", $body); |
562 | $body = ereg_replace("^\\.", "..", $body); |
af8eba43 |
563 | |
8ae331c2 |
564 | // this is to catch all plain \n instances and |
565 | // replace them with \r\n. |
566 | $body = ereg_replace("\r\n", "\n", $body); |
567 | $body = ereg_replace("\n", "\r\n", $body); |
3a8c414d |
568 | |
8ae331c2 |
569 | if ($useSendmail) { |
60994e13 |
570 | $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers); |
df15de21 |
571 | } else { |
60994e13 |
572 | $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers); |
df15de21 |
573 | } |
d1b8b679 |
574 | |
2966f172 |
575 | if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) { |
576 | sqimap_append ($imap_stream, $sent_folder, $length); |
60994e13 |
577 | write822Header ($imap_stream, $t, $c, $b, $subject, $more_headers); |
8ae331c2 |
578 | writeBody ($imap_stream, $body); |
2966f172 |
579 | sqimap_append_done ($imap_stream); |
8ae331c2 |
580 | } |
581 | sqimap_logout($imap_stream); |
a7d75834 |
582 | // Delete the files uploaded for attaching (if any). |
f95d9e7b |
583 | // only if $length != 0 (if there was no error) |
584 | if ($length) |
585 | ClearAttachments(); |
586 | |
587 | return $length; |
df15de21 |
588 | } |
8ae331c2 |
589 | |
434666c7 |
590 | ?> |