Differential D12680 Diff 30538 src/applications/metamta/adapter/PhabricatorMailImplementationSendGridAdapter.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/metamta/adapter/PhabricatorMailImplementationSendGridAdapter.php
| Show First 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | final class PhabricatorMailImplementationSendGridAdapter | ||||
| public function send() { | public function send() { | ||||
| $user = PhabricatorEnv::getEnvConfig('sendgrid.api-user'); | $user = PhabricatorEnv::getEnvConfig('sendgrid.api-user'); | ||||
| $key = PhabricatorEnv::getEnvConfig('sendgrid.api-key'); | $key = PhabricatorEnv::getEnvConfig('sendgrid.api-key'); | ||||
| if (!$user || !$key) { | if (!$user || !$key) { | ||||
| throw new Exception( | throw new Exception( | ||||
| "Configure 'sendgrid.api-user' and 'sendgrid.api-key' to use ". | pht( | ||||
| "SendGrid for mail delivery."); | "Configure '%s' and '%s' to use SendGrid for mail delivery.", | ||||
| 'sendgrid.api-user', | |||||
| 'sendgrid.api-key')); | |||||
| } | } | ||||
| $params = array(); | $params = array(); | ||||
| $ii = 0; | $ii = 0; | ||||
| foreach (idx($this->params, 'tos', array()) as $to) { | foreach (idx($this->params, 'tos', array()) as $to) { | ||||
| $params['to['.($ii++).']'] = $to; | $params['to['.($ii++).']'] = $to; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | public function send() { | ||||
| $future = new HTTPSFuture( | $future = new HTTPSFuture( | ||||
| 'https://sendgrid.com/api/mail.send.json', | 'https://sendgrid.com/api/mail.send.json', | ||||
| $params); | $params); | ||||
| $future->setMethod('POST'); | $future->setMethod('POST'); | ||||
| list($body) = $future->resolvex(); | list($body) = $future->resolvex(); | ||||
| $response = json_decode($body, true); | $response = null; | ||||
| if (!is_array($response)) { | try { | ||||
| throw new Exception("Failed to JSON decode response: {$body}"); | $response = phutil_json_decode($body); | ||||
| } catch (PhutilJSONParserException $ex) { | |||||
| throw new PhutilProxyException( | |||||
| pht('Failed to JSON decode response.'), | |||||
| $ex); | |||||
| } | } | ||||
| if ($response['message'] !== 'success') { | if ($response['message'] !== 'success') { | ||||
| $errors = implode(';', $response['errors']); | $errors = implode(';', $response['errors']); | ||||
| throw new Exception("Request failed with errors: {$errors}."); | throw new Exception(pht('Request failed with errors: %s.', $errors)); | ||||
| } | } | ||||
| return true; | return true; | ||||
| } | } | ||||
| } | } | ||||