function testBounceHeaders() { static $headers = array('Return-Path: <>', 'From: Mail Delivery Subsystem <*****@*****.**>', 'X-Failed-Recipients: xxxxrack@xbsoxxxxxx.com', 'Subject: Delivery Status Notification (Failure)', 'From: MAILER-DAEMON@mail1.dl.supportsystem.com (Mail Delivery System)', 'Subject: Undelivered Mail Returned to Sender', 'Content-Type: multipart/report; report-type=delivery-status; boundary="6053C732A.7354145592/mail1.dl.supportsystem.com"'); foreach ($headers as $h) { $this->assert(TicketFilter::isBounce($h), $h . ": Unidentified bouce"); } }
function createTicket($mid) { global $ost; if (!($mailinfo = $this->getHeaderInfo($mid))) { return false; } // TODO: If the content-type of the message is 'message/rfc822', // then this is a message with the forwarded message as the // attachment. Download the body and pass it along to the mail // parsing engine. $info = Mail_Parse::splitHeaders($mailinfo['header']); if (strtolower($info['Content-Type']) == 'message/rfc822') { if ($wrapped = $this->getPart($mid, 'message/rfc822')) { require_once INCLUDE_DIR . 'api.tickets.php'; // Simulate piping the contents into the system $api = new TicketApiController(); $parser = new EmailDataParser(); if ($data = $parser->parse($wrapped)) { return $api->processEmail($data); } } // If any of this fails, create the ticket as usual } //Is the email address banned? if ($mailinfo['email'] && TicketFilter::isBanned($mailinfo['email'])) { //We need to let admin know... $ost->logWarning(_S('Ticket denied'), sprintf(_S('Banned email — %s'), $mailinfo['email']), false); return true; //Report success (moved or delete) } // Parse MS TNEF emails if (($struct = imap_fetchstructure($this->mbox, $mid)) && ($attachments = $this->getAttachments($struct))) { foreach ($attachments as $i => $info) { if (0 === strcasecmp('application/ms-tnef', $info['type'])) { try { $data = $this->decode(imap_fetchbody($this->mbox, $mid, $info['index']), $info['encoding']); $tnef = new TnefStreamParser($data); $this->tnef = $tnef->getMessage(); // No longer considered an attachment unset($attachments[$i]); // There should only be one of these break; } catch (TnefException $ex) { // Noop -- winmail.dat remains an attachment } } } } $vars = $mailinfo; $vars['name'] = $mailinfo['name']; $vars['subject'] = $mailinfo['subject'] ?: '[No Subject]'; $vars['emailId'] = $mailinfo['emailId'] ?: $this->getEmailId(); $vars['to-email-id'] = $mailinfo['emailId'] ?: 0; $vars['flags'] = new ArrayObject(); if ($this->isBounceNotice($mid)) { // Fetch the original References and assign to 'references' if ($headers = $this->getOriginalMessageHeaders($mid)) { $vars['references'] = $headers['references']; $vars['in-reply-to'] = @$headers['in-reply-to'] ?: null; } // Fetch deliver status report $vars['message'] = $this->getDeliveryStatusMessage($mid) ?: $this->getBody($mid); $vars['thread-type'] = 'N'; $vars['flags']['bounce'] = true; } else { $vars['message'] = $this->getBody($mid); $vars['flags']['bounce'] = TicketFilter::isBounce($info); } //Missing FROM name - use email address. if (!$vars['name']) { list($vars['name']) = explode('@', $vars['email']); } if ($ost->getConfig()->useEmailPriority()) { $vars['priorityId'] = $this->getPriority($mid); } $ticket = null; $newticket = true; $errors = array(); $seen = false; // Use the settings on the thread entry on the ticket details // form to validate the attachments in the email $tform = TicketForm::objects()->one()->getForm(); $messageField = $tform->getField('message'); $fileField = $messageField->getWidget()->getAttachments(); // Fetch attachments if any. if ($messageField->isAttachmentsEnabled()) { // Include TNEF attachments in the attachments list if ($this->tnef) { foreach ($this->tnef->attachments as $at) { $attachments[] = array('cid' => @$at->AttachContentId ?: false, 'data' => $at, 'size' => @$at->DataSize ?: null, 'type' => @$at->AttachMimeTag ?: false, 'name' => $at->getName()); } } $vars['attachments'] = array(); foreach ($attachments as $a) { $file = array('name' => $a['name'], 'type' => $a['type']); if (@$a['data'] instanceof TnefAttachment) { $file['data'] = $a['data']->getData(); } else { // only fetch the body if necessary $self = $this; $file['data'] = function () use($self, $mid, $a) { return $self->decode(imap_fetchbody($self->mbox, $mid, $a['index']), $a['encoding']); }; } // Include the Content-Id if specified (for inline images) $file['cid'] = isset($a['cid']) ? $a['cid'] : false; // Validate and save immediately try { $file['id'] = $fileField->uploadAttachment($file); } catch (FileUploadError $ex) { $file['error'] = $file['name'] . ': ' . $ex->getMessage(); } $vars['attachments'][] = $file; } } // Allow signal handlers to interact with the message decoding Signal::send('mail.processed', $this, $vars); $seen = false; if (($thread = ThreadEntry::lookupByEmailHeaders($vars, $seen)) && ($t = $thread->getTicket()) && ($vars['staffId'] || !$t->isClosed() || $t->isReopenable()) && ($message = $thread->postEmail($vars))) { if (!$message instanceof ThreadEntry) { // Email has been processed previously return $message; } $ticket = $message->getTicket(); } elseif ($seen) { // Already processed, but for some reason (like rejection), no // thread item was created. Ignore the email return true; } elseif ($ticket = Ticket::create($vars, $errors, 'Email')) { $message = $ticket->getLastMessage(); } else { //Report success if the email was absolutely rejected. if (isset($errors['errno']) && $errors['errno'] == 403) { // Never process this email again! ThreadEntry::logEmailHeaders(0, $vars['mid']); return true; } // Log an error to the system logs $mailbox = Email::lookup($vars['emailId']); $ost->logError(_S('Mail Processing Exception'), sprintf(_S("Mailbox: %s | Error(s): %s"), $mailbox->getEmail(), print_r($errors, true)), false); // Indicate failure of mail processing return null; } return $ticket; }
function parse($stream) { global $cfg; $contents = ''; if (is_resource($stream)) { while (!feof($stream)) { $contents .= fread($stream, 8192); } } else { $contents = $stream; } $parser = new Mail_Parse($contents); if (!$parser->decode()) { //Decode...returns false on decoding errors return $this->err('Email parse failed [' . $parser->getError() . ']'); } $data = array(); $data['emailId'] = 0; $data['recipients'] = array(); $data['subject'] = $parser->getSubject(); $data['header'] = $parser->getHeader(); $data['mid'] = $parser->getMessageId(); $data['priorityId'] = $parser->getPriority(); $data['flags'] = new ArrayObject(); //FROM address: who sent the email. if ($fromlist = $parser->getFromAddressList()) { $from = $fromlist[0]; //Default. foreach ($fromlist as $fromobj) { if (!Validator::is_email($fromobj->mailbox . '@' . $fromobj->host)) { continue; } $from = $fromobj; break; } $data['email'] = $from->mailbox . '@' . $from->host; $data['name'] = trim($from->personal, '"'); if ($from->comment && $from->comment[0]) { $data['name'] .= ' (' . $from->comment[0] . ')'; } //Use email address as name when FROM address doesn't have a name. if (!$data['name'] && $data['email']) { $data['name'] = $data['email']; } } /* Scan through the list of addressees (via To, Cc, and Delivered-To headers), and identify * how the mail arrived at the system. One of the mails should be in the system email list. * The recipient list (without the Delivered-To addressees) will be made available to the * ticket filtering system. However, addresses in the Delivered-To header should never be * considered for the collaborator list. */ $tolist = array(); if ($to = $parser->getToAddressList()) { $tolist['to'] = $to; } if ($cc = $parser->getCcAddressList()) { $tolist['cc'] = $cc; } if ($dt = $parser->getDeliveredToAddressList()) { $tolist['delivered-to'] = $dt; } foreach ($tolist as $source => $list) { foreach ($list as $addr) { if (!($emailId = Email::getIdByEmail(strtolower($addr->mailbox) . '@' . $addr->host))) { //Skip virtual Delivered-To addresses if ($source == 'delivered-to') { continue; } $data['recipients'][] = array('source' => sprintf(_S("Email (%s)"), $source), 'name' => trim(@$addr->personal, '"'), 'email' => strtolower($addr->mailbox) . '@' . $addr->host); } elseif (!$data['emailId']) { $data['emailId'] = $emailId; } } } /* * In the event that the mail was delivered to the system although none of the system * mail addresses are in the addressee lists, be careful not to include the addressee * in the collaborator list. Therefore, the delivered-to addressees should be flagged so they * are not added to the collaborator list in the ticket creation process. */ if ($tolist['delivered-to']) { foreach ($tolist['delivered-to'] as $addr) { foreach ($data['recipients'] as $i => $r) { if (strcasecmp($r['email'], $addr->mailbox . '@' . $addr->host) === 0) { $data['recipients'][$i]['source'] = 'delivered-to'; } } } } //maybe we got BCC'ed?? if (!$data['emailId']) { $emailId = 0; if ($bcc = $parser->getBccAddressList()) { foreach ($bcc as $addr) { if ($emailId = Email::getIdByEmail($addr->mailbox . '@' . $addr->host)) { break; } } } $data['emailId'] = $emailId; } if ($parser->isBounceNotice()) { // Fetch the original References and assign to 'references' if ($headers = $parser->getOriginalMessageHeaders()) { $data['references'] = $headers['references']; $data['in-reply-to'] = @$headers['in-reply-to'] ?: null; } // Fetch deliver status report $data['message'] = $parser->getDeliveryStatusMessage() ?: $parser->getBody(); $data['thread-type'] = 'N'; $data['flags']['bounce'] = true; } else { // Typical email $data['message'] = $parser->getBody(); $data['in-reply-to'] = @$parser->struct->headers['in-reply-to']; $data['references'] = @$parser->struct->headers['references']; $data['flags']['bounce'] = TicketFilter::isBounce($data['header']); } $data['to-email-id'] = $data['emailId']; if ($replyto = $parser->getReplyTo()) { $replyto = $replyto[0]; $data['reply-to'] = $replyto->mailbox . '@' . $replyto->host; if ($replyto->personal) { $data['reply-to-name'] = trim($replyto->personal, " \t\n\r\v\""); } } $data['attachments'] = $parser->getAttachments(); return $data; }
function isBounce() { if (!isset($this->is_bounce)) { $this->is_bounce = $this->getEmailHeaderArray() ? TicketFilter::isBounce($this->getEmailHeaderArray()) : false; } return $this->is_bounce; }