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