Make mail transports pluggable and move integrations to plugins
- Postmark: https://github.com/kanboard/plugin-postmark - Mailgun: https://github.com/kanboard/plugin-mailgun - Sendgrid: https://github.com/kanboard/plugin-sendgrid
This commit is contained in:
@@ -1,95 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Integration;
|
||||
|
||||
use Kanboard\Core\Tool;
|
||||
|
||||
/**
|
||||
* Mailgun Integration
|
||||
*
|
||||
* @package integration
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class Mailgun extends \Kanboard\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
|
||||
*
|
||||
* @access public
|
||||
* @param array $payload Incoming email
|
||||
* @return boolean
|
||||
*/
|
||||
public function receiveEmail(array $payload)
|
||||
{
|
||||
if (empty($payload['sender']) || empty($payload['subject']) || empty($payload['recipient'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The user must exists in Kanboard
|
||||
$user = $this->user->getByEmail($payload['sender']);
|
||||
|
||||
if (empty($user)) {
|
||||
$this->container['logger']->debug('Mailgun: ignored => user not found');
|
||||
return false;
|
||||
}
|
||||
|
||||
// The project must have a short name
|
||||
$project = $this->project->getByIdentifier(Tool::getMailboxHash($payload['recipient']));
|
||||
|
||||
if (empty($project)) {
|
||||
$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('Mailgun: ignored => user is not member of the project');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the Markdown contents
|
||||
if (! empty($payload['stripped-html'])) {
|
||||
$description = $this->htmlConverter->convert($payload['stripped-html']);
|
||||
}
|
||||
else if (! empty($payload['stripped-text'])) {
|
||||
$description = $payload['stripped-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'],
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Integration;
|
||||
|
||||
/**
|
||||
* Postmark integration
|
||||
*
|
||||
* @package integration
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class Postmark extends \Kanboard\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(
|
||||
'Accept: application/json',
|
||||
'X-Postmark-Server-Token: '.POSTMARK_API_TOKEN,
|
||||
);
|
||||
|
||||
$payload = array(
|
||||
'From' => sprintf('%s <%s>', $author, MAIL_FROM),
|
||||
'To' => sprintf('%s <%s>', $name, $email),
|
||||
'Subject' => $subject,
|
||||
'HtmlBody' => $html,
|
||||
);
|
||||
|
||||
$this->httpClient->postJson('https://api.postmarkapp.com/email', $payload, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse incoming email
|
||||
*
|
||||
* @access public
|
||||
* @param array $payload Incoming email
|
||||
* @return boolean
|
||||
*/
|
||||
public function receiveEmail(array $payload)
|
||||
{
|
||||
if (empty($payload['From']) || empty($payload['Subject']) || empty($payload['MailboxHash'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The user must exists in Kanboard
|
||||
$user = $this->user->getByEmail($payload['From']);
|
||||
|
||||
if (empty($user)) {
|
||||
$this->container['logger']->debug('Postmark: ignored => user not found');
|
||||
return false;
|
||||
}
|
||||
|
||||
// The project must have a short name
|
||||
$project = $this->project->getByIdentifier($payload['MailboxHash']);
|
||||
|
||||
if (empty($project)) {
|
||||
$this->container['logger']->debug('Postmark: 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('Postmark: ignored => user is not member of the project');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the Markdown contents
|
||||
if (! empty($payload['HtmlBody'])) {
|
||||
$description = $this->htmlConverter->convert($payload['HtmlBody']);
|
||||
}
|
||||
else if (! empty($payload['TextBody'])) {
|
||||
$description = $payload['TextBody'];
|
||||
}
|
||||
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'],
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Integration;
|
||||
|
||||
use Kanboard\Core\Tool;
|
||||
|
||||
/**
|
||||
* Sendgrid Integration
|
||||
*
|
||||
* @package integration
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class Sendgrid extends \Kanboard\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
|
||||
*
|
||||
* @access public
|
||||
* @param array $payload Incoming email
|
||||
* @return boolean
|
||||
*/
|
||||
public function receiveEmail(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'])) {
|
||||
$description = $this->htmlConverter->convert($payload['html']);
|
||||
}
|
||||
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'],
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Integration;
|
||||
|
||||
use Swift_Message;
|
||||
use Swift_Mailer;
|
||||
use Swift_MailTransport;
|
||||
use Swift_SendmailTransport;
|
||||
use Swift_SmtpTransport;
|
||||
use Swift_TransportException;
|
||||
|
||||
/**
|
||||
* Smtp
|
||||
*
|
||||
* @package integration
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class Smtp extends \Kanboard\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)
|
||||
{
|
||||
try {
|
||||
|
||||
$message = Swift_Message::newInstance()
|
||||
->setSubject($subject)
|
||||
->setFrom(array(MAIL_FROM => $author))
|
||||
->setBody($html, 'text/html')
|
||||
->setTo(array($email => $name));
|
||||
|
||||
Swift_Mailer::newInstance($this->getTransport())->send($message);
|
||||
}
|
||||
catch (Swift_TransportException $e) {
|
||||
$this->container['logger']->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SwiftMailer transport
|
||||
*
|
||||
* @access private
|
||||
* @return \Swift_Transport
|
||||
*/
|
||||
private function getTransport()
|
||||
{
|
||||
switch (MAIL_TRANSPORT) {
|
||||
case 'smtp':
|
||||
$transport = Swift_SmtpTransport::newInstance(MAIL_SMTP_HOSTNAME, MAIL_SMTP_PORT);
|
||||
$transport->setUsername(MAIL_SMTP_USERNAME);
|
||||
$transport->setPassword(MAIL_SMTP_PASSWORD);
|
||||
$transport->setEncryption(MAIL_SMTP_ENCRYPTION);
|
||||
break;
|
||||
case 'sendmail':
|
||||
$transport = Swift_SendmailTransport::newInstance(MAIL_SENDMAIL_COMMAND);
|
||||
break;
|
||||
default:
|
||||
$transport = Swift_MailTransport::newInstance();
|
||||
}
|
||||
|
||||
return $transport;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user