예제 #1
0
 /**
  * Routes an email to the correct issue.
  *
  * @param   string $full_message The full email message, including headers
  * @return  mixed   true or array(ERROR_CODE, ERROR_STRING) in case of failure
  */
 public static function route_emails($full_message)
 {
     // need some validation here
     if (empty($full_message)) {
         return array(self::EX_NOINPUT, ev_gettext('Error: The email message was empty') . ".\n");
     }
     // save the full message for logging purposes
     Support::saveRoutedEmail($full_message);
     // check if the email routing interface is even supposed to be enabled
     $setup = Setup::get();
     if ($setup['email_routing']['status'] != 'enabled') {
         return array(self::EX_CONFIG, ev_gettext('Error: The email routing interface is disabled.') . "\n");
     }
     if (empty($setup['email_routing']['address_prefix'])) {
         return array(self::EX_CONFIG, ev_gettext('Error: Please configure the email address prefix.') . "\n");
     }
     if (empty($setup['email_routing']['address_host'])) {
         return array(self::EX_CONFIG, ev_gettext('Error: Please configure the email address domain.') . "\n");
     }
     // associate routed emails to the internal system account
     $sys_account = User::getNameEmail(APP_SYSTEM_USER_ID);
     if (empty($sys_account['usr_email'])) {
         return array(self::EX_CONFIG, ev_gettext('Error: The associated user for the email routing interface needs to be set.') . "\n");
     }
     unset($sys_account);
     // join the Content-Type line (for easier parsing?)
     if (preg_match('/^boundary=/m', $full_message)) {
         $pattern = "#(Content-Type: multipart/.+); ?\r?\n(boundary=.*)\$#im";
         $replacement = '$1; $2';
         $full_message = preg_replace($pattern, $replacement, $full_message);
     }
     // remove the reply-to: header
     if (preg_match('/^reply-to:.*/im', $full_message)) {
         $full_message = preg_replace("/^(reply-to:).*\n/im", '', $full_message, 1);
     }
     AuthCookie::setAuthCookie(APP_SYSTEM_USER_ID);
     $structure = Mime_Helper::decode($full_message, true, true);
     // find which issue ID this email refers to
     if (isset($structure->headers['to'])) {
         $issue_id = self::getMatchingIssueIDs($structure->headers['to'], 'email');
     }
     // we need to try the Cc header as well
     if (empty($issue_id) and isset($structure->headers['cc'])) {
         $issue_id = self::getMatchingIssueIDs($structure->headers['cc'], 'email');
     }
     if (empty($issue_id)) {
         return array(self::EX_DATAERR, ev_gettext('Error: The routed email had no associated Eventum issue ID or had an invalid recipient address.') . "\n");
     }
     $issue_prj_id = Issue::getProjectID($issue_id);
     if (empty($issue_prj_id)) {
         return array(self::EX_DATAERR, ev_gettext('Error: The routed email had no associated Eventum issue ID or had an invalid recipient address.') . "\n");
     }
     $email_account_id = Email_Account::getEmailAccount($issue_prj_id);
     if (empty($email_account_id)) {
         return array(self::EX_CONFIG, ev_gettext('Error: Please provide the email account ID.') . "\n");
     }
     $body = $structure->body;
     // hack for clients that set more then one from header
     if (is_array($structure->headers['from'])) {
         $structure->headers['from'] = $structure->headers['from'][0];
     }
     // associate the email to the issue
     $parts = array();
     Mime_Helper::parse_output($structure, $parts);
     // get the sender's email address
     $sender_email = strtolower(Mail_Helper::getEmailAddress($structure->headers['from']));
     // strip out the warning message sent to staff users
     if ($setup['email_routing']['status'] == 'enabled' && $setup['email_routing']['warning']['status'] == 'enabled') {
         $full_message = Mail_Helper::stripWarningMessage($full_message);
         $body = Mail_Helper::stripWarningMessage($body);
     }
     $prj_id = Issue::getProjectID($issue_id);
     AuthCookie::setAuthCookie(APP_SYSTEM_USER_ID);
     AuthCookie::setProjectCookie($prj_id);
     if (Mime_Helper::hasAttachments($structure)) {
         $has_attachments = 1;
     } else {
         $has_attachments = 0;
     }
     // remove certain CC addresses
     if (!empty($structure->headers['cc']) && $setup['smtp']['save_outgoing_email'] == 'yes') {
         $ccs = explode(',', @$structure->headers['cc']);
         foreach ($ccs as $i => $address) {
             if (Mail_Helper::getEmailAddress($address) == $setup['smtp']['save_address']) {
                 unset($ccs[$i]);
             }
         }
         $structure->headers['cc'] = implode(', ', $ccs);
     }
     // Remove excess Re's
     $structure->headers['subject'] = Mail_Helper::removeExcessRe(@$structure->headers['subject'], true);
     $t = array('issue_id' => $issue_id, 'ema_id' => $email_account_id, 'message_id' => @$structure->headers['message-id'], 'date' => Date_Helper::getCurrentDateGMT(), 'from' => @$structure->headers['from'], 'to' => @$structure->headers['to'], 'cc' => @$structure->headers['cc'], 'subject' => @$structure->headers['subject'], 'body' => @$body, 'full_email' => @$full_message, 'has_attachment' => $has_attachments, 'headers' => @$structure->headers);
     // automatically associate this incoming email with a customer
     if (CRM::hasCustomerIntegration($prj_id)) {
         $crm = CRM::getInstance($prj_id);
         if (!empty($structure->headers['from'])) {
             try {
                 $contact = $crm->getContactByEmail($sender_email);
                 $issue_contract = $crm->getContract(Issue::getContractID($issue_id));
                 if ($contact->canAccessContract($issue_contract)) {
                     $t['customer_id'] = $issue_contract->getCustomerID();
                 }
             } catch (CRMException $e) {
             }
         }
     }
     if (empty($t['customer_id'])) {
         $t['customer_id'] = null;
     }
     if (Support::blockEmailIfNeeded($t)) {
         return true;
     }
     // re-write Threading headers if needed
     list($t['full_email'], $t['headers']) = Mail_Helper::rewriteThreadingHeaders($t['issue_id'], $t['full_email'], $t['headers'], 'email');
     $res = Support::insertEmail($t, $structure, $sup_id);
     if ($res != -1) {
         Support::extractAttachments($issue_id, $structure);
         // notifications about new emails are always external
         $internal_only = false;
         $assignee_only = false;
         // special case when emails are bounced back, so we don't want a notification to customers about those
         if (Notification::isBounceMessage($sender_email)) {
             // broadcast this email only to the assignees for this issue
             $internal_only = true;
             $assignee_only = true;
         }
         Notification::notifyNewEmail(Auth::getUserID(), $issue_id, $t, $internal_only, $assignee_only, '', $sup_id);
         // try to get usr_id of sender, if not, use system account
         $usr_id = User::getUserIDByEmail(Mail_Helper::getEmailAddress($structure->headers['from']));
         if (!$usr_id) {
             $usr_id = APP_SYSTEM_USER_ID;
         }
         // mark this issue as updated
         if (!empty($t['customer_id']) && $t['customer_id'] != null) {
             Issue::markAsUpdated($issue_id, 'customer action');
         } else {
             if (!empty($usr_id) && $usr_id != APP_SYSTEM_USER_ID && User::getRoleByUser($usr_id, $prj_id) > User::ROLE_CUSTOMER) {
                 Issue::markAsUpdated($issue_id, 'staff response');
             } else {
                 Issue::markAsUpdated($issue_id, 'user response');
             }
         }
         // log routed email
         History::add($issue_id, $usr_id, 'email_routed', 'Email routed from {from}', array('from' => $structure->headers['from']));
     }
     return true;
 }
예제 #2
0
 /**
  * TODO: merge use of $options and $email arrays to just $email
  *
  * @param int $issue_id
  * @param string $type type of email
  * @param string $from
  * @param string $to
  * @param string $cc
  * @param string $subject
  * @param string $body
  * @param array $options optional parameters
  * - (int) parent_sup_id
  * - (array) iaf_ids attachment file ids
  * - (bool) add_unknown
  * - (int) ema_id
  * @return int 1 if it worked, -1 otherwise
  */
 public static function sendEmail($issue_id, $type, $from, $to, $cc, $subject, $body, $options = array())
 {
     $parent_sup_id = $options['parent_sup_id'];
     $iaf_ids = $options['iaf_ids'];
     $add_unknown = $options['add_unknown'];
     $ema_id = $options['ema_id'];
     $current_usr_id = Auth::getUserID();
     $prj_id = Issue::getProjectID($issue_id);
     // if we are replying to an existing email, set the In-Reply-To: header accordingly
     $in_reply_to = $parent_sup_id ? self::getMessageIDByID($parent_sup_id) : false;
     // get ID of whoever is sending this.
     $sender_usr_id = User::getUserIDByEmail(Mail_Helper::getEmailAddress($from)) ?: false;
     // remove extra 'Re: ' from subject
     $subject = Mail_Helper::removeExcessRe($subject, true);
     $internal_only = false;
     $message_id = Mail_Helper::generateMessageID();
     // process any files being uploaded
     // from ajax upload, attachment file ids
     if ($iaf_ids) {
         // FIXME: is it correct to use sender from post data?
         $attach_usr_id = $sender_usr_id ?: $current_usr_id;
         Attachment::attachFiles($issue_id, $attach_usr_id, $iaf_ids, false, 'Attachment originated from outgoing email');
     }
     // hack needed to get the full headers of this web-based email
     $full_email = self::buildFullHeaders($issue_id, $message_id, $from, $to, $cc, $subject, $body, $in_reply_to, $iaf_ids);
     // email blocking should only be done if this is an email about an associated issue
     if ($issue_id) {
         $user_info = User::getNameEmail($current_usr_id);
         // check whether the current user is allowed to send this email to customers or not
         if (!self::isAllowedToEmail($issue_id, $user_info['usr_email'])) {
             // add the message body as a note
             $note = Mail_Helper::getCannedBlockedMsgExplanation() . $body;
             $note_options = array('full_message' => $full_email, 'is_blocked' => true);
             Note::insertNote($current_usr_id, $issue_id, $subject, $note, $note_options);
             $email_details = array('from' => $from, 'to' => $to, 'cc' => $cc, 'subject' => $subject, 'body' => &$body, 'message' => &$body, 'title' => $subject);
             Workflow::handleBlockedEmail($prj_id, $issue_id, $email_details, 'web');
             return 1;
         }
     }
     // only send a direct email if the user doesn't want to add the Cc'ed people to the notification list
     if (($add_unknown || Workflow::shouldAutoAddToNotificationList($prj_id)) && $issue_id) {
         // add the recipients to the notification list of the associated issue
         $recipients = array($to);
         $recipients = array_merge($recipients, self::getRecipientsCC($cc));
         foreach ($recipients as $address) {
             if ($address && !Notification::isIssueRoutingSender($issue_id, $address)) {
                 $actions = Notification::getDefaultActions($issue_id, $address, 'add_unknown_user');
                 Notification::subscribeEmail($current_usr_id, $issue_id, Mail_Helper::getEmailAddress($address), $actions);
             }
         }
     } else {
         // Usually when sending out emails associated to an issue, we would
         // simply insert the email in the table and call the Notification::notifyNewEmail() method,
         // but on this case we need to actually send the email to the recipients that are not
         // already in the notification list for the associated issue, if any.
         // In the case of replying to an email that is not yet associated with an issue, then
         // we are always directly sending the email, without using any notification list
         // functionality.
         if ($issue_id) {
             // send direct emails only to the unknown addresses, and leave the rest to be
             // catched by the notification list
             $from = Notification::getFixedFromHeader($issue_id, $from, 'issue');
             // build the list of unknown recipients
             if ($to) {
                 $recipients = array($to);
                 $recipients = array_merge($recipients, self::getRecipientsCC($cc));
             } else {
                 $recipients = self::getRecipientsCC($cc);
             }
             $unknowns = array();
             foreach ($recipients as $address) {
                 if (!Notification::isSubscribedToEmails($issue_id, $address)) {
                     $unknowns[] = $address;
                 }
             }
             if ($unknowns) {
                 $to2 = array_shift($unknowns);
                 $cc2 = implode('; ', $unknowns);
                 // send direct emails
                 self::sendDirectEmail($issue_id, $from, $to2, $cc2, $subject, $body, $_FILES['attachment'], $message_id, $sender_usr_id);
             }
         } else {
             // send direct emails to all recipients, since we don't have an associated issue
             $project_info = Project::getOutgoingSenderAddress(Auth::getCurrentProject());
             // use the project-related outgoing email address, if there is one
             if (!empty($project_info['email'])) {
                 $from = Mail_Helper::getFormattedName(User::getFullName($current_usr_id), $project_info['email']);
             } else {
                 // otherwise, use the real email address for the current user
                 $from = User::getFromHeader($current_usr_id);
             }
             // send direct emails
             self::sendDirectEmail($issue_id, $from, $to, $cc, $subject, $body, $_FILES['attachment'], $message_id);
         }
     }
     $email = array('customer_id' => 'NULL', 'issue_id' => $issue_id, 'ema_id' => $ema_id, 'message_id' => $message_id, 'date' => Date_Helper::getCurrentDateGMT(), 'from' => $from, 'to' => $to, 'cc' => $cc, 'subject' => $subject, 'body' => $body, 'full_email' => $full_email);
     // associate this new email with a customer, if appropriate
     if (Auth::getCurrentRole() == User::getRoleID('Customer')) {
         if ($issue_id) {
             $crm = CRM::getInstance($prj_id);
             try {
                 $contact = $crm->getContact(User::getCustomerContactID($current_usr_id));
                 $issue_contract = $crm->getContract(Issue::getContractID($issue_id));
                 if ($contact->canAccessContract($issue_contract)) {
                     $email['customer_id'] = $issue_contract->getCustomerID();
                 }
             } catch (CRMException $e) {
             }
         } else {
             $customer_id = User::getCustomerID($current_usr_id);
             if ($customer_id && $customer_id != -1) {
                 $email['customer_id'] = $customer_id;
             }
         }
     }
     $email['has_attachment'] = $iaf_ids ? 1 : 0;
     $structure = Mime_Helper::decode($full_email, true, false);
     $email['headers'] = $structure->headers;
     self::insertEmail($email, $structure, $sup_id);
     if ($issue_id) {
         // need to send a notification
         Notification::notifyNewEmail($current_usr_id, $issue_id, $email, $internal_only, false, $type, $sup_id);
         // mark this issue as updated
         $has_customer = $email['customer_id'] && $email['customer_id'] != 'NULL';
         if ($has_customer && (!$current_usr_id || User::getRoleByUser($current_usr_id, $prj_id) == User::getRoleID('Customer'))) {
             Issue::markAsUpdated($issue_id, 'customer action');
         } else {
             if ($sender_usr_id && User::getRoleByUser($sender_usr_id, $prj_id) > User::getRoleID('Customer')) {
                 Issue::markAsUpdated($issue_id, 'staff response');
             } else {
                 Issue::markAsUpdated($issue_id, 'user response');
             }
         }
         History::add($issue_id, $current_usr_id, 'email_sent', 'Outgoing email sent by {user}', array('user' => User::getFullName($current_usr_id)));
     }
     return 1;
 }
예제 #3
0
        Misc::setMessage(ev_gettext('Thank you, the internal note was posted successfully.'), Misc::MSG_INFO);
    }
    $tpl->assign('post_result', $res);
    // enter the time tracking entry about this phone support entry
    if (!empty($_POST['time_spent'])) {
        if (isset($_POST['time_summary']) && !empty($_POST['time_summary'])) {
            $summary = (string) $_POST['time_summary'];
        } else {
            $summary = 'Time entry inserted when sending an internal note.';
        }
        $date = (array) $_POST['date'];
        $ttc_id = (int) $_POST['time_category'];
        $time_spent = (int) $_POST['time_spent'];
        Time_Tracking::addTimeEntry($issue_id, $ttc_id, $time_spent, $date, $summary);
    }
    Auth::redirect("post_note.php?cat=post_result&issue_id={$issue_id}&post_result={$res}");
} elseif ($cat == 'reply') {
    if (!empty($_GET['id'])) {
        $note = Note::getDetails($_GET['id']);
        $header = Misc::formatReplyPreamble($note['timestamp'], $note['not_from']);
        $note['not_body'] = $header . Misc::formatReply($note['not_note']);
        $tpl->assign(array('note' => $note, 'parent_note_id' => $_GET['id']));
        $reply_subject = Mail_Helper::removeExcessRe($note['not_title']);
    }
}
if (empty($reply_subject)) {
    // TRANSLATORS: %1 = issue summary
    $reply_subject = ev_gettext('Re: %1$s', $details['iss_summary']);
}
$tpl->assign(array('from' => User::getFromHeader($usr_id), 'users' => Project::getUserAssocList($prj_id, 'active', User::getRoleID('Customer')), 'current_user_prefs' => Prefs::get($usr_id), 'subscribers' => Notification::getSubscribers($issue_id, false, User::getRoleID('Standard User')), 'statuses' => Status::getAssocStatusList($prj_id, false), 'current_issue_status' => Issue::getStatusID($issue_id), 'time_categories' => Time_Tracking::getAssocCategories($prj_id), 'note_category_id' => Time_Tracking::getCategoryId($prj_id, 'Note Discussion'), 'reply_subject' => $reply_subject, 'issue_fields' => Issue_Field::getDisplayData($issue_id, 'post_note')));
$tpl->displayTemplate();