Skip to content

Commit ee81492

Browse files
author
epriestley
committedApr 6, 2016
Improve Amazon SES code error handling behavior
Summary: Fixes T10728. Fixes T10476. SES uses third-party code with unique, creative ideas about error handling. - Make the error handling behavior more correct, so it doesn't try to use undefined variables. - Simplify the error handling behavior (throw exceptions sooner, remove redundant code). - Explicitly test for `-smtp` misconfigurations. These can arise if you read the wrong column out of the table in the AWS docs, as in T10728. - Explicitly test for SimpleXML, to catch T10476 before it does damage. Test Plan: - Configured SES to use a bogus SMTP endpoint. - Faked past the SMTP check, hit sane error on the connection. - Undid faking, hit immediate hard stop on the STMP check. Reviewers: chad Reviewed By: chad Maniphest Tasks: T10476, T10728 Differential Revision: https://secure.phabricator.com/D15632
1 parent 9518a1a commit ee81492

File tree

1 file changed

+37
-57
lines changed

1 file changed

+37
-57
lines changed
 

‎externals/amazon-ses/ses.php

+37-57
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,30 @@ public function enableUseExceptions($enable = true) { $this->__useExceptions = $
8080
* @return void
8181
*/
8282
public function __construct($accessKey = null, $secretKey = null, $host = 'email.us-east-1.amazonaws.com') {
83+
if (!function_exists('simplexml_load_string')) {
84+
throw new Exception(
85+
pht(
86+
'The PHP SimpleXML extension is not available, but this '.
87+
'extension is required to send mail via Amazon SES, because '.
88+
'Amazon SES returns API responses in XML format. Install or '.
89+
'enable the SimpleXML extension.'));
90+
}
91+
92+
// Catch mistakes with reading the wrong column out of the SES
93+
// documentation. See T10728.
94+
if (preg_match('(-smtp)', $host)) {
95+
throw new Exception(
96+
pht(
97+
'Amazon SES is not configured correctly: the configured SES '.
98+
'endpoint ("%s") is an SMTP endpoint. Instead, use an API (HTTPS) '.
99+
'endpoint.',
100+
$host));
101+
}
102+
83103
if ($accessKey !== null && $secretKey !== null) {
84104
$this->setAuth($accessKey, $secretKey);
85105
}
106+
86107
$this->__host = $host;
87108
}
88109

@@ -108,13 +129,6 @@ public function listVerifiedEmailAddresses() {
108129
$rest->setParameter('Action', 'ListVerifiedEmailAddresses');
109130

110131
$rest = $rest->getResponse();
111-
if($rest->error === false && $rest->code !== 200) {
112-
$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
113-
}
114-
if($rest->error !== false) {
115-
$this->__triggerError('listVerifiedEmailAddresses', $rest->error);
116-
return false;
117-
}
118132

119133
$response = array();
120134
if(!isset($rest->body)) {
@@ -148,13 +162,6 @@ public function verifyEmailAddress($email) {
148162
$rest->setParameter('EmailAddress', $email);
149163

150164
$rest = $rest->getResponse();
151-
if($rest->error === false && $rest->code !== 200) {
152-
$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
153-
}
154-
if($rest->error !== false) {
155-
$this->__triggerError('verifyEmailAddress', $rest->error);
156-
return false;
157-
}
158165

159166
$response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId;
160167
return $response;
@@ -172,13 +179,6 @@ public function deleteVerifiedEmailAddress($email) {
172179
$rest->setParameter('EmailAddress', $email);
173180

174181
$rest = $rest->getResponse();
175-
if($rest->error === false && $rest->code !== 200) {
176-
$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
177-
}
178-
if($rest->error !== false) {
179-
$this->__triggerError('deleteVerifiedEmailAddress', $rest->error);
180-
return false;
181-
}
182182

183183
$response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId;
184184
return $response;
@@ -195,13 +195,6 @@ public function getSendQuota() {
195195
$rest->setParameter('Action', 'GetSendQuota');
196196

197197
$rest = $rest->getResponse();
198-
if($rest->error === false && $rest->code !== 200) {
199-
$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
200-
}
201-
if($rest->error !== false) {
202-
$this->__triggerError('getSendQuota', $rest->error);
203-
return false;
204-
}
205198

206199
$response = array();
207200
if(!isset($rest->body)) {
@@ -227,13 +220,6 @@ public function getSendStatistics() {
227220
$rest->setParameter('Action', 'GetSendStatistics');
228221

229222
$rest = $rest->getResponse();
230-
if($rest->error === false && $rest->code !== 200) {
231-
$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
232-
}
233-
if($rest->error !== false) {
234-
$this->__triggerError('getSendStatistics', $rest->error);
235-
return false;
236-
}
237223

238224
$response = array();
239225
if(!isset($rest->body)) {
@@ -265,13 +251,6 @@ public function sendRawEmail($raw) {
265251
$rest->setParameter('RawMessage.Data', base64_encode($raw));
266252

267253
$rest = $rest->getResponse();
268-
if($rest->error === false && $rest->code !== 200) {
269-
$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
270-
}
271-
if($rest->error !== false) {
272-
$this->__triggerError('sendRawEmail', $rest->error);
273-
return false;
274-
}
275254

276255
$response['MessageId'] = (string)$rest->body->SendEmailResult->MessageId;
277256
$response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId;
@@ -351,13 +330,6 @@ public function sendEmail($sesMessage) {
351330
}
352331

353332
$rest = $rest->getResponse();
354-
if($rest->error === false && $rest->code !== 200) {
355-
$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
356-
}
357-
if($rest->error !== false) {
358-
$this->__triggerError('sendEmail', $rest->error);
359-
return false;
360-
}
361333

362334
$response['MessageId'] = (string)$rest->body->SendEmailResult->MessageId;
363335
$response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId;
@@ -523,14 +495,22 @@ public function getResponse() {
523495
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
524496

525497
// Execute, grab errors
526-
if (curl_exec($curl)) {
527-
$this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
528-
} else {
529-
$this->response->error = array(
530-
'curl' => true,
531-
'code' => curl_errno($curl),
532-
'message' => curl_error($curl),
533-
);
498+
if (!curl_exec($curl)) {
499+
throw new SimpleEmailServiceException(
500+
pht(
501+
'Encountered an error while making an HTTP request to Amazon SES '.
502+
'(cURL Error #%d): %s',
503+
curl_errno($curl),
504+
curl_error($curl)));
505+
}
506+
507+
$this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
508+
if ($this->response->code != 200) {
509+
throw new SimpleEmailServiceException(
510+
pht(
511+
'Unexpected HTTP status while making request to Amazon SES: '.
512+
'expected 200, got %s.',
513+
$this->response->code));
534514
}
535515

536516
@curl_close($curl);

0 commit comments

Comments
 (0)
Failed to load comments.