This repository has been archived by the owner on Apr 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
EmailHandler.php
205 lines (175 loc) · 5.78 KB
/
EmailHandler.php
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace Kanboard\Plugin\Sendgrid;
require_once __DIR__.'/vendor/autoload.php';
use Exception;
use Kanboard\Core\Base;
use Kanboard\Core\Mail\ClientInterface;
use League\HTMLToMarkdown\HtmlConverter;
/**
* Sendgrid Mail Handler
*
* @package sendgrid
* @author Frederic Guillot
*/
class EmailHandler extends Base implements ClientInterface
{
const API_URL = 'https://api.sendgrid.com/v3/mail/send';
/**
* Get API token
*
* @access public
* @return string
*/
public function getApiKey()
{
if (defined('SENDGRID_API_KEY')) {
$key = SENDGRID_API_KEY;
} else {
$key = $this->configModel->get('sendgrid_api_key');
}
return trim($key);
}
/**
* Send a HTML email
*
* @access public
* @param string $recipientEmail
* @param string $recipientName
* @param string $subject
* @param string $html
* @param string $authorName
* @param string $authorEmail
*/
public function sendEmail($recipientEmail, $recipientName, $subject, $html, $authorName, $authorEmail = '')
{
$headers = array(
'Authorization: Bearer '.$this->getApiKey(),
);
$payload = array(
'from' => array(
'email' => $this->helper->mail->getMailSenderAddress(),
'name' => $authorName,
),
'personalizations' => array(
array(
'to' => array(
array(
'email' => $recipientEmail,
'name' => $recipientName,
)
),
)
),
'subject' => $subject,
'content' => array(
array(
'type' => 'text/html',
'value' => $html,
)
),
);
if (! empty($authorEmail)) {
$payload['reply_to'] = array(
'email' => $authorEmail
);
}
$this->httpClient->postJsonAsync(self::API_URL, $payload, $headers);
}
/**
* Parse incoming email
*
* @access public
* @param array $payload Incoming email
* @return boolean
*/
public function receiveEmail(array $payload)
{
if (empty($payload['envelope']) || empty($payload['subject'])) {
return false;
}
$envelope = json_decode($payload['envelope'], true);
$recipient = isset($envelope['to'][0]) ? $envelope['to'][0] : '';
// The user must exists in Kanboard
$user = $this->userModel->getByEmail($envelope['from']);
if (empty($user)) {
$this->logger->debug(__METHOD__.': Ignored => user not found: '.$envelope['from']);
return false;
}
// The project must have a short name
$project = $this->projectModel->getByEmail($recipient);
if (empty($project)) {
$this->logger->debug(__METHOD__.': Ignored => project not found: '.$recipient);
return false;
}
// The user must be member of the project
if (! $this->projectPermissionModel->isAssignable($project['id'], $user['id'])) {
$this->logger->debug(__METHOD__.': Ignored => user is not member of the project');
return false;
}
// Finally, we create the task
$taskId = $this->taskCreationModel->create(array(
'project_id' => $project['id'],
'title' => $this->helper->mail->filterSubject($payload['subject']),
'description' => $this->getTaskDescription($payload),
'creator_id' => $user['id'],
'swimlane_id' => $this->getSwimlaneId($project),
));
if ($taskId > 0) {
$this->addEmailBodyAsAttachment($taskId, $payload);
$this->uploadAttachments($taskId, $payload);
return true;
}
return false;
}
protected function getSwimlaneId(array $project)
{
$swimlane = $this->swimlaneModel->getFirstActiveSwimlane($project['id']);
return empty($swimlane) ? 0 : $swimlane['id'];
}
protected function getTaskDescription(array $payload)
{
if (! empty($payload['html'])) {
$htmlConverter = new HtmlConverter(array(
'strip_tags' => true,
'remove_nodes' => 'meta script style link img span',
));
return $htmlConverter->convert($payload['html']);
} elseif (! empty($payload['text'])) {
return $payload['text'];
}
return '';
}
protected function addEmailBodyAsAttachment($taskId, array $payload)
{
$filename = t('Email') . '.txt';
$data = '';
if (! empty($payload['html'])) {
$data = $payload['html'];
$filename = t('Email') . '.html';
} elseif (! empty($payload['text'])) {
$data = $payload['text'];
}
if (! empty($data)) {
$this->taskFileModel->uploadContent($taskId, $filename, $data, false);
}
}
protected function uploadAttachments($taskId, array $payload)
{
if (isset($payload['attachments']) && $payload['attachments'] > 0) {
for ($i = 1; $i <= $payload['attachments']; $i++) {
$this->uploadAttachment($taskId, 'attachment' . $i);
}
}
}
protected function uploadAttachment($taskId, $name)
{
$fileInfo = $this->request->getFileInfo($name);
if (! empty($fileInfo)) {
try {
$this->taskFileModel->uploadFile($taskId, $fileInfo);
} catch (Exception $e) {
$this->logger->error($e->getMessage());
}
}
}
}