Add Mailgun integration (incoming emails)

This commit is contained in:
Frederic Guillot
2015-04-19 16:01:41 -04:00
parent 392133d9ba
commit ac86c3100a
27 changed files with 255 additions and 3 deletions

View File

@@ -112,8 +112,20 @@ class Webhook extends Base
$this->response->text('Not Authorized', 401);
}
$result = $this->postmarkWebhook->parsePayload($this->request->getJson() ?: array());
echo $this->postmarkWebhook->parsePayload($this->request->getJson() ?: array()) ? 'PARSED' : 'IGNORED';
}
echo $result ? 'PARSED' : 'IGNORED';
/**
* Handle Mailgun webhooks
*
* @access public
*/
public function mailgun()
{
if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) {
$this->response->text('Not Authorized', 401);
}
echo $this->mailgunWebhook->parsePayload($_POST) ? 'PARSED' : 'IGNORED';
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Integration;
use HTML_To_Markdown;
/**
* Mailgun Webhook
*
* @package integration
* @author Frederic Guillot
*/
class MailgunWebhook extends Base
{
/**
* Parse incoming email
*
* @access public
* @param array $payload Incoming email
* @return boolean
*/
public function parsePayload(array $payload)
{
if (empty($payload['sender']) || empty($payload['subject']) || empty($payload['recipient']) || empty($payload['stripped-text'])) {
return false;
}
// The user must exists in Kanboard
$user = $this->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($this->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'])) {
$description = $payload['stripped-text'];
}
else {
$markdown = new HTML_To_Markdown($payload['stripped-html'], array('strip_tags' => true));
$description = $markdown->output();
}
// Finally, we create the task
return (bool) $this->taskCreation->create(array(
'project_id' => $project['id'],
'title' => $payload['subject'],
'description' => $description,
'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;
}
}

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -864,4 +864,6 @@ return array(
'Identifier' => 'Identificateur',
'Postmark (incoming emails)' => 'Postmark (emails entrants)',
'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',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -862,4 +862,6 @@ return array(
// 'Identifier' => '',
// 'Postmark (incoming emails)' => '',
// 'Help on Postmark integration' => '',
// 'Mailgun (incoming emails)' => '',
// 'Help on Mailgun integration' => '',
);

View File

@@ -68,6 +68,10 @@ class Project extends Base
*/
public function getByIdentifier($identifier)
{
if (empty($identifier)) {
return false;
}
return $this->db->table(self::TABLE)->eq('identifier', strtoupper($identifier))->findOne();
}

View File

@@ -77,6 +77,7 @@ class ClassProvider implements ServiceProviderInterface
'GithubWebhook',
'BitbucketWebhook',
'Hipchat',
'MailgunWebhook',
'SlackWebhook',
'PostmarkWebhook',
)

View File

@@ -6,6 +6,12 @@
<?= $this->formCsrf() ?>
<h3><img src="assets/img/mailgun-icon.png"/>&nbsp;<?= t('Mailgun (incoming emails)') ?></h3>
<div class="listing">
<input type="text" class="auto-select" readonly="readonly" value="<?= $this->getCurrentBaseUrl().$this->u('webhook', 'mailgun', array('token' => $values['webhook_token'])) ?>"/><br/>
<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/postmark-icon.png"/>&nbsp;<?= 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/>