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:
@@ -11,7 +11,7 @@ use Pimple\Container;
|
||||
* @author Frederic Guillot
|
||||
*
|
||||
* @property \Kanboard\Core\Helper $helper
|
||||
* @property \Kanboard\Core\EmailClient $emailClient
|
||||
* @property \Kanboard\Core\Mail\Client $emailClient
|
||||
* @property \Kanboard\Core\HttpClient $httpClient
|
||||
* @property \Kanboard\Core\Paginator $paginator
|
||||
* @property \Kanboard\Core\Request $request
|
||||
@@ -29,11 +29,7 @@ use Pimple\Container;
|
||||
* @property \Kanboard\Integration\GitlabWebhook $gitlabWebhook
|
||||
* @property \Kanboard\Integration\HipchatWebhook $hipchatWebhook
|
||||
* @property \Kanboard\Integration\Jabber $jabber
|
||||
* @property \Kanboard\Integration\Mailgun $mailgun
|
||||
* @property \Kanboard\Integration\Postmark $postmark
|
||||
* @property \Kanboard\Integration\Sendgrid $sendgrid
|
||||
* @property \Kanboard\Integration\SlackWebhook $slackWebhook
|
||||
* @property \Kanboard\Integration\Smtp $smtp
|
||||
* @property \Kanboard\Formatter\ProjectGanttFormatter $projectGanttFormatter
|
||||
* @property \Kanboard\Formatter\TaskFilterGanttFormatter $taskFilterGanttFormatter
|
||||
* @property \Kanboard\Formatter\TaskFilterAutoCompleteFormatter $taskFilterAutoCompleteFormatter
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Core;
|
||||
|
||||
/**
|
||||
* Mail client
|
||||
*
|
||||
* @package core
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class EmailClient extends Base
|
||||
{
|
||||
/**
|
||||
* Send a HTML email
|
||||
*
|
||||
* @access public
|
||||
* @param string $email
|
||||
* @param string $name
|
||||
* @param string $subject
|
||||
* @param string $html
|
||||
*/
|
||||
public function send($email, $name, $subject, $html)
|
||||
{
|
||||
$this->container['logger']->debug('Sending email to '.$email.' ('.MAIL_TRANSPORT.')');
|
||||
|
||||
$start_time = microtime(true);
|
||||
$author = 'Kanboard';
|
||||
|
||||
if (Session::isOpen() && $this->userSession->isLogged()) {
|
||||
$author = e('%s via Kanboard', $this->user->getFullname($this->session['user']));
|
||||
}
|
||||
|
||||
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;
|
||||
case 'postmark':
|
||||
$this->postmark->sendEmail($email, $name, $subject, $html, $author);
|
||||
break;
|
||||
default:
|
||||
$this->smtp->sendEmail($email, $name, $subject, $html, $author);
|
||||
}
|
||||
|
||||
$this->container['logger']->debug('Email sent in '.round(microtime(true) - $start_time, 6).' seconds');
|
||||
}
|
||||
}
|
||||
96
app/Core/Mail/Client.php
Normal file
96
app/Core/Mail/Client.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Core\Mail;
|
||||
|
||||
use Pimple\Container;
|
||||
use Kanboard\Core\Base;
|
||||
|
||||
/**
|
||||
* Mail Client
|
||||
*
|
||||
* @package mail
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class Client extends Base
|
||||
{
|
||||
/**
|
||||
* Mail transport instances
|
||||
*
|
||||
* @access private
|
||||
* @var \Pimple\Container
|
||||
*/
|
||||
private $transports;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
* @param \Pimple\Container $container
|
||||
*/
|
||||
public function __construct(Container $container)
|
||||
{
|
||||
parent::__construct($container);
|
||||
$this->transports = new Container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a HTML email
|
||||
*
|
||||
* @access public
|
||||
* @param string $email
|
||||
* @param string $name
|
||||
* @param string $subject
|
||||
* @param string $html
|
||||
* @return EmailClient
|
||||
*/
|
||||
public function send($email, $name, $subject, $html)
|
||||
{
|
||||
$this->container['logger']->debug('Sending email to '.$email.' ('.MAIL_TRANSPORT.')');
|
||||
|
||||
$start_time = microtime(true);
|
||||
$author = 'Kanboard';
|
||||
|
||||
if ($this->userSession->isLogged()) {
|
||||
$author = e('%s via Kanboard', $this->user->getFullname($this->session['user']));
|
||||
}
|
||||
|
||||
$this->getTransport(MAIL_TRANSPORT)->sendEmail($email, $name, $subject, $html, $author);
|
||||
|
||||
if (DEBUG) {
|
||||
$this->logger->debug('Email sent in '.round(microtime(true) - $start_time, 6).' seconds');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mail transport instance
|
||||
*
|
||||
* @access public
|
||||
* @param string $transport
|
||||
* @return EmailClientInterface
|
||||
*/
|
||||
public function getTransport($transport)
|
||||
{
|
||||
return $this->transports[$transport];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new mail transport
|
||||
*
|
||||
* @access public
|
||||
* @param string $transport
|
||||
* @param string $class
|
||||
* @return EmailClient
|
||||
*/
|
||||
public function setTransport($transport, $class)
|
||||
{
|
||||
$container = $this->container;
|
||||
|
||||
$this->transports[$transport] = function() use ($class, $container) {
|
||||
return new $class($container);
|
||||
};
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
24
app/Core/Mail/ClientInterface.php
Normal file
24
app/Core/Mail/ClientInterface.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Core\Mail;
|
||||
|
||||
/**
|
||||
* Mail Client Interface
|
||||
*
|
||||
* @package mail
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
interface ClientInterface
|
||||
{
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
57
app/Core/Mail/Transport/Mail.php
Normal file
57
app/Core/Mail/Transport/Mail.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Core\Mail\Transport;
|
||||
|
||||
use Swift_Message;
|
||||
use Swift_Mailer;
|
||||
use Swift_MailTransport;
|
||||
use Swift_TransportException;
|
||||
use Kanboard\Core\Base;
|
||||
use Kanboard\Core\Mail\ClientInterface;
|
||||
|
||||
/**
|
||||
* PHP Mail Handler
|
||||
*
|
||||
* @package transport
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class Mail extends Base implements ClientInterface
|
||||
{
|
||||
/**
|
||||
* 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->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SwiftMailer transport
|
||||
*
|
||||
* @access protected
|
||||
* @return \Swift_Transport
|
||||
*/
|
||||
protected function getTransport()
|
||||
{
|
||||
return Swift_MailTransport::newInstance();
|
||||
}
|
||||
}
|
||||
25
app/Core/Mail/Transport/Sendmail.php
Normal file
25
app/Core/Mail/Transport/Sendmail.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Core\Mail\Transport;
|
||||
|
||||
use Swift_SendmailTransport;
|
||||
|
||||
/**
|
||||
* PHP Mail Handler
|
||||
*
|
||||
* @package transport
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class Sendmail extends Mail
|
||||
{
|
||||
/**
|
||||
* Get SwiftMailer transport
|
||||
*
|
||||
* @access protected
|
||||
* @return \Swift_Transport
|
||||
*/
|
||||
protected function getTransport()
|
||||
{
|
||||
return Swift_SendmailTransport::newInstance(MAIL_SENDMAIL_COMMAND);
|
||||
}
|
||||
}
|
||||
30
app/Core/Mail/Transport/Smtp.php
Normal file
30
app/Core/Mail/Transport/Smtp.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Core\Mail\Transport;
|
||||
|
||||
use Swift_SmtpTransport;
|
||||
|
||||
/**
|
||||
* PHP Mail Handler
|
||||
*
|
||||
* @package transport
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class Smtp extends Mail
|
||||
{
|
||||
/**
|
||||
* Get SwiftMailer transport
|
||||
*
|
||||
* @access protected
|
||||
* @return \Swift_Transport
|
||||
*/
|
||||
protected function getTransport()
|
||||
{
|
||||
$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);
|
||||
|
||||
return $transport;
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,7 @@ abstract class Base extends \Kanboard\Core\Base
|
||||
*/
|
||||
public function getPluginName()
|
||||
{
|
||||
return ucfirst(substr(get_called_class(), 7, -7));
|
||||
return ucfirst(substr(get_called_class(), 16, -7));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user