Add Mailgun API as mail transport

This commit is contained in:
Frederic Guillot 2015-06-07 22:17:50 -04:00
parent 4f32352fe6
commit e22da9d32a
15 changed files with 153 additions and 33 deletions

View File

@ -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';
}
/**

View File

@ -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

View File

@ -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;

View File

@ -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);
}

View File

@ -89,7 +89,7 @@ class HipchatWebhook extends \Core\Base
$params['room_token']
);
$this->httpClient->post($url, $payload);
$this->httpClient->postJson($url, $payload);
}
}
}

View File

@ -6,13 +6,39 @@ use HTML_To_Markdown;
use Core\Tool;
/**
* Mailgun Webhook
* Mailgun Integration
*
* @package integration
* @author Frederic Guillot
*/
class MailgunWebhook extends \Core\Base
class Mailgun extends \Core\Base
{
/**
* Send a HTML email
*
* @access public
* @param string $email
* @param string $name
* @param string $subject
* @param string $html
* @param string $author
*/
public function sendEmail($email, $name, $subject, $html, $author)
{
$headers = array(
'Authorization: Basic '.base64_encode('api:'.MAILGUN_API_TOKEN)
);
$payload = array(
'from' => 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
*
@ -20,7 +46,7 @@ class MailgunWebhook extends \Core\Base
* @param array $payload Incoming email
* @return boolean
*/
public function parsePayload(array $payload)
public function receiveEmail(array $payload)
{
if (empty($payload['sender']) || empty($payload['subject']) || empty($payload['recipient'])) {
return false;
@ -30,7 +56,7 @@ class MailgunWebhook extends \Core\Base
$user = $this->user->getByEmail($payload['sender']);
if (empty($user)) {
$this->container['logger']->debug('MailgunWebhook: ignored => user not found');
$this->container['logger']->debug('Mailgun: ignored => user not found');
return false;
}
@ -38,13 +64,13 @@ class MailgunWebhook extends \Core\Base
$project = $this->project->getByIdentifier(Tool::getMailboxHash($payload['recipient']));
if (empty($project)) {
$this->container['logger']->debug('MailgunWebhook: ignored => project not found');
$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('MailgunWebhook: ignored => user is not member of the project');
$this->container['logger']->debug('Mailgun: ignored => user is not member of the project');
return false;
}

View File

@ -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);
}
/**

View File

@ -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);
}
}
}

View File

@ -30,7 +30,7 @@ class Webhook extends Base
$url .= '?token='.$token;
}
return $this->httpClient->post($url, $values);
return $this->httpClient->postJson($url, $values);
}
}
}

View File

@ -78,7 +78,7 @@ class ClassProvider implements ServiceProviderInterface
'GitlabWebhook',
'HipchatWebhook',
'Jabber',
'MailgunWebhook',
'Mailgun',
'Postmark',
'SendgridWebhook',
'SlackWebhook',

View File

@ -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);

View File

@ -1,6 +1,8 @@
<?php
// Rename this file to config.php if you want to change the values
/*******************************************************************/
/* Rename this file to config.php if you want to change the values */
/*******************************************************************/
// Enable/Disable debug
define('DEBUG', false);
@ -14,7 +16,7 @@ define('FILES_DIR', 'data/files/');
// E-mail address for the "From" header (notifications)
define('MAIL_FROM', 'notifications@kanboard.local');
// Mail transport to use: "smtp", "sendmail", "mail" (PHP mail function), "postmark"
// Mail transport available: "smtp", "sendmail", "mail" (PHP mail function), "postmark", "mailgun"
define('MAIL_TRANSPORT', 'mail');
// SMTP configuration to use when the "smtp" transport is chosen
@ -30,6 +32,12 @@ define('MAIL_SENDMAIL_COMMAND', '/usr/sbin/sendmail -bs');
// Postmark API token (used to send emails through their API)
define('POSTMARK_API_TOKEN', '');
// Mailgun API key (used to send emails through their API)
define('MAILGUN_API_TOKEN', '');
// Mailgun domain name
define('MAILGUN_DOMAIN', '');
// Database driver: sqlite, mysql or postgres (sqlite by default)
define('DB_DRIVER', 'sqlite');

View File

@ -20,6 +20,7 @@ There are several email transports available:
- SMTP
- Sendmail
- PHP native mail function
- Mailgun
- Postmark
Server settings
@ -75,6 +76,26 @@ This is the default configuration:
define('MAIL_TRANSPORT', 'mail');
```
### Mailgun HTTP API
You can use the HTTP API of Mailgun to send emails.
Configuration:
```php
// We choose "mailgun" as mail transport
define('MAIL_TRANSPORT', 'mailgun');
// Mailgun API key
define('MAILGUN_API_TOKEN', 'YOUR_API_KEY');
// Mailgun domain name
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');
```
### Postmark HTTP API
Postmark is a third-party email service.

View File

@ -53,7 +53,15 @@ class FakeHttpClient
return json_encode($this->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;

View File

@ -2,18 +2,38 @@
require_once __DIR__.'/Base.php';
use Integration\MailgunWebhook;
use Integration\Mailgun;
use Model\TaskCreation;
use Model\TaskFinder;
use Model\Project;
use Model\ProjectPermission;
use Model\User;
class MailgunWebhookTest extends Base
class MailgunTest extends Base
{
public function testSendEmail()
{
$pm = new Mailgun($this->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 <test@localhost>', $data['to']);
$this->assertEquals('Bob <notifications@kanboard.local>', $data['from']);
$this->assertEquals('Test', $data['subject']);
$this->assertEquals('Content', $data['html']);
}
public function testHandlePayload()
{
$w = new MailgunWebhook($this->container);
$w = new Mailgun($this->container);
$p = new Project($this->container);
$pp = new ProjectPermission($this->container);
$u = new User($this->container);
@ -26,20 +46,20 @@ class MailgunWebhookTest extends Base
$this->assertEquals(2, $p->create(array('name' => 'test2', 'identifier' => 'TEST1')));
// Empty payload
$this->assertFalse($w->parsePayload(array()));
$this->assertFalse($w->receiveEmail(array()));
// Unknown user
$this->assertFalse($w->parsePayload(array('sender' => 'a@b.c', 'subject' => 'Email task', 'recipient' => 'foobar', 'stripped-text' => 'boo')));
$this->assertFalse($w->receiveEmail(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')));
$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->parsePayload(array('sender' => 'me@localhost', 'subject' => 'Email task', 'recipient' => 'foo+test1@localhost', 'stripped-text' => 'boo')));
$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->parsePayload(array('sender' => 'me@localhost', 'subject' => 'Email task', 'recipient' => 'foo+test1@localhost', 'stripped-text' => 'boo')));
$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);