From 9d9e3afba2054bfa23ba6f019b7c8885c2d8415e Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 6 Jun 2015 14:10:31 -0400 Subject: Improve email sending system and add Postmark as mail transport --- app/Integration/Postmark.php | 97 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 app/Integration/Postmark.php (limited to 'app/Integration/Postmark.php') diff --git a/app/Integration/Postmark.php b/app/Integration/Postmark.php new file mode 100644 index 00000000..a367c23e --- /dev/null +++ b/app/Integration/Postmark.php @@ -0,0 +1,97 @@ + 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 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'])) { + $markdown = new HTML_To_Markdown($payload['HtmlBody'], array('strip_tags' => true)); + $description = $markdown->output(); + } + 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'], + )); + } +} -- cgit v1.2.3