diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php index e6e52686df77b946241f2b10ca30d28825b50725..3b862f82924a59bff69c9be4dc37a20567f1d174 100644 --- a/include/class.mailfetch.php +++ b/include/class.mailfetch.php @@ -438,6 +438,46 @@ class MailFetcher { return imap_fetchheader($this->mbox, $mid,FT_PREFETCHTEXT); } + function isBounceNotice($mid) { + if (!($body = $this->getPart($mid, 'message/delivery-status'))) + return false; + + $info = Mail_Parse::splitHeaders($body); + if (!isset($info['Action'])) + return false; + + return strcasecmp($info['Action'], 'failed') === 0; + } + + function getDeliveryStatusMessage($mid) { + if (!($struct = @imap_fetchstructure($this->mbox, $mid))) + return false; + + $ctype = $this->getMimeType($struct); + if (strtolower($ctype) == 'multipart/report') { + foreach ($struct->parameters as $p) { + if (strtolower($p->attribute) == 'report-type' + && $p->value == 'delivery-status') { + return sprintf('<pre>%s</pre>', + Format::htmlchars( + $this->getPart($mid, 'text/plain', $this->charset, $struct, false, 1) + )); + } + } + } + return false; + } + + function getOriginalMessage($mid) { + if (!($body = $this->getPart($mid, 'message/rfc822'))) + return null; + + $msg = new Mail_Parse($body); + if (!$msg->decode()) + return null; + + return $msg->struct; + } function getPriority($mid) { return Mail_Parse::parsePriority($this->getHeader($mid)); @@ -486,9 +526,22 @@ class MailFetcher { } $vars = $mailinfo; + if ($this->isBounceNotice($mid)) { + // Fetch the original References and assign to 'references' + if ($msg = $this->getOriginalMessage($mid)) { + $vars['references'] = $msg->headers['references']; + unset($vars['in-reply-to']); + } + // Fetch deliver status report + $vars['message'] = $this->getDeliveryStatusMessage($mid); + $vars['thread-type'] = 'N'; + } + else { + $vars['message']=Format::stripEmptyLines($this->getBody($mid)); + } + $vars['name']=$this->mime_decode($mailinfo['name']); $vars['subject']=$mailinfo['subject']?$this->mime_decode($mailinfo['subject']):'[No Subject]'; - $vars['message']=Format::stripEmptyLines($this->getBody($mid)); $vars['emailId']=$mailinfo['emailId']?$mailinfo['emailId']:$this->getEmailId(); //Missing FROM name - use email address.