From f2abf339120751f11f729606b46927332f886a1d Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 13 Jun 2015 13:17:16 -0400 Subject: Add Sendgrid as mail transport --- app/Controller/Base.php | 12 +++ app/Controller/Webhook.php | 39 +++------- app/Core/EmailClient.php | 3 + app/Integration/Sendgrid.php | 100 +++++++++++++++++++++++++ app/Integration/SendgridWebhook.php | 74 ------------------- app/ServiceProvider/ClassProvider.php | 2 +- app/constants.php | 2 + config.default.php | 6 +- docs/email-configuration.markdown | 20 ++++- tests/units/SendgridTest.php | 135 ++++++++++++++++++++++++++++++++++ tests/units/SendgridWebhookTest.php | 107 --------------------------- 11 files changed, 286 insertions(+), 214 deletions(-) create mode 100644 app/Integration/Sendgrid.php delete mode 100644 app/Integration/SendgridWebhook.php create mode 100644 tests/units/SendgridTest.php delete mode 100644 tests/units/SendgridWebhookTest.php diff --git a/app/Controller/Base.php b/app/Controller/Base.php index fcd07b99..19bb9ac9 100644 --- a/app/Controller/Base.php +++ b/app/Controller/Base.php @@ -210,6 +210,18 @@ abstract class Base extends \Core\Base } } + /** + * Check webhook token + * + * @access protected + */ + protected function checkWebhookToken() + { + if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) { + $this->response->text('Not Authorized', 401); + } + } + /** * Redirection when there is no project in the database * diff --git a/app/Controller/Webhook.php b/app/Controller/Webhook.php index 10a24e47..d04f83b3 100644 --- a/app/Controller/Webhook.php +++ b/app/Controller/Webhook.php @@ -17,9 +17,7 @@ class Webhook extends Base */ public function task() { - if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) { - $this->response->text('Not Authorized', 401); - } + $this->checkWebhookToken(); $defaultProject = $this->project->getFirst(); @@ -49,9 +47,7 @@ class Webhook extends Base */ public function github() { - if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) { - $this->response->text('Not Authorized', 401); - } + $this->checkWebhookToken(); $this->githubWebhook->setProjectId($this->request->getIntegerParam('project_id')); @@ -70,15 +66,10 @@ class Webhook extends Base */ public function gitlab() { - if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) { - $this->response->text('Not Authorized', 401); - } + $this->checkWebhookToken(); $this->gitlabWebhook->setProjectId($this->request->getIntegerParam('project_id')); - - $result = $this->gitlabWebhook->parsePayload( - $this->request->getJson() ?: array() - ); + $result = $this->gitlabWebhook->parsePayload($this->request->getJson() ?: array()); echo $result ? 'PARSED' : 'IGNORED'; } @@ -90,12 +81,9 @@ class Webhook extends Base */ public function bitbucket() { - if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) { - $this->response->text('Not Authorized', 401); - } + $this->checkWebhookToken(); $this->bitbucketWebhook->setProjectId($this->request->getIntegerParam('project_id')); - $result = $this->bitbucketWebhook->parsePayload(json_decode(@$_POST['payload'], true) ?: array()); echo $result ? 'PARSED' : 'IGNORED'; @@ -108,10 +96,7 @@ class Webhook extends Base */ public function postmark() { - if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) { - $this->response->text('Not Authorized', 401); - } - + $this->checkWebhookToken(); echo $this->postmark->receiveEmail($this->request->getJson() ?: array()) ? 'PARSED' : 'IGNORED'; } @@ -122,10 +107,7 @@ class Webhook extends Base */ public function mailgun() { - if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) { - $this->response->text('Not Authorized', 401); - } - + $this->checkWebhookToken(); echo $this->mailgun->receiveEmail($_POST) ? 'PARSED' : 'IGNORED'; } @@ -136,10 +118,7 @@ class Webhook extends Base */ public function sendgrid() { - if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) { - $this->response->text('Not Authorized', 401); - } - - echo $this->sendgridWebhook->parsePayload($_POST) ? 'PARSED' : 'IGNORED'; + $this->checkWebhookToken(); + echo $this->sendgrid->receiveEmail($_POST) ? 'PARSED' : 'IGNORED'; } } diff --git a/app/Core/EmailClient.php b/app/Core/EmailClient.php index 07687c42..b1986502 100644 --- a/app/Core/EmailClient.php +++ b/app/Core/EmailClient.php @@ -31,6 +31,9 @@ class EmailClient extends Base } switch (MAIL_TRANSPORT) { + case 'sendgrid': + $this->sendgrid->sendEmail($email, $name, $subject, $html, $author); + break; case 'mailgun': $this->mailgun->sendEmail($email, $name, $subject, $html, $author); break; diff --git a/app/Integration/Sendgrid.php b/app/Integration/Sendgrid.php new file mode 100644 index 00000000..902749f6 --- /dev/null +++ b/app/Integration/Sendgrid.php @@ -0,0 +1,100 @@ + SENDGRID_API_USER, + 'api_key' => SENDGRID_API_KEY, + 'to' => $email, + 'toname' => $name, + 'from' => MAIL_FROM, + 'fromname' => $author, + 'html' => $html, + 'subject' => $subject, + ); + + $this->httpClient->postForm('https://api.sendgrid.com/api/mail.send.json', $payload); + } + + /** + * Parse incoming email + * + * @access public + * @param array $payload Incoming email + * @return boolean + */ + public function receiveEmail(array $payload) + { + if (empty($payload['envelope']) || empty($payload['subject'])) { + return false; + } + + $envelope = json_decode($payload['envelope'], true); + $sender = isset($envelope['to'][0]) ? $envelope['to'][0] : ''; + + // The user must exists in Kanboard + $user = $this->user->getByEmail($envelope['from']); + + if (empty($user)) { + $this->container['logger']->debug('SendgridWebhook: ignored => user not found'); + return false; + } + + // The project must have a short name + $project = $this->project->getByIdentifier(Tool::getMailboxHash($sender)); + + if (empty($project)) { + $this->container['logger']->debug('SendgridWebhook: 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('SendgridWebhook: ignored => user is not member of the project'); + return false; + } + + // Get the Markdown contents + if (! empty($payload['html'])) { + $markdown = new HTML_To_Markdown($payload['html'], array('strip_tags' => true)); + $description = $markdown->output(); + } + else if (! empty($payload['text'])) { + $description = $payload['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/SendgridWebhook.php b/app/Integration/SendgridWebhook.php deleted file mode 100644 index 9125f00b..00000000 --- a/app/Integration/SendgridWebhook.php +++ /dev/null @@ -1,74 +0,0 @@ -user->getByEmail($envelope['from']); - - if (empty($user)) { - $this->container['logger']->debug('SendgridWebhook: ignored => user not found'); - return false; - } - - // The project must have a short name - $project = $this->project->getByIdentifier(Tool::getMailboxHash($sender)); - - if (empty($project)) { - $this->container['logger']->debug('SendgridWebhook: 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('SendgridWebhook: ignored => user is not member of the project'); - return false; - } - - // Get the Markdown contents - if (! empty($payload['html'])) { - $markdown = new HTML_To_Markdown($payload['html'], array('strip_tags' => true)); - $description = $markdown->output(); - } - else if (! empty($payload['text'])) { - $description = $payload['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/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index 28884b5a..4ecd357b 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -80,7 +80,7 @@ class ClassProvider implements ServiceProviderInterface 'Jabber', 'Mailgun', 'Postmark', - 'SendgridWebhook', + 'Sendgrid', 'SlackWebhook', 'Smtp', ) diff --git a/app/constants.php b/app/constants.php index 0b934569..9b66b746 100644 --- a/app/constants.php +++ b/app/constants.php @@ -67,6 +67,8 @@ defined('MAIL_SENDMAIL_COMMAND') or define('MAIL_SENDMAIL_COMMAND', '/usr/sbin/s 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', ''); +defined('SENDGRID_API_USER') or define('SENDGRID_API_USER', ''); +defined('SENDGRID_API_KEY') or define('SENDGRID_API_KEY', ''); // 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 e5fe4da3..7c6955e8 100644 --- a/config.default.php +++ b/config.default.php @@ -16,7 +16,7 @@ define('FILES_DIR', 'data/files/'); // E-mail address for the "From" header (notifications) define('MAIL_FROM', 'notifications@kanboard.local'); -// Mail transport available: "smtp", "sendmail", "mail" (PHP mail function), "postmark", "mailgun" +// Mail transport available: "smtp", "sendmail", "mail" (PHP mail function), "postmark", "mailgun", "sendgrid" define('MAIL_TRANSPORT', 'mail'); // SMTP configuration to use when the "smtp" transport is chosen @@ -38,6 +38,10 @@ define('MAILGUN_API_TOKEN', ''); // Mailgun domain name define('MAILGUN_DOMAIN', ''); +// Sendgrid API configuration +define('SENDGRID_API_USER', ''); +define('SENDGRID_API_KEY', ''); + // Database driver: sqlite, mysql or postgres (sqlite by default) define('DB_DRIVER', 'sqlite'); diff --git a/docs/email-configuration.markdown b/docs/email-configuration.markdown index 0d16a2fb..c66996c6 100644 --- a/docs/email-configuration.markdown +++ b/docs/email-configuration.markdown @@ -22,6 +22,7 @@ There are several email transports available: - PHP native mail function - Mailgun - Postmark +- Sendgrid Server settings --------------- @@ -93,7 +94,7 @@ define('MAILGUN_API_TOKEN', 'YOUR_API_KEY'); define('MAILGUN_DOMAIN', 'YOUR_DOMAIN_CONFIGURED_IN_MAILGUN'); // Be sure to use the sender email address configured in Mailgun -define('MAIL_FROM', 'sender-address-configured-in-postmark@example.org'); +define('MAIL_FROM', 'sender-address-configured-in-mailgun@example.org'); ``` ### Postmark HTTP API @@ -116,6 +117,23 @@ define('POSTMARK_API_TOKEN', 'COPY HERE YOUR POSTMARK API TOKEN'); define('MAIL_FROM', 'sender-address-configured-in-postmark@example.org'); ``` +### Sendgrid HTTP API + +You can use the HTTP API of Sendgrid to send emails. + +Configuration: + +```php +// We choose "sendgrid" as mail transport +define('MAIL_TRANSPORT', 'sendgrid'); + +// Sendgrid username +define('SENDGRID_API_USER', 'YOUR_SENDGRID_USERNAME'); + +// Sendgrid password +define('SENDGRID_API_KEY', 'YOUR_SENDGRID_PASSWORD'); +``` + ### The sender email address By default, emails will use the sender address `notifications@kanboard.local`. diff --git a/tests/units/SendgridTest.php b/tests/units/SendgridTest.php new file mode 100644 index 00000000..1814c761 --- /dev/null +++ b/tests/units/SendgridTest.php @@ -0,0 +1,135 @@ +container); + $pm->sendEmail('test@localhost', 'Me', 'Test', 'Content', 'Bob'); + + $this->assertEquals('https://api.sendgrid.com/api/mail.send.json', $this->container['httpClient']->getUrl()); + + $data = $this->container['httpClient']->getData(); + + $this->assertArrayHasKey('api_user', $data); + $this->assertArrayHasKey('api_key', $data); + $this->assertArrayHasKey('from', $data); + $this->assertArrayHasKey('fromname', $data); + $this->assertArrayHasKey('to', $data); + $this->assertArrayHasKey('toname', $data); + $this->assertArrayHasKey('subject', $data); + $this->assertArrayHasKey('html', $data); + + $this->assertEquals('test@localhost', $data['to']); + $this->assertEquals('Me', $data['toname']); + $this->assertEquals('notifications@kanboard.local', $data['from']); + $this->assertEquals('Bob', $data['fromname']); + $this->assertEquals('Test', $data['subject']); + $this->assertEquals('Content', $data['html']); + $this->assertEquals('', $data['api_key']); + $this->assertEquals('', $data['api_user']); + } + + public function testHandlePayload() + { + $w = new Sendgrid($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( + 'envelope' => '{"to":["a@b.c"],"from":"a.b.c"}', + 'subject' => 'Email task' + ))); + + // Project not found + $this->assertFalse($w->receiveEmail(array( + 'envelope' => '{"to":["a@b.c"],"from":"me@localhost"}', + 'subject' => 'Email task' + ))); + + // User is not member + $this->assertFalse($w->receiveEmail(array( + 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', + 'subject' => 'Email task' + ))); + + $this->assertTrue($pp->addMember(2, 2)); + + // The task must be created + $this->assertTrue($w->receiveEmail(array( + 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', + 'subject' => 'Email task' + ))); + + $task = $tf->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('', $task['description']); + $this->assertEquals(2, $task['creator_id']); + + // Html content + $this->assertTrue($w->receiveEmail(array( + 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', + 'subject' => 'Email task', + 'html' => 'bold text', + ))); + + $task = $tf->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('**bold** text', $task['description']); + $this->assertEquals(2, $task['creator_id']); + + // Text content + $this->assertTrue($w->receiveEmail(array( + 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', + 'subject' => 'Email task', + 'text' => '**bold** text', + ))); + + $task = $tf->getById(3); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('**bold** text', $task['description']); + $this->assertEquals(2, $task['creator_id']); + + // Text + html content + $this->assertTrue($w->receiveEmail(array( + 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', + 'subject' => 'Email task', + 'html' => 'bold html', + 'text' => '**bold** text', + ))); + + $task = $tf->getById(4); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('**bold** html', $task['description']); + $this->assertEquals(2, $task['creator_id']); + } +} diff --git a/tests/units/SendgridWebhookTest.php b/tests/units/SendgridWebhookTest.php deleted file mode 100644 index 3b30d212..00000000 --- a/tests/units/SendgridWebhookTest.php +++ /dev/null @@ -1,107 +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( - 'envelope' => '{"to":["a@b.c"],"from":"a.b.c"}', - 'subject' => 'Email task' - ))); - - // Project not found - $this->assertFalse($w->parsePayload(array( - 'envelope' => '{"to":["a@b.c"],"from":"me@localhost"}', - 'subject' => 'Email task' - ))); - - // User is not member - $this->assertFalse($w->parsePayload(array( - 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', - 'subject' => 'Email task' - ))); - - $this->assertTrue($pp->addMember(2, 2)); - - // The task must be created - $this->assertTrue($w->parsePayload(array( - 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', - 'subject' => 'Email task' - ))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals('Email task', $task['title']); - $this->assertEquals('', $task['description']); - $this->assertEquals(2, $task['creator_id']); - - // Html content - $this->assertTrue($w->parsePayload(array( - 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', - 'subject' => 'Email task', - 'html' => 'bold text', - ))); - - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals('Email task', $task['title']); - $this->assertEquals('**bold** text', $task['description']); - $this->assertEquals(2, $task['creator_id']); - - // Text content - $this->assertTrue($w->parsePayload(array( - 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', - 'subject' => 'Email task', - 'text' => '**bold** text', - ))); - - $task = $tf->getById(3); - $this->assertNotEmpty($task); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals('Email task', $task['title']); - $this->assertEquals('**bold** text', $task['description']); - $this->assertEquals(2, $task['creator_id']); - - // Text + html content - $this->assertTrue($w->parsePayload(array( - 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', - 'subject' => 'Email task', - 'html' => 'bold html', - 'text' => '**bold** text', - ))); - - $task = $tf->getById(4); - $this->assertNotEmpty($task); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals('Email task', $task['title']); - $this->assertEquals('**bold** html', $task['description']); - $this->assertEquals(2, $task['creator_id']); - } -} -- cgit v1.2.3