) var $from_name; // the senders Name ('Testferd Test') var $from_addr; // the sender mailaddr (test@waaf.net) var $cc; // the complete Cc ('CC' ) var $to; // the complete Recipient-Line ('R. E. Cipient' ) var $date; // the time the mail was sent var $subject; // the mails subject var $msgid; // the message-id of the message var $anh; // are there attachements? (0 | 1) function hildegard($headers){ $i = 0; foreach($headers as $ln => $value){ $z1 = substr($value,0,1); if($z1 == "\t"){ $n_headers[$i] .= " ".trim($value); } else { $i = $i + 1; $n_headers[$i] = trim($value); } } $headers = $n_headers; $this->head = join( $headers, "\n" ); $subj=preg_grep ("/^Subject:(.*)$/", $headers); $subj=join($subj, "\n" ); $this->subject = trim(decode_mime_string(quoted_printable_decode(str_replace ("\"", "'", (str_replace ("Subject:", "", $subj)))))); if($this->subject == ""){ $subj=preg_grep ("/^subject:(.*)$/", $headers); $subj=join($subj, "\n" ); $this->subject = trim(decode_mime_string(quoted_printable_decode(str_replace ("\"", "'", (str_replace ("subject:", "", $subj)))))); } if($this->subject == ""){ $subj=preg_grep ("/^SUBJECT:(.*)$/", $headers); $subj=join($subj, "\n" ); $this->subject = trim(decode_mime_string(quoted_printable_decode(str_replace ("\"", "'", (str_replace ("SUBJECT:", "", $subj)))))); } $msgid=preg_grep ("/^Message-ID:(.*)$/", $headers); $msgid=join($msgid, "\n" ); $this->msgid = trim(decode_mime_string(str_replace ("\"", "'", (str_replace ("Message-ID:", "",$msgid))))); if($this->msgid == ""){ $msgid=preg_grep ("/^X-MessageId:(.*)$/", $headers); $msgid=join($msgid, "\n" ); $this->msgid = trim(decode_mime_string(str_replace ("\"", "'", (str_replace ("X-MessageId:", "",$msgid))))); } if($this->msgid == ""){ $msgid=preg_grep ("/^Message-Id:(.*)$/", $headers); $msgid=join($msgid, "\n" ); $this->msgid = trim(decode_mime_string(str_replace ("\"", "'", (str_replace ("Message-Id:", "",$msgid))))); } $to=preg_grep ("/^To:(.*)$/", $headers); $to=join($to, "\n" ); $this->to = trim(decode_mime_string(str_replace ("\"", "'", (str_replace ("To:", "", $to))))); if($this->to == ""){ $to=preg_grep ("/^to:(.*)$/", $headers); $to=join($to, "\n" ); $this->to = trim(decode_mime_string(str_replace ("\"", "'", (str_replace ("to:", "", $to))))); } if($this->to == ""){ $to=preg_grep ("/^TO:(.*)$/", $headers); $to=join($to, "\n" ); $this->to = trim(decode_mime_string(str_replace ("\"", "'", (str_replace ("TO:", "", $to))))); } $cc=preg_grep ("/^Cc:(.*)$/", $headers); $cc=join($cc, "\n" ); $this->cc = trim(decode_mime_string(str_replace ("\"", "'", (str_replace ("Cc: ", "", $cc))))); $from=preg_grep ("/^From:(.*)$/", $headers); $from=join($from, "\n" ); $from_org = str_replace ("From:", "", $from); $this->from = $from_org; if($this->from == ""){ $from=preg_grep ("/^from:(.*)$/", $headers); $from=join($from, "\n" ); $from_org = str_replace ("from:", "", $from); $this->from = $from_org; } if($this->from == ""){ $from=preg_grep ("/^FROM:(.*)$/", $headers); $from=join($from, "\n" ); $from_org = str_replace ("FROM:", "", $from); $this->from = $from_org; } $this->from = str_replace ("\"", "", $this->from); $from23 = str_replace ("<", "", $this->from); $from23 = str_replace (">", "", $from23); $from2 = preg_split ("/[\s,]+/", $from23); for($z=0;$z<=count($from2);$z++){ if(preg_match("/(\S*)@(\S*)/", $from2[$z])){ $this->from_addr = trim(decode_mime_string($from2[$z])); } else { $this->from_name .= trim(decode_mime_string($from2[$z])). " "; } } $this->from_name=trim($this->from_name); $this->from = trim(decode_mime_string($from_org)); $date=preg_grep("/^Date:(.*)$/", $headers); $date=join($date, "\n" ); $this->date = trim(decode_mime_string(str_replace ("Date:", "", $date))); if($this->date == ""){ $date=preg_grep("/^date:(.*)$/", $headers); $date=join($date, "\n" ); $this->date = trim(decode_mime_string(str_replace ("date:", "", $date))); } if($this->date == ""){ $date=preg_grep("/^DATE:(.*)$/", $headers); $date=join($date, "\n" ); $this->date = trim(decode_mime_string(str_replace ("DATE:", "", $date))); } $headers=join($headers, "\n" ); if (eregi("Content-Type: multipart", $headers)) { $this->anh=1; } else { $this->anh=0; } } } /* function to decode a MIME-encoded string, returns the original string, if not MIME Thanks to horde! function taken from: http://ftp.horde.org/pub/imp/tarballs/old/imp-2.0.0.tar.gz */ function decode_mime_string ($string) { if (eregi("=?([A-Z,0-9,-]+)?([A-Z,0-9,-]+)?([A-Z,0-9,-,=,_]+)?=", $string)) { if (ereg("^=?", $string)) $string = ' ' . $string; $coded_strings = explode(' =?', $string); $counter = 1; $string = $coded_strings[0]; /* add non encoded text that is before the encoding */ while ($counter < sizeof($coded_strings)) { $elements = explode('?', $coded_strings[$counter]); /* part 0 = charset */ /* part 1 == encoding */ /* part 2 == encoded part */ /* part 3 == unencoded part beginning with a = */ /* How can we use the charset information? */ if (eregi("Q", $elements[1])) { $elements[2] = str_replace('_', ' ', $elements[2]); $elements[2] = eregi_replace("=([A-F,0-9]{2})", "%\\1", $elements[2]); $string .= urldecode($elements[2]); } else { /* we should check for B the only valid encoding other then Q */ $elements[2] = str_replace('=', '', $elements[2]); if ($elements[2]) { $string .= base64_decode($elements[2]); } } if (isset($elements[3]) && $elements[3] != '') { $elements[3] = ereg_replace("^=", '', $elements[3]); $string .= $elements[3]; } $counter++; } } return $string; } /* ++++++++++++ EXAMPLE ++++++++++++ */ /* read testmail from txtfile on webserver (change as needed!) */ $headers = file ('http://www.os.waaf.net/_hilde_test.mail'); $mail = new hildegard($headers); echo "
Mail is from:         ".htmlentities($mail->from)."
Mail is from name:    ".htmlentities($mail->from_name)."
Mail is from address: ".htmlentities($mail->from_addr)."
Mail is sent cc:      ".htmlentities($mail->cc)."
Mail is sent to:      ".( htmlentities($mail->to) )."
Mail was sent on:     ".htmlentities($mail->date)."
Mail has Message-ID:  ".htmlentities($mail->msgid)."
Mail has Subject:     ".htmlentities($mail->subject)."\n";
if($mail->anh)
  	echo "Mail has Attachment(s)\n";
echo "complete header as one string: \n----------------------\n".htmlentities($mail->head)."\n----------------------";
echo "
"; ?>