From f99a3c501fd6ed7b4914b8d6e855489c2ce5b219 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Fri, 16 Oct 2015 20:50:12 -0400 Subject: 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 --- app/Core/Mail/Client.php | 96 ++++++++++++++++++++++++++++++++++++ app/Core/Mail/ClientInterface.php | 24 +++++++++ app/Core/Mail/Transport/Mail.php | 57 +++++++++++++++++++++ app/Core/Mail/Transport/Sendmail.php | 25 ++++++++++ app/Core/Mail/Transport/Smtp.php | 30 +++++++++++ 5 files changed, 232 insertions(+) create mode 100644 app/Core/Mail/Client.php create mode 100644 app/Core/Mail/ClientInterface.php create mode 100644 app/Core/Mail/Transport/Mail.php create mode 100644 app/Core/Mail/Transport/Sendmail.php create mode 100644 app/Core/Mail/Transport/Smtp.php (limited to 'app/Core/Mail') diff --git a/app/Core/Mail/Client.php b/app/Core/Mail/Client.php new file mode 100644 index 00000000..c7cfaab9 --- /dev/null +++ b/app/Core/Mail/Client.php @@ -0,0 +1,96 @@ +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; + } +} diff --git a/app/Core/Mail/ClientInterface.php b/app/Core/Mail/ClientInterface.php new file mode 100644 index 00000000..66263a98 --- /dev/null +++ b/app/Core/Mail/ClientInterface.php @@ -0,0 +1,24 @@ +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(); + } +} diff --git a/app/Core/Mail/Transport/Sendmail.php b/app/Core/Mail/Transport/Sendmail.php new file mode 100644 index 00000000..849e3385 --- /dev/null +++ b/app/Core/Mail/Transport/Sendmail.php @@ -0,0 +1,25 @@ +setUsername(MAIL_SMTP_USERNAME); + $transport->setPassword(MAIL_SMTP_PASSWORD); + $transport->setEncryption(MAIL_SMTP_ENCRYPTION); + + return $transport; + } +} -- cgit v1.2.3