Add Sendgrid as mail transport

This commit is contained in:
Frederic Guillot
2015-06-13 13:17:16 -04:00
parent 7ba9b2d9b9
commit f2abf33912
9 changed files with 119 additions and 47 deletions

View File

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

View File

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

View File

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

View File

@@ -6,13 +6,39 @@ use HTML_To_Markdown;
use Core\Tool;
/**
* Sendgrid Webhook
* Sendgrid Integration
*
* @package integration
* @author Frederic Guillot
*/
class SendgridWebhook extends \Core\Base
class Sendgrid 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)
{
$payload = array(
'api_user' => 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
*
@@ -20,7 +46,7 @@ class SendgridWebhook extends \Core\Base
* @param array $payload Incoming email
* @return boolean
*/
public function parsePayload(array $payload)
public function receiveEmail(array $payload)
{
if (empty($payload['envelope']) || empty($payload['subject'])) {
return false;

View File

@@ -80,7 +80,7 @@ class ClassProvider implements ServiceProviderInterface
'Jabber',
'Mailgun',
'Postmark',
'SendgridWebhook',
'Sendgrid',
'SlackWebhook',
'Smtp',
)

View File

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