Skip to content
Snippets Groups Projects
Commit c89195f7 authored by Jared Hancock's avatar Jared Hancock
Browse files

emails: Add bounce detection for mail fetching

parent 385be007
No related branches found
No related tags found
No related merge requests found
......@@ -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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment