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/Controller/Webhook.php | 2 +- app/Core/Base.php | 2 +- app/Core/EmailClient.php | 3 ++ app/Core/HttpClient.php | 50 ++++++++++++++---- 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 +- app/Model/Webhook.php | 2 +- app/ServiceProvider/ClassProvider.php | 2 +- app/constants.php | 2 + config.default.php | 12 ++++- docs/email-configuration.markdown | 21 ++++++++ tests/units/Base.php | 10 +++- tests/units/MailgunTest.php | 71 +++++++++++++++++++++++++ tests/units/MailgunWebhookTest.php | 51 ------------------ 17 files changed, 261 insertions(+), 141 deletions(-) create mode 100644 app/Integration/Mailgun.php delete mode 100644 app/Integration/MailgunWebhook.php create mode 100644 tests/units/MailgunTest.php delete mode 100644 tests/units/MailgunWebhookTest.php diff --git a/app/Controller/Webhook.php b/app/Controller/Webhook.php index ddbf7d62..10a24e47 100644 --- a/app/Controller/Webhook.php +++ b/app/Controller/Webhook.php @@ -126,7 +126,7 @@ class Webhook extends Base $this->response->text('Not Authorized', 401); } - echo $this->mailgunWebhook->parsePayload($_POST) ? 'PARSED' : 'IGNORED'; + echo $this->mailgun->receiveEmail($_POST) ? 'PARSED' : 'IGNORED'; } /** diff --git a/app/Core/Base.php b/app/Core/Base.php index 5e179b13..6cb87cbc 100644 --- a/app/Core/Base.php +++ b/app/Core/Base.php @@ -22,7 +22,7 @@ use Pimple\Container; * @property \Integration\GitlabWebhook $gitlabWebhook * @property \Integration\HipchatWebhook $hipchatWebhook * @property \Integration\Jabber $jabber - * @property \Integration\MailgunWebhook $mailgunWebhook + * @property \Integration\Mailgun $mailgun * @property \Integration\Postmark $postmark * @property \Integration\SendgridWebhook $sendgridWebhook * @property \Integration\SlackWebhook $slackWebhook diff --git a/app/Core/EmailClient.php b/app/Core/EmailClient.php index 980f5acc..07687c42 100644 --- a/app/Core/EmailClient.php +++ b/app/Core/EmailClient.php @@ -31,6 +31,9 @@ class EmailClient extends Base } switch (MAIL_TRANSPORT) { + case 'mailgun': + $this->mailgun->sendEmail($email, $name, $subject, $html, $author); + break; case 'postmark': $this->postmark->sendEmail($email, $name, $subject, $html, $author); break; diff --git a/app/Core/HttpClient.php b/app/Core/HttpClient.php index fcfb1a47..2f280a1e 100644 --- a/app/Core/HttpClient.php +++ b/app/Core/HttpClient.php @@ -32,7 +32,7 @@ class HttpClient extends Base const HTTP_USER_AGENT = 'Kanboard'; /** - * Send a POST HTTP request + * Send a POST HTTP request encoded in JSON * * @access public * @param string $url @@ -40,17 +40,49 @@ class HttpClient extends Base * @param array $headers * @return string */ - public function post($url, array $data, array $headers = array()) + public function postJson($url, array $data, array $headers = array()) + { + return $this->doRequest( + $url, + json_encode($data), + array_merge(array('Content-type: application/json'), $headers) + ); + } + + /** + * Send a POST HTTP request encoded in www-form-urlencoded + * + * @access public + * @param string $url + * @param array $data + * @param array $headers + * @return string + */ + public function postForm($url, array $data, array $headers = array()) + { + return $this->doRequest( + $url, + http_build_query($data), + array_merge(array('Content-type: application/x-www-form-urlencoded'), $headers) + ); + } + + /** + * Make the HTTP request + * + * @access private + * @param string $url + * @param array $content + * @param array $headers + * @return string + */ + private function doRequest($url, $content, array $headers) { if (empty($url)) { return ''; } - $headers = array_merge(array( - 'User-Agent: '.self::HTTP_USER_AGENT, - 'Content-Type: application/json', - 'Connection: close', - ), $headers); + $headers = array_merge(array('User-Agent: '.self::HTTP_USER_AGENT, 'Connection: close'), $headers); $context = stream_context_create(array( 'http' => array( @@ -59,7 +91,7 @@ class HttpClient extends Base 'timeout' => self::HTTP_TIMEOUT, 'max_redirects' => self::HTTP_MAX_REDIRECTS, 'header' => implode("\r\n", $headers), - 'content' => json_encode($data) + 'content' => $content ) )); @@ -75,7 +107,7 @@ class HttpClient extends Base if (DEBUG) { $this->container['logger']->debug('HttpClient: url='.$url); - $this->container['logger']->debug('HttpClient: payload='.var_export($data, true)); + $this->container['logger']->debug('HttpClient: payload='.$content); $this->container['logger']->debug('HttpClient: metadata='.var_export(@stream_get_meta_data($stream), true)); $this->container['logger']->debug('HttpClient: response='.$response); } 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); } } } diff --git a/app/Model/Webhook.php b/app/Model/Webhook.php index 8c270fb6..e3af37f7 100644 --- a/app/Model/Webhook.php +++ b/app/Model/Webhook.php @@ -30,7 +30,7 @@ class Webhook extends Base $url .= '?token='.$token; } - return $this->httpClient->post($url, $values); + return $this->httpClient->postJson($url, $values); } } } diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index 3fc6c850..28884b5a 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -78,7 +78,7 @@ class ClassProvider implements ServiceProviderInterface 'GitlabWebhook', 'HipchatWebhook', 'Jabber', - 'MailgunWebhook', + 'Mailgun', 'Postmark', 'SendgridWebhook', 'SlackWebhook', diff --git a/app/constants.php b/app/constants.php index b487e0bd..0b934569 100644 --- a/app/constants.php +++ b/app/constants.php @@ -65,6 +65,8 @@ defined('MAIL_SMTP_PASSWORD') or define('MAIL_SMTP_PASSWORD', ''); defined('MAIL_SMTP_ENCRYPTION') or define('MAIL_SMTP_ENCRYPTION', null); defined('MAIL_SENDMAIL_COMMAND') or define('MAIL_SENDMAIL_COMMAND', '/usr/sbin/sendmail -bs'); defined('POSTMARK_API_TOKEN') or define('POSTMARK_API_TOKEN', ''); +defined('MAILGUN_API_TOKEN') or define('MAILGUN_API_TOKEN', ''); +defined('MAILGUN_DOMAIN') or define('MAILGUN_DOMAIN', ''); // Enable or disable "Strict-Transport-Security" HTTP header defined('ENABLE_HSTS') or define('ENABLE_HSTS', true); diff --git a/config.default.php b/config.default.php index 99698420..e5fe4da3 100644 --- a/config.default.php +++ b/config.default.php @@ -1,6 +1,8 @@ data, JSON_PRETTY_PRINT); } - public function post($url, array $data, array $headers = array()) + public function postJson($url, array $data, array $headers = array()) + { + $this->url = $url; + $this->data = $data; + $this->headers = $headers; + return true; + } + + public function postForm($url, array $data, array $headers = array()) { $this->url = $url; $this->data = $data; diff --git a/tests/units/MailgunTest.php b/tests/units/MailgunTest.php new file mode 100644 index 00000000..b33a66fe --- /dev/null +++ b/tests/units/MailgunTest.php @@ -0,0 +1,71 @@ +container); + $pm->sendEmail('test@localhost', 'Me', 'Test', 'Content', 'Bob'); + + $this->assertStringStartsWith('https://api.mailgun.net/v3/', $this->container['httpClient']->getUrl()); + + $data = $this->container['httpClient']->getData(); + + $this->assertArrayHasKey('from', $data); + $this->assertArrayHasKey('to', $data); + $this->assertArrayHasKey('subject', $data); + $this->assertArrayHasKey('html', $data); + + $this->assertEquals('Me ', $data['to']); + $this->assertEquals('Bob ', $data['from']); + $this->assertEquals('Test', $data['subject']); + $this->assertEquals('Content', $data['html']); + } + + public function testHandlePayload() + { + $w = new Mailgun($this->container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new User($this->container); + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(2, $u->create(array('name' => 'me', 'email' => 'me@localhost'))); + + $this->assertEquals(1, $p->create(array('name' => 'test1'))); + $this->assertEquals(2, $p->create(array('name' => 'test2', 'identifier' => 'TEST1'))); + + // Empty payload + $this->assertFalse($w->receiveEmail(array())); + + // Unknown user + $this->assertFalse($w->receiveEmail(array('sender' => 'a@b.c', 'subject' => 'Email task', 'recipient' => 'foobar', 'stripped-text' => 'boo'))); + + // Project not found + $this->assertFalse($w->receiveEmail(array('sender' => 'me@localhost', 'subject' => 'Email task', 'recipient' => 'foo+test@localhost', 'stripped-text' => 'boo'))); + + // User is not member + $this->assertFalse($w->receiveEmail(array('sender' => 'me@localhost', 'subject' => 'Email task', 'recipient' => 'foo+test1@localhost', 'stripped-text' => 'boo'))); + $this->assertTrue($pp->addMember(2, 2)); + + // The task must be created + $this->assertTrue($w->receiveEmail(array('sender' => 'me@localhost', 'subject' => 'Email task', 'recipient' => 'foo+test1@localhost', 'stripped-text' => 'boo'))); + + $task = $tf->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('boo', $task['description']); + $this->assertEquals(2, $task['creator_id']); + } +} diff --git a/tests/units/MailgunWebhookTest.php b/tests/units/MailgunWebhookTest.php deleted file mode 100644 index c2745180..00000000 --- a/tests/units/MailgunWebhookTest.php +++ /dev/null @@ -1,51 +0,0 @@ -container); - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $u = new User($this->container); - $tc = new TaskCreation($this->container); - $tf = new TaskFinder($this->container); - - $this->assertEquals(2, $u->create(array('name' => 'me', 'email' => 'me@localhost'))); - - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2', 'identifier' => 'TEST1'))); - - // Empty payload - $this->assertFalse($w->parsePayload(array())); - - // Unknown user - $this->assertFalse($w->parsePayload(array('sender' => 'a@b.c', 'subject' => 'Email task', 'recipient' => 'foobar', 'stripped-text' => 'boo'))); - - // Project not found - $this->assertFalse($w->parsePayload(array('sender' => 'me@localhost', 'subject' => 'Email task', 'recipient' => 'foo+test@localhost', 'stripped-text' => 'boo'))); - - // User is not member - $this->assertFalse($w->parsePayload(array('sender' => 'me@localhost', 'subject' => 'Email task', 'recipient' => 'foo+test1@localhost', 'stripped-text' => 'boo'))); - $this->assertTrue($pp->addMember(2, 2)); - - // The task must be created - $this->assertTrue($w->parsePayload(array('sender' => 'me@localhost', 'subject' => 'Email task', 'recipient' => 'foo+test1@localhost', 'stripped-text' => 'boo'))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals('Email task', $task['title']); - $this->assertEquals('boo', $task['description']); - $this->assertEquals(2, $task['creator_id']); - } -} -- cgit v1.2.3