From e22da9d32addefa8d739171febcf6f6d0c06ba7b Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 7 Jun 2015 22:17:50 -0400 Subject: Add Mailgun API as mail transport --- app/Integration/HipchatWebhook.php | 2 +- app/Integration/Mailgun.php | 97 ++++++++++++++++++++++++++++++++++++++ app/Integration/MailgunWebhook.php | 71 ---------------------------- app/Integration/Postmark.php | 2 +- app/Integration/SlackWebhook.php | 2 +- 5 files changed, 100 insertions(+), 74 deletions(-) create mode 100644 app/Integration/Mailgun.php delete mode 100644 app/Integration/MailgunWebhook.php (limited to 'app/Integration') diff --git a/app/Integration/HipchatWebhook.php b/app/Integration/HipchatWebhook.php index 5cd01fb0..f1be0f34 100644 --- a/app/Integration/HipchatWebhook.php +++ b/app/Integration/HipchatWebhook.php @@ -89,7 +89,7 @@ class HipchatWebhook extends \Core\Base $params['room_token'] ); - $this->httpClient->post($url, $payload); + $this->httpClient->postJson($url, $payload); } } } diff --git a/app/Integration/Mailgun.php b/app/Integration/Mailgun.php new file mode 100644 index 00000000..1451b211 --- /dev/null +++ b/app/Integration/Mailgun.php @@ -0,0 +1,97 @@ + sprintf('%s <%s>', $author, MAIL_FROM), + 'to' => sprintf('%s <%s>', $name, $email), + 'subject' => $subject, + 'html' => $html, + ); + + $this->httpClient->postForm('https://api.mailgun.net/v3/'.MAILGUN_DOMAIN.'/messages', $payload, $headers); + } + + /** + * Parse incoming email + * + * @access public + * @param array $payload Incoming email + * @return boolean + */ + public function receiveEmail(array $payload) + { + if (empty($payload['sender']) || empty($payload['subject']) || empty($payload['recipient'])) { + return false; + } + + // The user must exists in Kanboard + $user = $this->user->getByEmail($payload['sender']); + + if (empty($user)) { + $this->container['logger']->debug('Mailgun: ignored => user not found'); + return false; + } + + // The project must have a short name + $project = $this->project->getByIdentifier(Tool::getMailboxHash($payload['recipient'])); + + if (empty($project)) { + $this->container['logger']->debug('Mailgun: 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('Mailgun: ignored => user is not member of the project'); + return false; + } + + // Get the Markdown contents + if (! empty($payload['stripped-html'])) { + $markdown = new HTML_To_Markdown($payload['stripped-html'], array('strip_tags' => true)); + $description = $markdown->output(); + } + else if (! empty($payload['stripped-text'])) { + $description = $payload['stripped-text']; + } + else { + $description = ''; + } + + // Finally, we create the task + return (bool) $this->taskCreation->create(array( + 'project_id' => $project['id'], + 'title' => $payload['subject'], + 'description' => $description, + 'creator_id' => $user['id'], + )); + } +} diff --git a/app/Integration/MailgunWebhook.php b/app/Integration/MailgunWebhook.php deleted file mode 100644 index 50d96a4a..00000000 --- a/app/Integration/MailgunWebhook.php +++ /dev/null @@ -1,71 +0,0 @@ -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(Tool::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'])) { - $markdown = new HTML_To_Markdown($payload['stripped-html'], array('strip_tags' => true)); - $description = $markdown->output(); - } - else if (! empty($payload['stripped-text'])) { - $description = $payload['stripped-text']; - } - else { - $description = ''; - } - - // Finally, we create the task - return (bool) $this->taskCreation->create(array( - 'project_id' => $project['id'], - 'title' => $payload['subject'], - 'description' => $description, - 'creator_id' => $user['id'], - )); - } -} diff --git a/app/Integration/Postmark.php b/app/Integration/Postmark.php index a367c23e..dbb70aee 100644 --- a/app/Integration/Postmark.php +++ b/app/Integration/Postmark.php @@ -36,7 +36,7 @@ class Postmark extends \Core\Base 'HtmlBody' => $html, ); - $this->httpClient->post('https://api.postmarkapp.com/email', $payload, $headers); + $this->httpClient->postJson('https://api.postmarkapp.com/email', $payload, $headers); } /** diff --git a/app/Integration/SlackWebhook.php b/app/Integration/SlackWebhook.php index 8be33496..975ea21f 100644 --- a/app/Integration/SlackWebhook.php +++ b/app/Integration/SlackWebhook.php @@ -69,7 +69,7 @@ class SlackWebhook extends \Core\Base $payload['text'] .= '|'.t('view the task on Kanboard').'>'; } - $this->httpClient->post($this->getWebhookUrl($project_id), $payload); + $this->httpClient->postJson($this->getWebhookUrl($project_id), $payload); } } } -- cgit v1.2.3