Path: blob/master/src/applications/metamta/controller/PhabricatorMetaMTASendGridReceiveController.php
12262 views
<?php12final class PhabricatorMetaMTASendGridReceiveController3extends PhabricatorMetaMTAController {45public function shouldRequireLogin() {6return false;7}89public function handleRequest(AphrontRequest $request) {10// SendGrid doesn't sign payloads so we can't be sure that SendGrid11// actually sent this request, but require a configured SendGrid mailer12// before we activate this endpoint.13$mailers = PhabricatorMetaMTAMail::newMailers(14array(15'inbound' => true,16'types' => array(17PhabricatorMailSendGridAdapter::ADAPTERTYPE,18),19));20if (!$mailers) {21return new Aphront404Response();22}2324// No CSRF for SendGrid.25$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();26$user = $request->getUser();2728$raw_headers = $request->getStr('headers');29$raw_headers = explode("\n", rtrim($raw_headers));30$raw_dict = array();31foreach (array_filter($raw_headers) as $header) {32list($name, $value) = explode(':', $header, 2);33$raw_dict[$name] = ltrim($value);34}3536$headers = array(37'to' => $request->getStr('to'),38'from' => $request->getStr('from'),39'subject' => $request->getStr('subject'),40) + $raw_dict;4142$received = new PhabricatorMetaMTAReceivedMail();43$received->setHeaders($headers);44$received->setBodies(array(45'text' => $request->getStr('text'),46'html' => $request->getStr('from'),47));4849$file_phids = array();50foreach ($_FILES as $file_raw) {51try {52$file = PhabricatorFile::newFromPHPUpload(53$file_raw,54array(55'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,56));57$file_phids[] = $file->getPHID();58} catch (Exception $ex) {59phlog($ex);60}61}62$received->setAttachments($file_phids);63$received->save();6465$received->processReceivedMail();6667$response = new AphrontWebpageResponse();68$response->setContent(pht('Got it! Thanks, SendGrid!')."\n");69return $response;70}7172}737475