diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-06-06 14:10:31 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-06-06 14:10:31 -0400 |
commit | 9d9e3afba2054bfa23ba6f019b7c8885c2d8415e (patch) | |
tree | 1113e318c3d5e15bed48480f90f5498347343783 /app/Integration | |
parent | c87e1fbc33416e7b2f59b98c2ffed173958b3146 (diff) |
Improve email sending system and add Postmark as mail transport
Diffstat (limited to 'app/Integration')
-rw-r--r-- | app/Integration/Postmark.php (renamed from app/Integration/PostmarkWebhook.php) | 39 | ||||
-rw-r--r-- | app/Integration/Smtp.php | 71 |
2 files changed, 104 insertions, 6 deletions
diff --git a/app/Integration/PostmarkWebhook.php b/app/Integration/Postmark.php index 9051e5f7..a367c23e 100644 --- a/app/Integration/PostmarkWebhook.php +++ b/app/Integration/Postmark.php @@ -5,21 +5,48 @@ namespace Integration; use HTML_To_Markdown; /** - * Postmark Webhook + * Postmark integration * * @package integration * @author Frederic Guillot */ -class PostmarkWebhook extends \Core\Base +class Postmark 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( + '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->post('https://api.postmarkapp.com/email', $payload, $headers); + } + + /** * Parse incoming email * * @access public * @param array $payload Incoming email * @return boolean */ - public function parsePayload(array $payload) + public function receiveEmail(array $payload) { if (empty($payload['From']) || empty($payload['Subject']) || empty($payload['MailboxHash'])) { return false; @@ -29,7 +56,7 @@ class PostmarkWebhook extends \Core\Base $user = $this->user->getByEmail($payload['From']); if (empty($user)) { - $this->container['logger']->debug('PostmarkWebhook: ignored => user not found'); + $this->container['logger']->debug('Postmark: ignored => user not found'); return false; } @@ -37,13 +64,13 @@ class PostmarkWebhook extends \Core\Base $project = $this->project->getByIdentifier($payload['MailboxHash']); if (empty($project)) { - $this->container['logger']->debug('PostmarkWebhook: ignored => project not found'); + $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('PostmarkWebhook: ignored => user is not member of the project'); + $this->container['logger']->debug('Postmark: ignored => user is not member of the project'); return false; } diff --git a/app/Integration/Smtp.php b/app/Integration/Smtp.php new file mode 100644 index 00000000..ad2f30f8 --- /dev/null +++ b/app/Integration/Smtp.php @@ -0,0 +1,71 @@ +<?php + +namespace 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 \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; + } +} |