/** * Processes incoming emails so that issues can be created/updated * * @param unknown_type $emails */ public function processIncomingEmails($emails) { foreach ($emails as $mail) { // First we need to find which customer the email belongs to $from = ifset($mail->headers, 'from', false); if (!$from) { za()->log("Failed finding from email header in " . print_r($mail, true)); continue; } // clean it up a bit if (!preg_match("/[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}/i", $from, $matches)) { za()->log("Error finding valid email address", Zend_Log::ERR); continue; } $email = $matches[0]; // Get the contact now. If it doesn't exist that's okay, it // might be a system user instead. $contact = $this->clientService->getContactByField('email', $email); // If not found by primary, try secondary if (!$contact) { $contact = $this->clientService->getContactByField('altemail', $email); } // We'll also see whether this issue was sent in by a user // of the system. $user = $this->userService->getUserByField('email', $email); if (!$contact && !$user) { za()->log("No valid user found with 'from' address {$email}", Zend_Log::WARN); $this->trackerService->track('invalid-issue-email', $email, null, null, serialize($mail)); continue; } // Next up, see if the contact has an associated user, because // we'll add them in as the 'creator' of the issue if ($contact != null) { $contactUser = $this->userService->getUserbyField('contactid', $contact->id); if ($contactUser != null) { $user = $contactUser; } } if ($user != null) { za()->setUser($user); } $params = array(); // Saving a new issue uses the title of the email $subject = ifset($mail->headers, 'subject', false); if (!$subject) { // we'll accept an empty subject, just create a default title $subject = "Support request from {$from}"; } $textBody = $this->emailService->getTextBody($mail); $issue = null; // Try and get an existing ticket za()->log("Checking email subject {$subject}"); if (preg_match("/#(\\d+)/", $subject, $matches)) { // existing $id = $matches[1]; $issue = $this->getIssue($id); if ($issue) { za()->log("Adding note to request {$id}"); // Make sure the issue found currently belongs to the contact // client! // if there's no contact, make sure there's a user instead if ($contact) { if ($issue->clientid != $contact->clientid) { $issue = null; } } else { if (!$user) { $issue = null; } } } else { $this->log->warn("Request not found for id {$id}"); } } $infoText = ""; if ($user) { $infoText = "Email coming from user #" . $user->id . " - " . $user->username . " "; } if ($contact) { $infoText = "Email coming from contact #" . $contact->id . " - " . $contact->firstname . " "; } $this->trackerService->track('incoming-issue-email', $email, null, null, "Processing email from " . $email . ": " . $infoText); $notifyOfId = false; // If we've already got an issue, it means we're wanting to // just update its comments if ($issue) { // Just add an additional comment to the // current issue. $poster = $contact ? $contact->firstname : $user->getUsername(); $note = $this->notificationService->addNoteTo($issue, $textBody, $subject, $poster); $this->notificationService->sendWatchNotifications($note, array('controller' => 'issue', 'action' => 'edit', 'params' => array('id' => $issue->id))); za()->log("Note added to request {$issue->id}", Zend_Log::INFO); } else { // new $issue = new Issue(); $issue->title = $subject; $issue->description = $textBody; $issue->issuetype = 'Support'; // Send a notification to the user that the report was received, // with the bug ID in it so the user knows what to respond to $notifyOfId = !is_null($contact); } if ($contact) { $issue->clientid = $contact->clientid; } $this->saveIssue($issue); // Go through the attachments for the email, if any $attachments = $this->emailService->getEmailAttachments($mail); $i = 1; foreach ($attachments as $emailAttachment) { $this->addAttachmentToIssue($issue, $emailAttachment, $i); $i++; } if ($notifyOfId) { $model = array('issue' => $issue, 'contact' => $contact); $receipient = new User(); $receipient->username = $contact->firstname; $receipient->email = $contact->email; $subject = "New request created: #{$issue->id}"; $msg = $this->notificationService->generateEmail('new-issue-contact.php', $model); $this->trackerService->track('notify-request-sender', $receipient->username, null, null, $subject); $this->notificationService->notifyUser($subject, array($receipient), $msg, ifset($mail->headers, 'to', null), 'Support'); } } }