Changeset View
Changeset View
Standalone View
Standalone View
externals/amazon-ses/ses.php
| Show First 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | class SimpleEmailService | ||||
| /** | /** | ||||
| * Constructor | * Constructor | ||||
| * | * | ||||
| * @param string $accessKey Access key | * @param string $accessKey Access key | ||||
| * @param string $secretKey Secret key | * @param string $secretKey Secret key | ||||
| * @return void | * @return void | ||||
| */ | */ | ||||
| public function __construct($accessKey = null, $secretKey = null, $host = 'email.us-east-1.amazonaws.com') { | public function __construct($accessKey = null, $secretKey = null, $host = 'email.us-east-1.amazonaws.com') { | ||||
| if (!function_exists('simplexml_load_string')) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'The PHP SimpleXML extension is not available, but this '. | |||||
| 'extension is required to send mail via Amazon SES, because '. | |||||
| 'Amazon SES returns API responses in XML format. Install or '. | |||||
| 'enable the SimpleXML extension.')); | |||||
| } | |||||
| // Catch mistakes with reading the wrong column out of the SES | |||||
| // documentation. See T10728. | |||||
| if (preg_match('(-smtp)', $host)) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Amazon SES is not configured correctly: the configured SES '. | |||||
| 'endpoint ("%s") is an SMTP endpoint. Instead, use an API (HTTPS) '. | |||||
| 'endpoint.', | |||||
| $host)); | |||||
| } | |||||
| if ($accessKey !== null && $secretKey !== null) { | if ($accessKey !== null && $secretKey !== null) { | ||||
| $this->setAuth($accessKey, $secretKey); | $this->setAuth($accessKey, $secretKey); | ||||
| } | } | ||||
| $this->__host = $host; | $this->__host = $host; | ||||
| } | } | ||||
| /** | /** | ||||
| * Set AWS access key and secret key | * Set AWS access key and secret key | ||||
| * | * | ||||
| * @param string $accessKey Access key | * @param string $accessKey Access key | ||||
| * @param string $secretKey Secret key | * @param string $secretKey Secret key | ||||
| Show All 9 Lines | class SimpleEmailService | ||||
| * | * | ||||
| * @return An array containing two items: a list of verified email addresses, and the request id. | * @return An array containing two items: a list of verified email addresses, and the request id. | ||||
| */ | */ | ||||
| public function listVerifiedEmailAddresses() { | public function listVerifiedEmailAddresses() { | ||||
| $rest = new SimpleEmailServiceRequest($this, 'GET'); | $rest = new SimpleEmailServiceRequest($this, 'GET'); | ||||
| $rest->setParameter('Action', 'ListVerifiedEmailAddresses'); | $rest->setParameter('Action', 'ListVerifiedEmailAddresses'); | ||||
| $rest = $rest->getResponse(); | $rest = $rest->getResponse(); | ||||
| if($rest->error === false && $rest->code !== 200) { | |||||
| $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |||||
| } | |||||
| if($rest->error !== false) { | |||||
| $this->__triggerError('listVerifiedEmailAddresses', $rest->error); | |||||
| return false; | |||||
| } | |||||
| $response = array(); | $response = array(); | ||||
| if(!isset($rest->body)) { | if(!isset($rest->body)) { | ||||
| return $response; | return $response; | ||||
| } | } | ||||
| $addresses = array(); | $addresses = array(); | ||||
| foreach($rest->body->ListVerifiedEmailAddressesResult->VerifiedEmailAddresses->member as $address) { | foreach($rest->body->ListVerifiedEmailAddressesResult->VerifiedEmailAddresses->member as $address) { | ||||
| Show All 17 Lines | class SimpleEmailService | ||||
| * @return The request id for this request. | * @return The request id for this request. | ||||
| */ | */ | ||||
| public function verifyEmailAddress($email) { | public function verifyEmailAddress($email) { | ||||
| $rest = new SimpleEmailServiceRequest($this, 'POST'); | $rest = new SimpleEmailServiceRequest($this, 'POST'); | ||||
| $rest->setParameter('Action', 'VerifyEmailAddress'); | $rest->setParameter('Action', 'VerifyEmailAddress'); | ||||
| $rest->setParameter('EmailAddress', $email); | $rest->setParameter('EmailAddress', $email); | ||||
| $rest = $rest->getResponse(); | $rest = $rest->getResponse(); | ||||
| if($rest->error === false && $rest->code !== 200) { | |||||
| $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |||||
| } | |||||
| if($rest->error !== false) { | |||||
| $this->__triggerError('verifyEmailAddress', $rest->error); | |||||
| return false; | |||||
| } | |||||
| $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId; | $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId; | ||||
| return $response; | return $response; | ||||
| } | } | ||||
| /** | /** | ||||
| * Removes the specified email address from the list of verified addresses. | * Removes the specified email address from the list of verified addresses. | ||||
| * | * | ||||
| * @param string email The email address to remove | * @param string email The email address to remove | ||||
| * @return The request id for this request. | * @return The request id for this request. | ||||
| */ | */ | ||||
| public function deleteVerifiedEmailAddress($email) { | public function deleteVerifiedEmailAddress($email) { | ||||
| $rest = new SimpleEmailServiceRequest($this, 'DELETE'); | $rest = new SimpleEmailServiceRequest($this, 'DELETE'); | ||||
| $rest->setParameter('Action', 'DeleteVerifiedEmailAddress'); | $rest->setParameter('Action', 'DeleteVerifiedEmailAddress'); | ||||
| $rest->setParameter('EmailAddress', $email); | $rest->setParameter('EmailAddress', $email); | ||||
| $rest = $rest->getResponse(); | $rest = $rest->getResponse(); | ||||
| if($rest->error === false && $rest->code !== 200) { | |||||
| $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |||||
| } | |||||
| if($rest->error !== false) { | |||||
| $this->__triggerError('deleteVerifiedEmailAddress', $rest->error); | |||||
| return false; | |||||
| } | |||||
| $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId; | $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId; | ||||
| return $response; | return $response; | ||||
| } | } | ||||
| /** | /** | ||||
| * Retrieves information on the current activity limits for this account. | * Retrieves information on the current activity limits for this account. | ||||
| * See http://docs.amazonwebservices.com/ses/latest/APIReference/API_GetSendQuota.html | * See http://docs.amazonwebservices.com/ses/latest/APIReference/API_GetSendQuota.html | ||||
| * | * | ||||
| * @return An array containing information on this account's activity limits. | * @return An array containing information on this account's activity limits. | ||||
| */ | */ | ||||
| public function getSendQuota() { | public function getSendQuota() { | ||||
| $rest = new SimpleEmailServiceRequest($this, 'GET'); | $rest = new SimpleEmailServiceRequest($this, 'GET'); | ||||
| $rest->setParameter('Action', 'GetSendQuota'); | $rest->setParameter('Action', 'GetSendQuota'); | ||||
| $rest = $rest->getResponse(); | $rest = $rest->getResponse(); | ||||
| if($rest->error === false && $rest->code !== 200) { | |||||
| $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |||||
| } | |||||
| if($rest->error !== false) { | |||||
| $this->__triggerError('getSendQuota', $rest->error); | |||||
| return false; | |||||
| } | |||||
| $response = array(); | $response = array(); | ||||
| if(!isset($rest->body)) { | if(!isset($rest->body)) { | ||||
| return $response; | return $response; | ||||
| } | } | ||||
| $response['Max24HourSend'] = (string)$rest->body->GetSendQuotaResult->Max24HourSend; | $response['Max24HourSend'] = (string)$rest->body->GetSendQuotaResult->Max24HourSend; | ||||
| $response['MaxSendRate'] = (string)$rest->body->GetSendQuotaResult->MaxSendRate; | $response['MaxSendRate'] = (string)$rest->body->GetSendQuotaResult->MaxSendRate; | ||||
| Show All 9 Lines | class SimpleEmailService | ||||
| * | * | ||||
| * @return An array of activity statistics. Each array item covers a 15-minute period. | * @return An array of activity statistics. Each array item covers a 15-minute period. | ||||
| */ | */ | ||||
| public function getSendStatistics() { | public function getSendStatistics() { | ||||
| $rest = new SimpleEmailServiceRequest($this, 'GET'); | $rest = new SimpleEmailServiceRequest($this, 'GET'); | ||||
| $rest->setParameter('Action', 'GetSendStatistics'); | $rest->setParameter('Action', 'GetSendStatistics'); | ||||
| $rest = $rest->getResponse(); | $rest = $rest->getResponse(); | ||||
| if($rest->error === false && $rest->code !== 200) { | |||||
| $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |||||
| } | |||||
| if($rest->error !== false) { | |||||
| $this->__triggerError('getSendStatistics', $rest->error); | |||||
| return false; | |||||
| } | |||||
| $response = array(); | $response = array(); | ||||
| if(!isset($rest->body)) { | if(!isset($rest->body)) { | ||||
| return $response; | return $response; | ||||
| } | } | ||||
| $datapoints = array(); | $datapoints = array(); | ||||
| foreach($rest->body->GetSendStatisticsResult->SendDataPoints->member as $datapoint) { | foreach($rest->body->GetSendStatisticsResult->SendDataPoints->member as $datapoint) { | ||||
| Show All 15 Lines | class SimpleEmailService | ||||
| public function sendRawEmail($raw) { | public function sendRawEmail($raw) { | ||||
| $rest = new SimpleEmailServiceRequest($this, 'POST'); | $rest = new SimpleEmailServiceRequest($this, 'POST'); | ||||
| $rest->setParameter('Action', 'SendRawEmail'); | $rest->setParameter('Action', 'SendRawEmail'); | ||||
| $rest->setParameter('RawMessage.Data', base64_encode($raw)); | $rest->setParameter('RawMessage.Data', base64_encode($raw)); | ||||
| $rest = $rest->getResponse(); | $rest = $rest->getResponse(); | ||||
| if($rest->error === false && $rest->code !== 200) { | |||||
| $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |||||
| } | |||||
| if($rest->error !== false) { | |||||
| $this->__triggerError('sendRawEmail', $rest->error); | |||||
| return false; | |||||
| } | |||||
| $response['MessageId'] = (string)$rest->body->SendEmailResult->MessageId; | $response['MessageId'] = (string)$rest->body->SendEmailResult->MessageId; | ||||
| $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId; | $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId; | ||||
| return $response; | return $response; | ||||
| } | } | ||||
| /** | /** | ||||
| * Given a SimpleEmailServiceMessage object, submits the message to the service for sending. | * Given a SimpleEmailServiceMessage object, submits the message to the service for sending. | ||||
| ▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | public function sendEmail($sesMessage) { | ||||
| if($sesMessage->messagehtml != null && strlen($sesMessage->messagehtml) > 0) { | if($sesMessage->messagehtml != null && strlen($sesMessage->messagehtml) > 0) { | ||||
| $rest->setParameter('Message.Body.Html.Data', $sesMessage->messagehtml); | $rest->setParameter('Message.Body.Html.Data', $sesMessage->messagehtml); | ||||
| if($sesMessage->messageHtmlCharset != null && strlen($sesMessage->messageHtmlCharset) > 0) { | if($sesMessage->messageHtmlCharset != null && strlen($sesMessage->messageHtmlCharset) > 0) { | ||||
| $rest->setParameter('Message.Body.Html.Charset', $sesMessage->messageHtmlCharset); | $rest->setParameter('Message.Body.Html.Charset', $sesMessage->messageHtmlCharset); | ||||
| } | } | ||||
| } | } | ||||
| $rest = $rest->getResponse(); | $rest = $rest->getResponse(); | ||||
| if($rest->error === false && $rest->code !== 200) { | |||||
| $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |||||
| } | |||||
| if($rest->error !== false) { | |||||
| $this->__triggerError('sendEmail', $rest->error); | |||||
| return false; | |||||
| } | |||||
| $response['MessageId'] = (string)$rest->body->SendEmailResult->MessageId; | $response['MessageId'] = (string)$rest->body->SendEmailResult->MessageId; | ||||
| $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId; | $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId; | ||||
| return $response; | return $response; | ||||
| } | } | ||||
| /** | /** | ||||
| * Trigger an error message | * Trigger an error message | ||||
| ▲ Show 20 Lines • Show All 149 Lines • ▼ Show 20 Lines | public function getResponse() { | ||||
| curl_setopt($curl, CURLOPT_HEADER, false); | curl_setopt($curl, CURLOPT_HEADER, false); | ||||
| curl_setopt($curl, CURLOPT_URL, $url); | curl_setopt($curl, CURLOPT_URL, $url); | ||||
| curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); | curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); | ||||
| curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback')); | curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback')); | ||||
| curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | ||||
| // Execute, grab errors | // Execute, grab errors | ||||
| if (curl_exec($curl)) { | if (!curl_exec($curl)) { | ||||
| throw new SimpleEmailServiceException( | |||||
| pht( | |||||
| 'Encountered an error while making an HTTP request to Amazon SES '. | |||||
| '(cURL Error #%d): %s', | |||||
| curl_errno($curl), | |||||
| curl_error($curl))); | |||||
| } | |||||
| $this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | $this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | ||||
| } else { | if ($this->response->code != 200) { | ||||
| $this->response->error = array( | throw new SimpleEmailServiceException( | ||||
| 'curl' => true, | pht( | ||||
| 'code' => curl_errno($curl), | 'Unexpected HTTP status while making request to Amazon SES: '. | ||||
| 'message' => curl_error($curl), | 'expected 200, got %s.', | ||||
| ); | $this->response->code)); | ||||
| } | } | ||||
| @curl_close($curl); | @curl_close($curl); | ||||
| // Parse body into XML | // Parse body into XML | ||||
| if ($this->response->error === false && isset($this->response->body)) { | if ($this->response->error === false && isset($this->response->body)) { | ||||
| $this->response->body = simplexml_load_string($this->response->body); | $this->response->body = simplexml_load_string($this->response->body); | ||||
| ▲ Show 20 Lines • Show All 201 Lines • Show Last 20 Lines | |||||