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 7
/
Copy pathEmailHandler.php
245 lines (211 loc) · 6.75 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
namespace Kanboard\Plugin\Mailgun;
require_once __DIR__.'/vendor/autoload.php';
use Exception;
use Kanboard\Core\Base;
use Kanboard\Core\Mail\ClientInterface;
use League\HTMLToMarkdown\HtmlConverter;
/**
* Mailgun Mail Handler
*
* @package mailgun
* @author Frederic Guillot
*/
class EmailHandler extends Base implements ClientInterface
{
/**
* 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: Basic '.base64_encode('api:'.$this->getApiToken())
);
$payload = array(
'from' => sprintf('%s <%s>', $authorName, $this->helper->mail->getMailSenderAddress()),
'to' => sprintf('%s <%s>', $recipientName, $recipientEmail),
'subject' => $subject,
'html' => $html,
);
if (! empty($authorEmail)) {
$payload['h:Reply-To'] = $authorEmail;
}
$this->httpClient->postFormAsync('https://api.mailgun.net/v3/'.$this->getDomain().'/messages', $payload, $headers);
}
/**
* Parse incoming email
*
* @access public
* @param array $payload Incoming email
* @return boolean
*/
public function receiveEmail(array $payload)
{
$result = $this->validate($payload);
if ($result === false) {
return false;
}
list($user, $project) = $result;
$taskId = $this->taskCreationModel->create(array(
'project_id' => $project['id'],
'title' => $this->getTitle($payload),
'description' => trim($this->getDescription($payload)),
'creator_id' => $user['id'],
'owner_id' => $user['id'],
'swimlane_id' => $this->getSwimlaneId($project),
));
if ($taskId > 0) {
$this->addEmailBodyAsAttachment($taskId, $payload);
$this->uploadAttachments($taskId, $payload);
return true;
}
return false;
}
/**
* Validate incoming email
*
* @access public
* @param array $payload
* @return array|boolean
*/
public function validate(array $payload)
{
if (empty($payload['sender']) || empty($payload['subject']) || empty($payload['recipient'])) {
return false;
}
// The project must have a short name
$project = $this->projectModel->getByEmail($payload['recipient']);
if (empty($project)) {
$this->logger->info('Mailgun: ignored => project not found');
return false;
}
// The user must exists in Kanboard
$user = $this->userModel->getByEmail($payload['sender']);
// Check to see if a catchall user was specified - if the original sender is unrecognized
if (empty($user)) {
$catchAllAddress = $this->projectMetadataModel->get($project['id'], 'mailgun_catch_all');
$user = $this->userModel->getByEmail($catchAllAddress);
$this->logger->info('Mailgun: unknown user mapped to ' . $user['name'] . ' (' . $user['email'] . ') in project ' . $project['name']);
}
if (empty($user)) {
$this->logger->info('Mailgun: ignored => user not found');
return false;
}
// The user must be member of the project
if (! $this->projectPermissionModel->isAssignable($project['id'], $user['id'])) {
$this->logger->info('Mailgun: ignored => user is not member of the project');
return false;
}
return array($user, $project);
}
/**
* Get task title
*
* @access public
* @param array $payload
* @return string
*/
public function getTitle(array $payload)
{
return $this->helper->mail->filterSubject($payload['subject']);
}
/**
* Get Markdown content for the task
*
* @access public
* @param array $payload
* @return string
*/
public function getDescription(array $payload)
{
if (! empty($payload['stripped-html'])) {
$htmlConverter = new HtmlConverter(array(
'strip_tags' => true,
'remove_nodes' => 'meta script style link img span',
));
return $htmlConverter->convert($payload['stripped-html']);
} elseif (! empty($payload['body-plain'])) {
return $payload['body-plain'];
}
return '';
}
/**
* Get swimlane id
*
* @access public
* @param array $project
* @return string
*/
public function getSwimlaneId(array $project)
{
$swimlane = $this->swimlaneModel->getFirstActiveSwimlane($project['id']);
return empty($swimlane) ? 0 : $swimlane['id'];
}
/**
* Get API token
*
* @access public
* @return string
*/
public function getApiToken()
{
if (defined('MAILGUN_API_TOKEN')) {
return MAILGUN_API_TOKEN;
}
return $this->configModel->get('mailgun_api_token');
}
/**
* Get Mailgun domain
*
* @access public
* @return string
*/
public function getDomain()
{
if (defined('MAILGUN_DOMAIN')) {
return MAILGUN_DOMAIN;
}
return $this->configModel->get('mailgun_domain');
}
protected function uploadAttachments($taskId, array $payload)
{
if (isset($payload['attachment-count']) && $payload['attachment-count'] > 0) {
for ($i = 1; $i <= $payload['attachment-count']; $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());
}
}
}
protected function addEmailBodyAsAttachment($taskId, array $payload)
{
$filename = t('Email') . '.txt';
$data = '';
if (! empty($payload['body-html'])) {
$data = $payload['body-html'];
$filename = t('Email') . '.html';
} elseif (! empty($payload['body-plain'])) {
$data = $payload['body-plain'];
}
if (! empty($data)) {
$this->taskFileModel->uploadContent($taskId, $filename, $data, false);
}
}
}