From f190be9e2d4d285fb71d84e5d3884206067cf7af Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 19 Apr 2015 19:23:42 -0400 Subject: Add Sendgrid integration (incoming email handling) --- app/Controller/Webhook.php | 14 +++++++ app/Core/Tool.php | 20 +++++++++ app/Integration/MailgunWebhook.php | 29 +++++-------- app/Integration/PostmarkWebhook.php | 11 +++-- app/Integration/SendgridWebhook.php | 74 ++++++++++++++++++++++++++++++++++ app/Locale/da_DK/translations.php | 2 + app/Locale/de_DE/translations.php | 2 + app/Locale/es_ES/translations.php | 2 + app/Locale/fi_FI/translations.php | 2 + app/Locale/fr_FR/translations.php | 2 + app/Locale/hu_HU/translations.php | 2 + app/Locale/it_IT/translations.php | 2 + app/Locale/ja_JP/translations.php | 2 + app/Locale/nl_NL/translations.php | 2 + app/Locale/pl_PL/translations.php | 2 + app/Locale/pt_BR/translations.php | 2 + app/Locale/ru_RU/translations.php | 2 + app/Locale/sr_Latn_RS/translations.php | 2 + app/Locale/sv_SE/translations.php | 2 + app/Locale/th_TH/translations.php | 2 + app/Locale/tr_TR/translations.php | 2 + app/Locale/zh_CN/translations.php | 2 + app/Model/User.php | 4 ++ app/ServiceProvider/ClassProvider.php | 1 + app/Template/config/integrations.php | 6 +++ 25 files changed, 169 insertions(+), 24 deletions(-) create mode 100644 app/Integration/SendgridWebhook.php (limited to 'app') diff --git a/app/Controller/Webhook.php b/app/Controller/Webhook.php index 06bfcd4e..667c5087 100644 --- a/app/Controller/Webhook.php +++ b/app/Controller/Webhook.php @@ -128,4 +128,18 @@ class Webhook extends Base echo $this->mailgunWebhook->parsePayload($_POST) ? 'PARSED' : 'IGNORED'; } + + /** + * Handle Sendgrid webhooks + * + * @access public + */ + 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'; + } } diff --git a/app/Core/Tool.php b/app/Core/Tool.php index ade99cad..84e42ba8 100644 --- a/app/Core/Tool.php +++ b/app/Core/Tool.php @@ -31,4 +31,24 @@ class Tool fclose($fp); } } + + /** + * Get the mailbox hash from an email address + * + * @static + * @access public + * @param string $email + * @return string + */ + public static function getMailboxHash($email) + { + if (! strpos($email, '@') || ! strpos($email, '+')) { + return ''; + } + + list($local_part,) = explode('@', $email); + list(,$identifier) = explode('+', $local_part); + + return $identifier; + } } diff --git a/app/Integration/MailgunWebhook.php b/app/Integration/MailgunWebhook.php index 17338faa..8e542513 100644 --- a/app/Integration/MailgunWebhook.php +++ b/app/Integration/MailgunWebhook.php @@ -3,6 +3,7 @@ namespace Integration; use HTML_To_Markdown; +use Core\Tool; /** * Mailgun Webhook @@ -21,7 +22,7 @@ class MailgunWebhook extends Base */ public function parsePayload(array $payload) { - if (empty($payload['sender']) || empty($payload['subject']) || empty($payload['recipient']) || empty($payload['stripped-text'])) { + if (empty($payload['sender']) || empty($payload['subject']) || empty($payload['recipient'])) { return false; } @@ -34,7 +35,7 @@ class MailgunWebhook extends Base } // The project must have a short name - $project = $this->project->getByIdentifier($this->getMailboxHash($payload['recipient'])); + $project = $this->project->getByIdentifier(Tool::getMailboxHash($payload['recipient'])); if (empty($project)) { $this->container['logger']->debug('MailgunWebhook: ignored => project not found'); @@ -48,12 +49,15 @@ class MailgunWebhook extends Base } // Get the Markdown contents - if (empty($payload['stripped-html'])) { + 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 { - $markdown = new HTML_To_Markdown($payload['stripped-html'], array('strip_tags' => true)); - $description = $markdown->output(); + $description = ''; } // Finally, we create the task @@ -64,19 +68,4 @@ class MailgunWebhook extends Base '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; - } } diff --git a/app/Integration/PostmarkWebhook.php b/app/Integration/PostmarkWebhook.php index 6387ba20..642955df 100644 --- a/app/Integration/PostmarkWebhook.php +++ b/app/Integration/PostmarkWebhook.php @@ -21,7 +21,7 @@ class PostmarkWebhook extends Base */ public function parsePayload(array $payload) { - if (empty($payload['From']) || empty($payload['Subject']) || empty($payload['MailboxHash']) || empty($payload['TextBody'])) { + if (empty($payload['From']) || empty($payload['Subject']) || empty($payload['MailboxHash'])) { return false; } @@ -48,12 +48,15 @@ class PostmarkWebhook extends Base } // Get the Markdown contents - if (empty($payload['HtmlBody'])) { + if (! empty($payload['HtmlBody'])) { + $markdown = new HTML_To_Markdown($payload['HtmlBody'], array('strip_tags' => true)); + $description = $markdown->output(); + } + else if (! empty($payload['TextBody'])) { $description = $payload['TextBody']; } else { - $markdown = new HTML_To_Markdown($payload['HtmlBody'], array('strip_tags' => true)); - $description = $markdown->output(); + $description = ''; } // Finally, we create the task diff --git a/app/Integration/SendgridWebhook.php b/app/Integration/SendgridWebhook.php new file mode 100644 index 00000000..142ed49f --- /dev/null +++ b/app/Integration/SendgridWebhook.php @@ -0,0 +1,74 @@ +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/Locale/da_DK/translations.php b/app/Locale/da_DK/translations.php index cc380929..f1352b8d 100644 --- a/app/Locale/da_DK/translations.php +++ b/app/Locale/da_DK/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/de_DE/translations.php b/app/Locale/de_DE/translations.php index 20d45977..2fc01f3e 100644 --- a/app/Locale/de_DE/translations.php +++ b/app/Locale/de_DE/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/es_ES/translations.php b/app/Locale/es_ES/translations.php index f1d344be..c150bfe5 100644 --- a/app/Locale/es_ES/translations.php +++ b/app/Locale/es_ES/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/fi_FI/translations.php b/app/Locale/fi_FI/translations.php index 728a5746..99ead93c 100644 --- a/app/Locale/fi_FI/translations.php +++ b/app/Locale/fi_FI/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/fr_FR/translations.php b/app/Locale/fr_FR/translations.php index 54370f3a..709131a6 100644 --- a/app/Locale/fr_FR/translations.php +++ b/app/Locale/fr_FR/translations.php @@ -866,4 +866,6 @@ return array( 'Help on Postmark integration' => 'Aide sur l\'intégration avec Postmark', 'Mailgun (incoming emails)' => 'Mailgun (emails entrants)', 'Help on Mailgun integration' => 'Aide sur l\'intégration avec Mailgun', + 'Sendgrid (incoming emails)' => 'Sendgrid (emails entrants)', + 'Help on Sendgrid integration' => 'Aide sur l\'intégration avec Sendgrid', ); diff --git a/app/Locale/hu_HU/translations.php b/app/Locale/hu_HU/translations.php index feca5d32..d2d0d6e9 100644 --- a/app/Locale/hu_HU/translations.php +++ b/app/Locale/hu_HU/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/it_IT/translations.php b/app/Locale/it_IT/translations.php index 8601af4f..c2882ffc 100644 --- a/app/Locale/it_IT/translations.php +++ b/app/Locale/it_IT/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/ja_JP/translations.php b/app/Locale/ja_JP/translations.php index 3a08da17..a35ff2e9 100644 --- a/app/Locale/ja_JP/translations.php +++ b/app/Locale/ja_JP/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/nl_NL/translations.php b/app/Locale/nl_NL/translations.php index 4a53bf48..06a7026a 100644 --- a/app/Locale/nl_NL/translations.php +++ b/app/Locale/nl_NL/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/pl_PL/translations.php b/app/Locale/pl_PL/translations.php index 519fb32b..c91d4ba1 100644 --- a/app/Locale/pl_PL/translations.php +++ b/app/Locale/pl_PL/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/pt_BR/translations.php b/app/Locale/pt_BR/translations.php index 953f3a59..1e5dae59 100644 --- a/app/Locale/pt_BR/translations.php +++ b/app/Locale/pt_BR/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/ru_RU/translations.php b/app/Locale/ru_RU/translations.php index dee35d70..53616d9a 100644 --- a/app/Locale/ru_RU/translations.php +++ b/app/Locale/ru_RU/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/sr_Latn_RS/translations.php b/app/Locale/sr_Latn_RS/translations.php index c4bab8cb..fd5c4f5d 100644 --- a/app/Locale/sr_Latn_RS/translations.php +++ b/app/Locale/sr_Latn_RS/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/sv_SE/translations.php b/app/Locale/sv_SE/translations.php index 22286c36..7f765815 100644 --- a/app/Locale/sv_SE/translations.php +++ b/app/Locale/sv_SE/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/th_TH/translations.php b/app/Locale/th_TH/translations.php index c8e253b6..f282beee 100644 --- a/app/Locale/th_TH/translations.php +++ b/app/Locale/th_TH/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/tr_TR/translations.php b/app/Locale/tr_TR/translations.php index 5fb403e8..11cba833 100644 --- a/app/Locale/tr_TR/translations.php +++ b/app/Locale/tr_TR/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Locale/zh_CN/translations.php b/app/Locale/zh_CN/translations.php index 7470bae1..09df6d45 100644 --- a/app/Locale/zh_CN/translations.php +++ b/app/Locale/zh_CN/translations.php @@ -864,4 +864,6 @@ return array( // 'Help on Postmark integration' => '', // 'Mailgun (incoming emails)' => '', // 'Help on Mailgun integration' => '', + // 'Sendgrid (incoming emails)' => '', + // 'Help on Sendgrid integration' => '', ); diff --git a/app/Model/User.php b/app/Model/User.php index d9f174bd..be2b034b 100644 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -150,6 +150,10 @@ class User extends Base */ public function getByEmail($email) { + if (empty($email)) { + return false; + } + return $this->db->table(self::TABLE)->eq('email', $email)->findOne(); } diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index de9de546..a64ac061 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -78,6 +78,7 @@ class ClassProvider implements ServiceProviderInterface 'BitbucketWebhook', 'Hipchat', 'MailgunWebhook', + 'SendgridWebhook', 'SlackWebhook', 'PostmarkWebhook', ) diff --git a/app/Template/config/integrations.php b/app/Template/config/integrations.php index a012b566..bf051cfc 100644 --- a/app/Template/config/integrations.php +++ b/app/Template/config/integrations.php @@ -12,6 +12,12 @@

+

 

+
+
+

+
+

 


-- cgit v1.2.3