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