Workaround for Courier when sending mixed messages with nested multipart/alternative...
[squirrelmail.git] / class / deliver / Deliver.class.php
1 <?php
2 /**
3 * Deliver.class.php
4 *
5 * Copyright (c) 1999-2005 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * This contains all the functions needed to send messages through
9 * a delivery backend.
10 *
11 * @version $Id$
12 * @author Marc Groot Koerkamp
13 * @package squirrelmail
14 */
15
16 /**
17 * Deliver Class - called to actually deliver the message
18 *
19 * This class is called by compose.php and other code that needs
20 * to send messages. All delivery functionality should be centralized
21 * in this class.
22 *
23 * Do not place UI code in this class, as UI code should be placed in templates
24 * going forward.
25 *
26 * @author Marc Groot Koerkamp
27 * @package squirrelmail
28 */
29 class Deliver {
30
31 /**
32 * function mail - send the message parts to the SMTP stream
33 *
34 * @param Message $message Message class to send
35 * @param resource $stream file handle to the SMTP stream
36 *
37 * @return integer $raw_length
38 */
39 function mail($message, $stream=false) {
40 $rfc822_header = $message->rfc822_header;
41 if (count($message->entities)) {
42 $boundary = $this->mimeBoundary();
43 $rfc822_header->content_type->properties['boundary']='"'.$boundary.'"';
44 } else {
45 $boundary='';
46 }
47 $raw_length = 0;
48 $reply_rfc822_header = (isset($message->reply_rfc822_header)
49 ? $message->reply_rfc822_header : '');
50 $header = $this->prepareRFC822_Header($rfc822_header, $reply_rfc822_header, $raw_length);
51
52 if ($stream) {
53 $this->preWriteToStream($header);
54 $this->writeToStream($stream, $header);
55 }
56 $this->writeBody($message, $stream, $raw_length, $boundary);
57 return $raw_length;
58 }
59
60 /**
61 * function writeBody - generate and write the mime boundaries around each part to the stream
62 *
63 * Recursively formats and writes the MIME boundaries of the $message
64 * to the output stream.
65 *
66 * @param Message $message Message object to transform
67 * @param resource $stream SMTP output stream
68 * @param integer &$length_raw raw length of the message (part)
69 * as returned by mail fn
70 * @param string $boundary custom boundary to call, usually for subparts
71 *
72 * @return void
73 */
74 function writeBody($message, $stream, &$length_raw, $boundary='') {
75 // calculate boundary in case of multidimensional mime structures
76 if ($boundary && $message->entity_id && count($message->entities)) {
77 if (strpos($boundary,'_part_')) {
78 $boundary = substr($boundary,0,strpos($boundary,'_part_'));
79
80 // the next four lines use strrev to reverse any nested boundaries
81 // as a workaround for Courier-IMAP, which will stop parsing
82 // when it sees the original outer boundary string and ignore
83 // our "_part_..." addition to that string. a bug report has been
84 // made, but in the meantime...
85 //
86 } else if (strpos($boundary,'_trap_')) {
87 $boundary = substr(strrev($boundary),0,strpos(strrev($boundary),'_part_'));
88 }
89 $boundary_new = strrev($boundary . '_part_'.$message->entity_id);
90 } else {
91 $boundary_new = $boundary;
92 }
93 if ($boundary && !$message->rfc822_header) {
94 $s = '--'.$boundary."\r\n";
95 $s .= $this->prepareMIME_Header($message, $boundary_new);
96 $length_raw += strlen($s);
97 if ($stream) {
98 $this->preWriteToStream($s);
99 $this->writeToStream($stream, $s);
100 }
101 }
102 $this->writeBodyPart($message, $stream, $length_raw);
103
104 $last = false;
105 for ($i=0, $entCount=count($message->entities);$i<$entCount;$i++) {
106 $msg = $this->writeBody($message->entities[$i], $stream, $length_raw, $boundary_new);
107 if ($i == $entCount-1) $last = true;
108 }
109 if ($boundary && $last) {
110 $s = "--".$boundary_new."--\r\n\r\n";
111 $length_raw += strlen($s);
112 if ($stream) {
113 $this->preWriteToStream($s);
114 $this->writeToStream($stream, $s);
115 }
116 }
117 }
118
119 /**
120 * function writeBodyPart - write each individual mimepart
121 *
122 * Recursively called by WriteBody to write each mime part to the SMTP stream
123 *
124 * @param Message $message Message object to transform
125 * @param resource $stream SMTP output stream
126 * @param integer &$length length of the message part
127 * as returned by mail fn
128 *
129 * @return void
130 */
131 function writeBodyPart($message, $stream, &$length) {
132 if ($message->mime_header) {
133 $type0 = $message->mime_header->type0;
134 } else {
135 $type0 = $message->rfc822_header->content_type->type0;
136 }
137
138 $body_part_trailing = $last = '';
139 switch ($type0)
140 {
141 case 'text':
142 case 'message':
143 if ($message->body_part) {
144 $body_part = $message->body_part;
145 // remove NUL characters
146 $body_part = str_replace("\0",'',$body_part);
147 $length += $this->clean_crlf($body_part);
148 if ($stream) {
149 $this->preWriteToStream($body_part);
150 $this->writeToStream($stream, $body_part);
151 }
152 $last = $body_part;
153 } elseif ($message->att_local_name) {
154 $filename = $message->att_local_name;
155 $file = fopen ($filename, 'rb');
156 while ($body_part = fgets($file, 4096)) {
157 $length += $this->clean_crlf($body_part);
158 if ($stream) {
159 $this->preWriteToStream($body_part);
160 $this->writeToStream($stream, $body_part);
161 }
162 $last = $body_part;
163 }
164 fclose($file);
165 }
166 break;
167 default:
168 if ($message->body_part) {
169 $body_part = $message->body_part;
170 $length += $this->clean_crlf($body_part);
171 if ($stream) {
172 $this->writeToStream($stream, $body_part);
173 }
174 } elseif ($message->att_local_name) {
175 $filename = $message->att_local_name;
176 $file = fopen ($filename, 'rb');
177 while ($tmp = fread($file, 570)) {
178 $body_part = chunk_split(base64_encode($tmp));
179 $length += $this->clean_crlf($body_part);
180 if ($stream) {
181 $this->writeToStream($stream, $body_part);
182 }
183 }
184 fclose($file);
185 }
186 break;
187 }
188 $body_part_trailing = '';
189 if ($last && substr($last,-1) != "\n") {
190 $body_part_trailing = "\r\n";
191 }
192 if ($body_part_trailing) {
193 $length += strlen($body_part_trailing);
194 if ($stream) {
195 $this->preWriteToStream($body_part_trailing);
196 $this->writeToStream($stream, $body_part_trailing);
197 }
198 }
199 }
200
201 /**
202 * function clean_crlf - change linefeeds and newlines to legal characters
203 *
204 * The SMTP format only allows CRLF as line terminators.
205 * This function replaces illegal teminators with the correct terminator.
206 *
207 * @param string &$s string to clean linefeeds on
208 *
209 * @return void
210 */
211 function clean_crlf(&$s) {
212 $s = str_replace("\r\n", "\n", $s);
213 $s = str_replace("\r", "\n", $s);
214 $s = str_replace("\n", "\r\n", $s);
215 return strlen($s);
216 }
217
218 /**
219 * function strip_crlf - strip linefeeds and newlines from a string
220 *
221 * The SMTP format only allows CRLF as line terminators.
222 * This function strips all line terminators from the string.
223 *
224 * @param string &$s string to clean linefeeds on
225 *
226 * @return void
227 */
228 function strip_crlf(&$s) {
229 $s = str_replace("\r\n ", '', $s);
230 $s = str_replace("\r", '', $s);
231 $s = str_replace("\n", '', $s);
232 }
233
234 /**
235 * function preWriteToStream - reserved for extended functionality
236 *
237 * This function is not yet implemented.
238 * Reserved for extended functionality.
239 *
240 * @param string &$s string to operate on
241 *
242 * @return void
243 */
244 function preWriteToStream(&$s) {
245 }
246
247 /**
248 * function writeToStream - write data to the SMTP stream
249 *
250 * @param resource $stream SMTP output stream
251 * @param string $data string with data to send to the SMTP stream
252 *
253 * @return void
254 */
255 function writeToStream($stream, $data) {
256 fputs($stream, $data);
257 }
258
259 /**
260 * function initStream - reserved for extended functionality
261 *
262 * This function is not yet implemented.
263 * Reserved for extended functionality.
264 *
265 * @param Message $message Message object
266 * @param string $host host name or IP to connect to
267 * @param string $user username to log into the SMTP server with
268 * @param string $pass password to log into the SMTP server with
269 * @param integer $length
270 *
271 * @return handle $stream file handle resource to SMTP stream
272 */
273 function initStream($message, $length=0, $host='', $port='', $user='', $pass='') {
274 return $stream;
275 }
276
277 /**
278 * function getBCC - reserved for extended functionality
279 *
280 * This function is not yet implemented.
281 * Reserved for extended functionality.
282 *
283 */
284 function getBCC() {
285 return false;
286 }
287
288 /**
289 * function prepareMIME_Header - creates the mime header
290 *
291 * @param Message $message Message object to act on
292 * @param string $boundary mime boundary from fn MimeBoundary
293 *
294 * @return string $header properly formatted mime header
295 */
296 function prepareMIME_Header($message, $boundary) {
297 $mime_header = $message->mime_header;
298 $rn="\r\n";
299 $header = array();
300
301 $contenttype = 'Content-Type: '. $mime_header->type0 .'/'.
302 $mime_header->type1;
303 if (count($message->entities)) {
304 $contenttype .= ';' . 'boundary="'.$boundary.'"';
305 }
306 if (isset($mime_header->parameters['name'])) {
307 $contenttype .= '; name="'.
308 encodeHeader($mime_header->parameters['name']). '"';
309 }
310 if (isset($mime_header->parameters['charset'])) {
311 $charset = $mime_header->parameters['charset'];
312 $contenttype .= '; charset="'.
313 encodeHeader($charset). '"';
314 }
315
316 $header[] = $contenttype . $rn;
317 if ($mime_header->description) {
318 $header[] = 'Content-Description: ' . $mime_header->description . $rn;
319 }
320 if ($mime_header->encoding) {
321 $header[] = 'Content-Transfer-Encoding: ' . $mime_header->encoding . $rn;
322 } else {
323 if ($mime_header->type0 == 'text' || $mime_header->type0 == 'message') {
324 $header[] = 'Content-Transfer-Encoding: 8bit' . $rn;
325 } else {
326 $header[] = 'Content-Transfer-Encoding: base64' . $rn;
327 }
328 }
329 if ($mime_header->id) {
330 $header[] = 'Content-ID: ' . $mime_header->id . $rn;
331 }
332 if ($mime_header->disposition) {
333 $disposition = $mime_header->disposition;
334 $contentdisp = 'Content-Disposition: ' . $disposition->name;
335 if ($disposition->getProperty('filename')) {
336 $contentdisp .= '; filename="'.
337 encodeHeader($disposition->getProperty('filename')). '"';
338 }
339 $header[] = $contentdisp . $rn;
340 }
341 if ($mime_header->md5) {
342 $header[] = 'Content-MD5: ' . $mime_header->md5 . $rn;
343 }
344 if ($mime_header->language) {
345 $header[] = 'Content-Language: ' . $mime_header->language . $rn;
346 }
347
348 $cnt = count($header);
349 $hdr_s = '';
350 for ($i = 0 ; $i < $cnt ; $i++) {
351 $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4));
352 }
353 $header = $hdr_s;
354 $header .= $rn; /* One blank line to separate mimeheader and body-entity */
355 return $header;
356 }
357
358 /**
359 * function prepareRFC822_Header - prepares the RFC822 header string from Rfc822Header object(s)
360 *
361 * This function takes the Rfc822Header object(s) and formats them
362 * into the RFC822Header string to send to the SMTP server as part
363 * of the SMTP message.
364 *
365 * @param Rfc822Header $rfc822_header
366 * @param Rfc822Header $reply_rfc822_header
367 * @param integer &$raw_length length of the message
368 *
369 * @return string $header
370 */
371 function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) {
372 global $domain, $version, $username, $skip_SM_header;
373
374 /* if server var SERVER_NAME not available, use $domain */
375 if(!sqGetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER)) {
376 $SERVER_NAME = $domain;
377 }
378
379 sqGetGlobalVar('REMOTE_ADDR', $REMOTE_ADDR, SQ_SERVER);
380 sqGetGlobalVar('REMOTE_PORT', $REMOTE_PORT, SQ_SERVER);
381 sqGetGlobalVar('REMOTE_HOST', $REMOTE_HOST, SQ_SERVER);
382 sqGetGlobalVar('HTTP_VIA', $HTTP_VIA, SQ_SERVER);
383 sqGetGlobalVar('HTTP_X_FORWARDED_FOR', $HTTP_X_FORWARDED_FOR, SQ_SERVER);
384
385 $rn = "\r\n";
386
387 /* This creates an RFC 822 date */
388 $date = date('D, j M Y H:i:s ', mktime()) . $this->timezone();
389 /* Create a message-id */
390 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
391 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
392 /* Make an RFC822 Received: line */
393 if (isset($REMOTE_HOST)) {
394 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
395 } else {
396 $received_from = $REMOTE_ADDR;
397 }
398 if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) {
399 if (!isset($HTTP_X_FORWARDED_FOR) || $HTTP_X_FORWARDED_FOR == '') {
400 $HTTP_X_FORWARDED_FOR = 'unknown';
401 }
402 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
403 }
404 $header = array();
405 if ( !isset($skip_SM_header) || !$skip_SM_header )
406 {
407 $header[] = "Received: from $received_from" . $rn;
408 $header[] = " (SquirrelMail authenticated user $username)" . $rn;
409 $header[] = " by $SERVER_NAME with HTTP;" . $rn;
410 $header[] = " $date" . $rn;
411 }
412 /* Insert the rest of the header fields */
413 $header[] = 'Message-ID: '. $message_id . $rn;
414 if ($reply_rfc822_header->message_id) {
415 $rep_message_id = $reply_rfc822_header->message_id;
416 // $this->strip_crlf($message_id);
417 $header[] = 'In-Reply-To: '.$rep_message_id . $rn;
418 $references = $this->calculate_references($reply_rfc822_header);
419 $header[] = 'References: '.$references . $rn;
420 }
421 $header[] = "Date: $date" . $rn;
422 $header[] = 'Subject: '.encodeHeader($rfc822_header->subject) . $rn;
423 $header[] = 'From: '. $rfc822_header->getAddr_s('from',",$rn ",true) . $rn;
424
425 // folding address list [From|To|Cc|Bcc] happens by using ",$rn<space>" as delimiter
426 // Do not use foldLine for that.
427
428 // RFC2822 if from contains more then 1 address
429 if (count($rfc822_header->from) > 1) {
430 $header[] = 'Sender: '. $rfc822_header->getAddr_s('sender',',',true) . $rn;
431 }
432 if (count($rfc822_header->to)) {
433 $header[] = 'To: '. $rfc822_header->getAddr_s('to',",$rn ",true) . $rn;
434 }
435 if (count($rfc822_header->cc)) {
436 $header[] = 'Cc: '. $rfc822_header->getAddr_s('cc',",$rn ",true) . $rn;
437 }
438 if (count($rfc822_header->reply_to)) {
439 $header[] = 'Reply-To: '. $rfc822_header->getAddr_s('reply_to',',',true) . $rn;
440 }
441 /* Sendmail should return true. Default = false */
442 $bcc = $this->getBcc();
443 if (count($rfc822_header->bcc)) {
444 $s = 'Bcc: '. $rfc822_header->getAddr_s('bcc',",$rn ",true) . $rn;
445 if (!$bcc) {
446 $raw_length += strlen($s);
447 } else {
448 $header[] = $s;
449 }
450 }
451 /* Identify SquirrelMail */
452 $header[] = 'User-Agent: SquirrelMail/' . $version . $rn;
453 /* Do the MIME-stuff */
454 $header[] = 'MIME-Version: 1.0' . $rn;
455 $contenttype = 'Content-Type: '. $rfc822_header->content_type->type0 .'/'.
456 $rfc822_header->content_type->type1;
457 if (count($rfc822_header->content_type->properties)) {
458 foreach ($rfc822_header->content_type->properties as $k => $v) {
459 if ($k && $v) {
460 $contenttype .= ';' .$k.'='.$v;
461 }
462 }
463 }
464 $header[] = $contenttype . $rn;
465 if ($encoding = $rfc822_header->encoding) {
466 $header[] = 'Content-Transfer-Encoding: ' . $encoding . $rn;
467 }
468 if ($rfc822_header->dnt) {
469 $dnt = $rfc822_header->getAddr_s('dnt');
470 /* Pegasus Mail */
471 $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn;
472 /* RFC 2298 */
473 $header[] = 'Disposition-Notification-To: '.$dnt. $rn;
474 }
475 if ($rfc822_header->priority) {
476 switch($rfc822_header->priority)
477 {
478 case 1:
479 $header[] = 'X-Priority: 1 (Highest)'.$rn;
480 $header[] = 'Importance: High'. $rn; break;
481 case 5:
482 $header[] = 'X-Priority: 5 (Lowest)'.$rn;
483 $header[] = 'Importance: Low'. $rn; break;
484 default: break;
485 }
486 }
487 /* Insert headers from the $more_headers array */
488 if(count($rfc822_header->more_headers)) {
489 reset($rfc822_header->more_headers);
490 foreach ($rfc822_header->more_headers as $k => $v) {
491 $header[] = $k.': '.$v .$rn;
492 }
493 }
494 $cnt = count($header);
495 $hdr_s = '';
496
497 for ($i = 0 ; $i < $cnt ; $i++) {
498 $sKey = substr($header[$i],0,strpos($header[$i],':'));
499 switch ($sKey)
500 {
501 case 'Message-ID':
502 case 'In-Reply_To':
503 $hdr_s .= $header[$i];
504 break;
505 case 'References':
506 $sRefs = substr($header[$i],12);
507 $aRefs = explode(' ',$sRefs);
508 $sLine = 'References:';
509 foreach ($aRefs as $sReference) {
510 if (strlen($sLine)+strlen($sReference) >76) {
511 $hdr_s .= $sLine;
512 $sLine = $rn . ' ' . $sReference;
513 } else {
514 $sLine .= ' '. $sReference;
515 }
516 }
517 $hdr_s .= $sLine;
518 break;
519 case 'To':
520 case 'Cc':
521 case 'Bcc':
522 case 'From':
523 $hdr_s .= $header[$i];
524 break;
525 default: $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4)); break;
526 }
527 }
528 $header = $hdr_s;
529 $header .= $rn; /* One blank line to separate header and body */
530 $raw_length += strlen($header);
531 return $header;
532 }
533
534 /**
535 * function foldLine - for cleanly folding of headerlines
536 *
537 * @param string $line
538 * @param integer $length length to fold the line at
539 * @param string $pre prefix the line with...
540 *
541 * @return string $line folded line with trailing CRLF
542 */
543 function foldLine($line, $length, $pre='') {
544 $line = substr($line,0, -2);
545 $length -= 2; /* do not fold between \r and \n */
546 $cnt = strlen($line);
547 if ($cnt > $length) { /* try folding */
548 $fold_string = "\r\n " . $pre;
549 $bFirstFold = false;
550 $aFoldLine = array();
551 while (strlen($line) > $length) {
552 $fold = false;
553 /* handle encoded parts */
554 if (preg_match('/(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(\s+|.*)/Ui',$line,$regs)) {
555 $fold_tmp = $regs[1];
556 if (!trim($regs[5])) {
557 $fold_tmp .= $regs[5];
558 }
559 $iPosEnc = strpos($line,$fold_tmp);
560 $iLengthEnc = strlen($fold_tmp);
561 $iPosEncEnd = $iPosEnc+$iLengthEnc;
562 if ($iPosEnc < $length && (($iPosEncEnd) > $length)) {
563 $fold = true;
564 /* fold just before the start of the encoded string */
565 if ($iPosEnc) {
566 $aFoldLine[] = substr($line,0,$iPosEnc);
567 }
568 $line = substr($line,$iPosEnc);
569 if (!$bFirstFold) {
570 $bFirstFold = true;
571 $length -= strlen($fold_string);
572 }
573 if ($iLengthEnc > $length) { /* place the encoded
574 string on a separate line and do not fold inside it*/
575 /* minimize foldstring */
576 $fold_string = "\r\n ";
577 $aFoldLine[] = substr($line,0,$iLengthEnc);
578 $line = substr($line,$iLengthEnc);
579 }
580 } else if ($iPosEnc < $length) { /* the encoded string fits into the foldlength */
581 /*remainder */
582 $sLineRem = substr($line,$iPosEncEnd,$length - $iPosEncEnd);
583 if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',$sLineRem) || !preg_match('/[=,;\s]/',$sLineRem)) {
584 /*impossible to fold clean in the next part -> fold after the enc string */
585 $aFoldLine[] = substr($line,0,$iPosEncEnd);
586 $line = substr($line,$iPosEncEnd);
587 $fold = true;
588 if (!$bFirstFold) {
589 $bFirstFold = true;
590 $length -= strlen($fold_string);
591 }
592 }
593 }
594 }
595 if (!$fold) {
596 $line_tmp = substr($line,0,$length);
597 $iFoldPos = false;
598 /* try to fold at logical places */
599 switch (true)
600 {
601 case ($iFoldPos = strrpos($line_tmp,',')): break;
602 case ($iFoldPos = strrpos($line_tmp,';')): break;
603 case ($iFoldPos = strrpos($line_tmp,' ')): break;
604 case ($iFoldPos = strrpos($line_tmp,'=')): break;
605 default: break;
606 }
607
608 if (!$iFoldPos) { /* clean folding didn't work */
609 $iFoldPos = $length;
610 }
611 $aFoldLine[] = substr($line,0,$iFoldPos+1);
612 $line = substr($line,$iFoldPos+1);
613 if (!$bFirstFold) {
614 $bFirstFold = true;
615 $length -= strlen($fold_string);
616 }
617 }
618 }
619 /*$reconstruct the line */
620 if ($line) {
621 $aFoldLine[] = $line;
622 }
623 $line = implode($fold_string,$aFoldLine);
624 }
625 return $line."\r\n";
626 }
627
628 /**
629 * function mimeBoundary - calculates the mime boundary to use
630 *
631 * This function will generate a random mime boundary base part
632 * for the message if the boundary has not already been set.
633 *
634 * @return string $mimeBoundaryString random mime boundary string
635 */
636 function mimeBoundary () {
637 static $mimeBoundaryString;
638
639 if ( !isset( $mimeBoundaryString ) ||
640 $mimeBoundaryString == '') {
641 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
642 mt_rand( 10000, 99999 );
643 }
644 return $mimeBoundaryString;
645 }
646
647 /**
648 * function timezone - Time offset for correct timezone
649 *
650 * @return string $result with timezone and offset
651 */
652 function timezone () {
653 global $invert_time;
654
655 $diff_second = date('Z');
656 if ($invert_time) {
657 $diff_second = - $diff_second;
658 }
659 if ($diff_second > 0) {
660 $sign = '+';
661 } else {
662 $sign = '-';
663 }
664 $diff_second = abs($diff_second);
665 $diff_hour = floor ($diff_second / 3600);
666 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
667 $zonename = '('.strftime('%Z').')';
668 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,
669 $zonename);
670 return ($result);
671 }
672
673 /**
674 * function calculate_references - calculate correct Referer string
675 *
676 * @param Rfc822Header $hdr message header to calculate from
677 *
678 * @return string $refer concatenated and trimmed Referer string
679 */
680 function calculate_references($hdr) {
681 $refer = $hdr->references;
682 $message_id = $hdr->message_id;
683 $in_reply_to = $hdr->in_reply_to;
684 if (strlen($refer) > 2) {
685 $refer .= ' ' . $message_id;
686 } else {
687 if ($in_reply_to) {
688 $refer .= $in_reply_to . ' ' . $message_id;
689 } else {
690 $refer .= $message_id;
691 }
692 }
693 trim($refer);
694 return $refer;
695 }
696 }
697 ?>