Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/metamta/controller/PhabricatorMetaMTASendGridReceiveController.php
12262 views
1
<?php
2
3
final class PhabricatorMetaMTASendGridReceiveController
4
extends PhabricatorMetaMTAController {
5
6
public function shouldRequireLogin() {
7
return false;
8
}
9
10
public function handleRequest(AphrontRequest $request) {
11
// SendGrid doesn't sign payloads so we can't be sure that SendGrid
12
// actually sent this request, but require a configured SendGrid mailer
13
// before we activate this endpoint.
14
$mailers = PhabricatorMetaMTAMail::newMailers(
15
array(
16
'inbound' => true,
17
'types' => array(
18
PhabricatorMailSendGridAdapter::ADAPTERTYPE,
19
),
20
));
21
if (!$mailers) {
22
return new Aphront404Response();
23
}
24
25
// No CSRF for SendGrid.
26
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
27
$user = $request->getUser();
28
29
$raw_headers = $request->getStr('headers');
30
$raw_headers = explode("\n", rtrim($raw_headers));
31
$raw_dict = array();
32
foreach (array_filter($raw_headers) as $header) {
33
list($name, $value) = explode(':', $header, 2);
34
$raw_dict[$name] = ltrim($value);
35
}
36
37
$headers = array(
38
'to' => $request->getStr('to'),
39
'from' => $request->getStr('from'),
40
'subject' => $request->getStr('subject'),
41
) + $raw_dict;
42
43
$received = new PhabricatorMetaMTAReceivedMail();
44
$received->setHeaders($headers);
45
$received->setBodies(array(
46
'text' => $request->getStr('text'),
47
'html' => $request->getStr('from'),
48
));
49
50
$file_phids = array();
51
foreach ($_FILES as $file_raw) {
52
try {
53
$file = PhabricatorFile::newFromPHPUpload(
54
$file_raw,
55
array(
56
'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,
57
));
58
$file_phids[] = $file->getPHID();
59
} catch (Exception $ex) {
60
phlog($ex);
61
}
62
}
63
$received->setAttachments($file_phids);
64
$received->save();
65
66
$received->processReceivedMail();
67
68
$response = new AphrontWebpageResponse();
69
$response->setContent(pht('Got it! Thanks, SendGrid!')."\n");
70
return $response;
71
}
72
73
}
74
75