summaryrefslogtreecommitdiff
path: root/app/Integration/Postmark.php
blob: dbb70aeed2022a5b0121bf73c6e9514d27719a90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

namespace Integration;

use HTML_To_Markdown;

/**
 * Postmark integration
 *
 * @package  integration
 * @author   Frederic Guillot
 */
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->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'])) {
            $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'],
        ));
    }
}