From ac86c3100a1030026024c33c1cf02ec79f08ff51 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 19 Apr 2015 16:01:41 -0400 Subject: Add Mailgun integration (incoming emails) --- app/Integration/MailgunWebhook.php | 82 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 app/Integration/MailgunWebhook.php (limited to 'app/Integration/MailgunWebhook.php') diff --git a/app/Integration/MailgunWebhook.php b/app/Integration/MailgunWebhook.php new file mode 100644 index 00000000..17338faa --- /dev/null +++ b/app/Integration/MailgunWebhook.php @@ -0,0 +1,82 @@ +user->getByEmail($payload['sender']); + + if (empty($user)) { + $this->container['logger']->debug('MailgunWebhook: ignored => user not found'); + return false; + } + + // The project must have a short name + $project = $this->project->getByIdentifier($this->getMailboxHash($payload['recipient'])); + + if (empty($project)) { + $this->container['logger']->debug('MailgunWebhook: ignored => project not found'); + return false; + } + + // The user must be member of the project + if (! $this->projectPermission->isMember($project['id'], $user['id'])) { + $this->container['logger']->debug('MailgunWebhook: ignored => user is not member of the project'); + return false; + } + + // Get the Markdown contents + if (empty($payload['stripped-html'])) { + $description = $payload['stripped-text']; + } + else { + $markdown = new HTML_To_Markdown($payload['stripped-html'], array('strip_tags' => true)); + $description = $markdown->output(); + } + + // Finally, we create the task + return (bool) $this->taskCreation->create(array( + 'project_id' => $project['id'], + 'title' => $payload['subject'], + 'description' => $description, + 'creator_id' => $user['id'], + )); + } + + /** + * Get the project identifier + * + * @access public + * @param string $email + * @return string + */ + public function getMailboxHash($email) + { + list($local_part,) = explode('@', $email); + list(,$identifier) = explode('+', $local_part); + + return $identifier; + } +} -- cgit v1.2.3