Add Sendgrid integration (incoming email handling)
This commit is contained in:
@@ -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';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
74
app/Integration/SendgridWebhook.php
Normal file
74
app/Integration/SendgridWebhook.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Integration;
|
||||
|
||||
use HTML_To_Markdown;
|
||||
use Core\Tool;
|
||||
|
||||
/**
|
||||
* Sendgrid Webhook
|
||||
*
|
||||
* @package integration
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class SendgridWebhook extends Base
|
||||
{
|
||||
/**
|
||||
* Parse incoming email
|
||||
*
|
||||
* @access public
|
||||
* @param array $payload Incoming email
|
||||
* @return boolean
|
||||
*/
|
||||
public function parsePayload(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'],
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -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',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -864,4 +864,6 @@ return array(
|
||||
// 'Help on Postmark integration' => '',
|
||||
// 'Mailgun (incoming emails)' => '',
|
||||
// 'Help on Mailgun integration' => '',
|
||||
// 'Sendgrid (incoming emails)' => '',
|
||||
// 'Help on Sendgrid integration' => '',
|
||||
);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -78,6 +78,7 @@ class ClassProvider implements ServiceProviderInterface
|
||||
'BitbucketWebhook',
|
||||
'Hipchat',
|
||||
'MailgunWebhook',
|
||||
'SendgridWebhook',
|
||||
'SlackWebhook',
|
||||
'PostmarkWebhook',
|
||||
)
|
||||
|
||||
@@ -12,6 +12,12 @@
|
||||
<p class="form-help"><a href="http://kanboard.net/documentation/mailgun" target="_blank"><?= t('Help on Mailgun integration') ?></a></p>
|
||||
</div>
|
||||
|
||||
<h3><img src="assets/img/sendgrid-icon.png"/> <?= t('Sendgrid (incoming emails)') ?></h3>
|
||||
<div class="listing">
|
||||
<input type="text" class="auto-select" readonly="readonly" value="<?= $this->getCurrentBaseUrl().$this->u('webhook', 'sendgrid', array('token' => $values['webhook_token'])) ?>"/><br/>
|
||||
<p class="form-help"><a href="http://kanboard.net/documentation/sendgrid" target="_blank"><?= t('Help on Sendgrid integration') ?></a></p>
|
||||
</div>
|
||||
|
||||
<h3><img src="assets/img/postmark-icon.png"/> <?= t('Postmark (incoming emails)') ?></h3>
|
||||
<div class="listing">
|
||||
<input type="text" class="auto-select" readonly="readonly" value="<?= $this->getCurrentBaseUrl().$this->u('webhook', 'postmark', array('token' => $values['webhook_token'])) ?>"/><br/>
|
||||
|
||||
Reference in New Issue
Block a user