Exemple #1
0
    $extid = trim(preg_replace("/[^0-9]/", "", $regs[0]));
    $ticket = new Ticket(Ticket::getIdByExtId($extid));
    //Allow mismatched emails?? For now hell NO.
    if (!is_object($ticket) || strcasecmp($ticket->getEmail(), $var['email'])) {
        $ticket = null;
    }
}
$errors = array();
$msgid = 0;
if (!$ticket) {
    //New tickets...
    $ticket = Ticket::create($var, $errors, 'email');
    if (!is_object($ticket) || $errors) {
        api_exit(EX_DATAERR, 'Ticket create Failed ' . implode("\n", $errors) . "\n\n");
    }
    $msgid = $ticket->getLastMsgId();
} else {
    //post message....postMessage does the cleanup.
    if (!($msgid = $ticket->postMessage($var['message'], 'Email', $var['mid'], $var['header']))) {
        api_exit(EX_DATAERR, 'Unable to post message');
    }
}
//Ticket created...save attachments if enabled.
if ($cfg->allowEmailAttachments() && ($attachments = $parser->getAttachments())) {
    foreach ($attachments as $attachment) {
        if ($attachment['filename'] && $ost->isFileTypeAllowed($attachment['filename'])) {
            $ticket->saveAttachment(array('name' => $attachment['filename'], 'data' => $attachment['body']), $msgid, 'M');
        }
    }
}
api_exit(EX_SUCCESS);
$msgid = 0;
if (!$ticket) {
    //New tickets...
    $ticket = Ticket::create($var, $errors, 'email');
    if (!is_object($ticket) || $errors) {
        api_exit(EX_DATAERR, $trl->translate('TEXT_CREATE_FAILED') . ' ' . implode("\n", $errors) . "\n\n");
    }
    $msgid = $ticket->getLastMsgId();
} else {
    $message = $var['message'];
    //Strip quoted reply...TODO: figure out how mail clients do it without special tag..
    if ($cfg->stripQuotedReply() && ($tag = $cfg->getReplySeparator()) && strpos($var['message'], $tag)) {
        list($message) = split($tag, $var['message']);
    }
    //post message....postMessage does the cleanup.
    if (!($msgid = $ticket->postMessage($message, 'Email', $var['mid'], $var['header']))) {
        api_exit(EX_DATAERR, "Unable to post message \n\n {$message}\n");
    }
}
//Ticket created...save attachments if enabled.
if ($cfg->allowEmailAttachments()) {
    if ($attachments = $parser->getAttachments()) {
        //print_r($attachments);
        foreach ($attachments as $k => $attachment) {
            if ($attachment['filename'] && $cfg->canUploadFileType($attachment['filename'])) {
                $ticket->saveAttachment($attachment['filename'], $attachment['body'], $msgid, 'M');
            }
        }
    }
}
api_exit(EX_SUCCESS);
Exemple #3
0
 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 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();
     //FROM address: who sent the email.
     if (($fromlist = $parser->getFromAddressList()) && !PEAR::isError($fromlist)) {
         $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'];
         }
     }
     //TO Address:Try to figure out the email address... associated with the incoming email.
     $emailId = 0;
     if ($tolist = $parser->getToAddressList()) {
         foreach ($tolist as $toaddr) {
             if ($emailId = Email::getIdByEmail($toaddr->mailbox . '@' . $toaddr->host)) {
                 break;
             }
         }
     }
     //maybe we got CC'ed??
     if (!$emailId && ($cclist = $parser->getCcAddressList())) {
         foreach ($cclist as $ccaddr) {
             if ($emailId = Email::getIdByEmail($ccaddr->mailbox . '@' . $ccaddr->host)) {
                 break;
             }
         }
     }
     $data['subject'] = $parser->getSubject();
     $data['message'] = Format::stripEmptyLines($parser->getBody());
     $data['header'] = $parser->getHeader();
     $data['mid'] = $parser->getMessageId();
     $data['priorityId'] = $parser->getPriority();
     $data['emailId'] = $emailId;
     $data['in-reply-to'] = $parser->struct->headers['in-reply-to'];
     $data['references'] = $parser->struct->headers['references'];
     if (($replyto = $parser->getReplyTo()) && !PEAR::isError($replyto)) {
         $replyto = $replyto[0];
         $data['reply-to'] = $replyto->mailbox . '@' . $replyto->host;
         if ($replyto->personal) {
             $data['reply-to-name'] = trim($replyto->personal, " \t\n\r\v\"");
         }
     }
     if ($cfg && $cfg->allowEmailAttachments()) {
         $data['attachments'] = $parser->getAttachments();
     }
     return $data;
 }