[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/externals/amazon-ses/ -> ses.php (source)

   1  <?php
   2  /**
   3  *
   4  * Copyright (c) 2011, Dan Myers.
   5  * Parts copyright (c) 2008, Donovan Schonknecht.
   6  * All rights reserved.
   7  *
   8  * Redistribution and use in source and binary forms, with or without
   9  * modification, are permitted provided that the following conditions are met:
  10  *
  11  * - Redistributions of source code must retain the above copyright notice,
  12  *   this list of conditions and the following disclaimer.
  13  * - Redistributions in binary form must reproduce the above copyright
  14  *   notice, this list of conditions and the following disclaimer in the
  15  *   documentation and/or other materials provided with the distribution.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27  * POSSIBILITY OF SUCH DAMAGE.
  28  *
  29  * This is a modified BSD license (the third clause has been removed).
  30  * The BSD license may be found here:
  31  * http://www.opensource.org/licenses/bsd-license.php
  32  *
  33  * Amazon Simple Email Service is a trademark of Amazon.com, Inc. or its affiliates.
  34  *
  35  * SimpleEmailService is based on Donovan Schonknecht's Amazon S3 PHP class, found here:
  36  * http://undesigned.org.za/2007/10/22/amazon-s3-php-class
  37  *
  38  */
  39  
  40  /**
  41  * Amazon SimpleEmailService PHP class
  42  *
  43  * @link http://sourceforge.net/projects/php-aws-ses/
  44  * version 0.8.1
  45  *
  46  */
  47  class SimpleEmailService
  48  {
  49    protected $__accessKey; // AWS Access key
  50    protected $__secretKey; // AWS Secret key
  51    protected $__host;
  52  
  53    public function getAccessKey() { return $this->__accessKey; }
  54    public function getSecretKey() { return $this->__secretKey; }
  55    public function getHost() { return $this->__host; }
  56  
  57    protected $__verifyHost = 1;
  58    protected $__verifyPeer = 1;
  59  
  60    // verifyHost and verifyPeer determine whether curl verifies ssl certificates.
  61    // It may be necessary to disable these checks on certain systems.
  62    // These only have an effect if SSL is enabled.
  63    public function verifyHost() { return $this->__verifyHost; }
  64    public function enableVerifyHost($enable = true) { $this->__verifyHost = $enable; }
  65  
  66    public function verifyPeer() { return $this->__verifyPeer; }
  67    public function enableVerifyPeer($enable = true) { $this->__verifyPeer = $enable; }
  68  
  69    // If you use exceptions, errors will be communicated by throwing a
  70    // SimpleEmailServiceException. By default, they will be trigger_error()'d.
  71    protected $__useExceptions = 0;
  72    public function useExceptions() { return $this->__useExceptions; }
  73    public function enableUseExceptions($enable = true) { $this->__useExceptions = $enable; }
  74  
  75    /**
  76    * Constructor
  77    *
  78    * @param string $accessKey Access key
  79    * @param string $secretKey Secret key
  80    * @return void
  81    */
  82    public function __construct($accessKey = null, $secretKey = null, $host = 'email.us-east-1.amazonaws.com') {
  83      if ($accessKey !== null && $secretKey !== null) {
  84        $this->setAuth($accessKey, $secretKey);
  85      }
  86      $this->__host = $host;
  87    }
  88  
  89    /**
  90    * Set AWS access key and secret key
  91    *
  92    * @param string $accessKey Access key
  93    * @param string $secretKey Secret key
  94    * @return void
  95    */
  96    public function setAuth($accessKey, $secretKey) {
  97      $this->__accessKey = $accessKey;
  98      $this->__secretKey = $secretKey;
  99    }
 100  
 101    /**
 102    * Lists the email addresses that have been verified and can be used as the 'From' address
 103    *
 104    * @return An array containing two items: a list of verified email addresses, and the request id.
 105    */
 106    public function listVerifiedEmailAddresses() {
 107      $rest = new SimpleEmailServiceRequest($this, 'GET');
 108      $rest->setParameter('Action', 'ListVerifiedEmailAddresses');
 109  
 110      $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      }
 118  
 119      $response = array();
 120      if(!isset($rest->body)) {
 121        return $response;
 122      }
 123  
 124      $addresses = array();
 125      foreach($rest->body->ListVerifiedEmailAddressesResult->VerifiedEmailAddresses->member as $address) {
 126        $addresses[] = (string)$address;
 127      }
 128  
 129      $response['Addresses'] = $addresses;
 130      $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId;
 131  
 132      return $response;
 133    }
 134  
 135    /**
 136    * Requests verification of the provided email address, so it can be used
 137    * as the 'From' address when sending emails through SimpleEmailService.
 138    *
 139    * After submitting this request, you should receive a verification email
 140    * from Amazon at the specified address containing instructions to follow.
 141    *
 142    * @param string email The email address to get verified
 143    * @return The request id for this request.
 144    */
 145    public function verifyEmailAddress($email) {
 146      $rest = new SimpleEmailServiceRequest($this, 'POST');
 147      $rest->setParameter('Action', 'VerifyEmailAddress');
 148      $rest->setParameter('EmailAddress', $email);
 149  
 150      $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      }
 158  
 159      $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId;
 160      return $response;
 161    }
 162  
 163    /**
 164    * Removes the specified email address from the list of verified addresses.
 165    *
 166    * @param string email The email address to remove
 167    * @return The request id for this request.
 168    */
 169    public function deleteVerifiedEmailAddress($email) {
 170      $rest = new SimpleEmailServiceRequest($this, 'DELETE');
 171      $rest->setParameter('Action', 'DeleteVerifiedEmailAddress');
 172      $rest->setParameter('EmailAddress', $email);
 173  
 174      $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      }
 182  
 183      $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId;
 184      return $response;
 185    }
 186  
 187    /**
 188    * Retrieves information on the current activity limits for this account.
 189    * See http://docs.amazonwebservices.com/ses/latest/APIReference/API_GetSendQuota.html
 190    *
 191    * @return An array containing information on this account's activity limits.
 192    */
 193    public function getSendQuota() {
 194      $rest = new SimpleEmailServiceRequest($this, 'GET');
 195      $rest->setParameter('Action', 'GetSendQuota');
 196  
 197      $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      }
 205  
 206      $response = array();
 207      if(!isset($rest->body)) {
 208        return $response;
 209      }
 210  
 211      $response['Max24HourSend'] = (string)$rest->body->GetSendQuotaResult->Max24HourSend;
 212      $response['MaxSendRate'] = (string)$rest->body->GetSendQuotaResult->MaxSendRate;
 213      $response['SentLast24Hours'] = (string)$rest->body->GetSendQuotaResult->SentLast24Hours;
 214      $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId;
 215  
 216      return $response;
 217    }
 218  
 219    /**
 220    * Retrieves statistics for the last two weeks of activity on this account.
 221    * See http://docs.amazonwebservices.com/ses/latest/APIReference/API_GetSendStatistics.html
 222    *
 223    * @return An array of activity statistics.  Each array item covers a 15-minute period.
 224    */
 225    public function getSendStatistics() {
 226      $rest = new SimpleEmailServiceRequest($this, 'GET');
 227      $rest->setParameter('Action', 'GetSendStatistics');
 228  
 229      $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      }
 237  
 238      $response = array();
 239      if(!isset($rest->body)) {
 240        return $response;
 241      }
 242  
 243      $datapoints = array();
 244      foreach($rest->body->GetSendStatisticsResult->SendDataPoints->member as $datapoint) {
 245        $p = array();
 246        $p['Bounces'] = (string)$datapoint->Bounces;
 247        $p['Complaints'] = (string)$datapoint->Complaints;
 248        $p['DeliveryAttempts'] = (string)$datapoint->DeliveryAttempts;
 249        $p['Rejects'] = (string)$datapoint->Rejects;
 250        $p['Timestamp'] = (string)$datapoint->Timestamp;
 251  
 252        $datapoints[] = $p;
 253      }
 254  
 255      $response['SendDataPoints'] = $datapoints;
 256      $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId;
 257  
 258      return $response;
 259    }
 260  
 261  
 262    public function sendRawEmail($raw) {
 263      $rest = new SimpleEmailServiceRequest($this, 'POST');
 264      $rest->setParameter('Action', 'SendRawEmail');
 265      $rest->setParameter('RawMessage.Data', base64_encode($raw));
 266  
 267      $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      }
 275  
 276      $response['MessageId'] = (string)$rest->body->SendEmailResult->MessageId;
 277      $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId;
 278      return $response;
 279    }
 280  
 281    /**
 282    * Given a SimpleEmailServiceMessage object, submits the message to the service for sending.
 283    *
 284    * @return An array containing the unique identifier for this message and a separate request id.
 285    *         Returns false if the provided message is missing any required fields.
 286    */
 287    public function sendEmail($sesMessage) {
 288      if(!$sesMessage->validate()) {
 289        return false;
 290      }
 291  
 292      $rest = new SimpleEmailServiceRequest($this, 'POST');
 293      $rest->setParameter('Action', 'SendEmail');
 294  
 295      $i = 1;
 296      foreach($sesMessage->to as $to) {
 297        $rest->setParameter('Destination.ToAddresses.member.'.$i, $to);
 298        $i++;
 299      }
 300  
 301      if(is_array($sesMessage->cc)) {
 302        $i = 1;
 303        foreach($sesMessage->cc as $cc) {
 304          $rest->setParameter('Destination.CcAddresses.member.'.$i, $cc);
 305          $i++;
 306        }
 307      }
 308  
 309      if(is_array($sesMessage->bcc)) {
 310        $i = 1;
 311        foreach($sesMessage->bcc as $bcc) {
 312          $rest->setParameter('Destination.BccAddresses.member.'.$i, $bcc);
 313          $i++;
 314        }
 315      }
 316  
 317      if(is_array($sesMessage->replyto)) {
 318        $i = 1;
 319        foreach($sesMessage->replyto as $replyto) {
 320          $rest->setParameter('ReplyToAddresses.member.'.$i, $replyto);
 321          $i++;
 322        }
 323      }
 324  
 325      $rest->setParameter('Source', $sesMessage->from);
 326  
 327      if($sesMessage->returnpath != null) {
 328        $rest->setParameter('ReturnPath', $sesMessage->returnpath);
 329      }
 330  
 331      if($sesMessage->subject != null && strlen($sesMessage->subject) > 0) {
 332        $rest->setParameter('Message.Subject.Data', $sesMessage->subject);
 333        if($sesMessage->subjectCharset != null && strlen($sesMessage->subjectCharset) > 0) {
 334          $rest->setParameter('Message.Subject.Charset', $sesMessage->subjectCharset);
 335        }
 336      }
 337  
 338  
 339      if($sesMessage->messagetext != null && strlen($sesMessage->messagetext) > 0) {
 340        $rest->setParameter('Message.Body.Text.Data', $sesMessage->messagetext);
 341        if($sesMessage->messageTextCharset != null && strlen($sesMessage->messageTextCharset) > 0) {
 342          $rest->setParameter('Message.Body.Text.Charset', $sesMessage->messageTextCharset);
 343        }
 344      }
 345  
 346      if($sesMessage->messagehtml != null && strlen($sesMessage->messagehtml) > 0) {
 347        $rest->setParameter('Message.Body.Html.Data', $sesMessage->messagehtml);
 348        if($sesMessage->messageHtmlCharset != null && strlen($sesMessage->messageHtmlCharset) > 0) {
 349          $rest->setParameter('Message.Body.Html.Charset', $sesMessage->messageHtmlCharset);
 350        }
 351      }
 352  
 353      $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      }
 361  
 362      $response['MessageId'] = (string)$rest->body->SendEmailResult->MessageId;
 363      $response['RequestId'] = (string)$rest->body->ResponseMetadata->RequestId;
 364      return $response;
 365    }
 366  
 367    /**
 368    * Trigger an error message
 369    *
 370    * @internal Used by member functions to output errors
 371    * @param array $error Array containing error information
 372    * @return string
 373    */
 374    public function __triggerError($functionname, $error)
 375    {
 376      if($error == false) {
 377        $message = sprintf("SimpleEmailService::%s(): Encountered an error, but no description given", $functionname);
 378      }
 379      else if(isset($error['curl']) && $error['curl'])
 380      {
 381        $message = sprintf("SimpleEmailService::%s(): %s %s", $functionname, $error['code'], $error['message']);
 382      }
 383      else if(isset($error['Error']))
 384      {
 385        $e = $error['Error'];
 386        $message = sprintf("SimpleEmailService::%s(): %s - %s: %s\nRequest Id: %s\n", $functionname, $e['Type'], $e['Code'], $e['Message'], $error['RequestId']);
 387      }
 388  
 389      if ($this->useExceptions()) {
 390        throw new SimpleEmailServiceException($message);
 391      } else {
 392        trigger_error($message, E_USER_WARNING);
 393      }
 394    }
 395  
 396    /**
 397    * Callback handler for 503 retries.
 398    *
 399    * @internal Used by SimpleDBRequest to call the user-specified callback, if set
 400    * @param $attempt The number of failed attempts so far
 401    * @return The retry delay in microseconds, or 0 to stop retrying.
 402    */
 403    public function __executeServiceTemporarilyUnavailableRetryDelay($attempt)
 404    {
 405      if(is_callable($this->__serviceUnavailableRetryDelayCallback)) {
 406        $callback = $this->__serviceUnavailableRetryDelayCallback;
 407        return $callback($attempt);
 408      }
 409      return 0;
 410    }
 411  }
 412  
 413  final class SimpleEmailServiceRequest
 414  {
 415    private $ses, $verb, $parameters = array();
 416    public $response;
 417  
 418    /**
 419    * Constructor
 420    *
 421    * @param string $ses The SimpleEmailService object making this request
 422    * @param string $action action
 423    * @param string $verb HTTP verb
 424    * @return mixed
 425    */
 426    function __construct($ses, $verb) {
 427      $this->ses = $ses;
 428      $this->verb = $verb;
 429      $this->response = new STDClass;
 430      $this->response->error = false;
 431    }
 432  
 433    /**
 434    * Set request parameter
 435    *
 436    * @param string  $key Key
 437    * @param string  $value Value
 438    * @param boolean $replace Whether to replace the key if it already exists (default true)
 439    * @return void
 440    */
 441    public function setParameter($key, $value, $replace = true) {
 442      if(!$replace && isset($this->parameters[$key]))
 443      {
 444        $temp = (array)($this->parameters[$key]);
 445        $temp[] = $value;
 446        $this->parameters[$key] = $temp;
 447      }
 448      else
 449      {
 450        $this->parameters[$key] = $value;
 451      }
 452    }
 453  
 454    /**
 455    * Get the response
 456    *
 457    * @return object | false
 458    */
 459    public function getResponse() {
 460  
 461      $params = array();
 462      foreach ($this->parameters as $var => $value)
 463      {
 464        if(is_array($value))
 465        {
 466          foreach($value as $v)
 467          {
 468            $params[] = $var.'='.$this->__customUrlEncode($v);
 469          }
 470        }
 471        else
 472        {
 473          $params[] = $var.'='.$this->__customUrlEncode($value);
 474        }
 475      }
 476  
 477      sort($params, SORT_STRING);
 478  
 479      // must be in format 'Sun, 06 Nov 1994 08:49:37 GMT'
 480      $date = gmdate('D, d M Y H:i:s e');
 481  
 482      $query = implode('&', $params);
 483  
 484      $headers = array();
 485      $headers[] = 'Date: '.$date;
 486      $headers[] = 'Host: '.$this->ses->getHost();
 487  
 488      $auth = 'AWS3-HTTPS AWSAccessKeyId='.$this->ses->getAccessKey();
 489      $auth .= ',Algorithm=HmacSHA256,Signature='.$this->__getSignature($date);
 490      $headers[] = 'X-Amzn-Authorization: '.$auth;
 491  
 492      $url = 'https://'.$this->ses->getHost().'/';
 493  
 494      // Basic setup
 495      $curl = curl_init();
 496      curl_setopt($curl, CURLOPT_USERAGENT, 'SimpleEmailService/php');
 497  
 498      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, ($this->ses->verifyHost() ? 2 : 0));
 499      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, ($this->ses->verifyPeer() ? 1 : 0));
 500  
 501      // Request types
 502      switch ($this->verb) {
 503        case 'GET':
 504          $url .= '?'.$query;
 505          break;
 506        case 'POST':
 507          curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
 508          curl_setopt($curl, CURLOPT_POSTFIELDS, $query);
 509          $headers[] = 'Content-Type: application/x-www-form-urlencoded';
 510        break;
 511        case 'DELETE':
 512          $url .= '?'.$query;
 513          curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
 514        break;
 515        default: break;
 516      }
 517      curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
 518      curl_setopt($curl, CURLOPT_HEADER, false);
 519  
 520      curl_setopt($curl, CURLOPT_URL, $url);
 521      curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
 522      curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback'));
 523      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
 524  
 525      // 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          'resource' => $this->resource
 534        );
 535      }
 536  
 537      @curl_close($curl);
 538  
 539      // Parse body into XML
 540      if ($this->response->error === false && isset($this->response->body)) {
 541        $this->response->body = simplexml_load_string($this->response->body);
 542  
 543        // Grab SES errors
 544        if (!in_array($this->response->code, array(200, 201, 202, 204))
 545          && isset($this->response->body->Error)) {
 546          $error = $this->response->body->Error;
 547          $output = array();
 548          $output['curl'] = false;
 549          $output['Error'] = array();
 550          $output['Error']['Type'] = (string)$error->Type;
 551          $output['Error']['Code'] = (string)$error->Code;
 552          $output['Error']['Message'] = (string)$error->Message;
 553          $output['RequestId'] = (string)$this->response->body->RequestId;
 554  
 555          $this->response->error = $output;
 556          unset($this->response->body);
 557        }
 558      }
 559  
 560      return $this->response;
 561    }
 562  
 563    /**
 564    * CURL write callback
 565    *
 566    * @param resource &$curl CURL resource
 567    * @param string &$data Data
 568    * @return integer
 569    */
 570    private function __responseWriteCallback(&$curl, &$data) {
 571      if(!isset($this->response->body)) $this->response->body = '';
 572      $this->response->body .= $data;
 573      return strlen($data);
 574    }
 575  
 576    /**
 577    * Contributed by afx114
 578    * URL encode the parameters as per http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?Query_QueryAuth.html
 579    * PHP's rawurlencode() follows RFC 1738, not RFC 3986 as required by Amazon. The only difference is the tilde (~), so convert it back after rawurlencode
 580    * See: http://www.morganney.com/blog/API/AWS-Product-Advertising-API-Requires-a-Signed-Request.php
 581    *
 582    * @param string $var String to encode
 583    * @return string
 584    */
 585    private function __customUrlEncode($var) {
 586      return str_replace('%7E', '~', rawurlencode($var));
 587    }
 588  
 589    /**
 590    * Generate the auth string using Hmac-SHA256
 591    *
 592    * @internal Used by SimpleDBRequest::getResponse()
 593    * @param string $string String to sign
 594    * @return string
 595    */
 596    private function __getSignature($string) {
 597      return base64_encode(hash_hmac('sha256', $string, $this->ses->getSecretKey(), true));
 598    }
 599  }
 600  
 601  
 602  final class SimpleEmailServiceMessage {
 603  
 604    // these are public for convenience only
 605    // these are not to be used outside of the SimpleEmailService class!
 606    public $to, $cc, $bcc, $replyto;
 607    public $from, $returnpath;
 608    public $subject, $messagetext, $messagehtml;
 609    public $subjectCharset, $messageTextCharset, $messageHtmlCharset;
 610  
 611    function __construct() {
 612      $to = array();
 613      $cc = array();
 614      $bcc = array();
 615      $replyto = array();
 616  
 617      $from = null;
 618      $returnpath = null;
 619  
 620      $subject = null;
 621      $messagetext = null;
 622      $messagehtml = null;
 623  
 624      $subjectCharset = null;
 625      $messageTextCharset = null;
 626      $messageHtmlCharset = null;
 627    }
 628  
 629  
 630    /**
 631    * addTo, addCC, addBCC, and addReplyTo have the following behavior:
 632    * If a single address is passed, it is appended to the current list of addresses.
 633    * If an array of addresses is passed, that array is merged into the current list.
 634    */
 635    function addTo($to) {
 636      if(!is_array($to)) {
 637        $this->to[] = $to;
 638      }
 639      else {
 640        $this->to = array_merge($this->to, $to);
 641      }
 642    }
 643  
 644    function addCC($cc) {
 645      if(!is_array($cc)) {
 646        $this->cc[] = $cc;
 647      }
 648      else {
 649        $this->cc = array_merge($this->cc, $cc);
 650      }
 651    }
 652  
 653    function addBCC($bcc) {
 654      if(!is_array($bcc)) {
 655        $this->bcc[] = $bcc;
 656      }
 657      else {
 658        $this->bcc = array_merge($this->bcc, $bcc);
 659      }
 660    }
 661  
 662    function addReplyTo($replyto) {
 663      if(!is_array($replyto)) {
 664        $this->replyto[] = $replyto;
 665      }
 666      else {
 667        $this->replyto = array_merge($this->replyto, $replyto);
 668      }
 669    }
 670  
 671    function setFrom($from) {
 672      $this->from = $from;
 673    }
 674  
 675    function setReturnPath($returnpath) {
 676      $this->returnpath = $returnpath;
 677    }
 678  
 679    function setSubject($subject) {
 680      $this->subject = $subject;
 681    }
 682  
 683    function setSubjectCharset($charset) {
 684      $this->subjectCharset = $charset;
 685    }
 686  
 687    function setMessageFromString($text, $html = null) {
 688      $this->messagetext = $text;
 689      $this->messagehtml = $html;
 690    }
 691  
 692    function setMessageFromFile($textfile, $htmlfile = null) {
 693      if(file_exists($textfile) && is_file($textfile) && is_readable($textfile)) {
 694        $this->messagetext = file_get_contents($textfile);
 695      }
 696      if(file_exists($htmlfile) && is_file($htmlfile) && is_readable($htmlfile)) {
 697        $this->messagehtml = file_get_contents($htmlfile);
 698      }
 699    }
 700  
 701    function setMessageFromURL($texturl, $htmlurl = null) {
 702      $this->messagetext = file_get_contents($texturl);
 703      if($htmlurl !== null) {
 704        $this->messagehtml = file_get_contents($htmlurl);
 705      }
 706    }
 707  
 708    function setMessageCharset($textCharset, $htmlCharset = null) {
 709      $this->messageTextCharset = $textCharset;
 710      $this->messageHtmlCharset = $htmlCharset;
 711    }
 712  
 713    /**
 714    * Validates whether the message object has sufficient information to submit a request to SES.
 715    * This does not guarantee the message will arrive, nor that the request will succeed;
 716    * instead, it makes sure that no required fields are missing.
 717    *
 718    * This is used internally before attempting a SendEmail or SendRawEmail request,
 719    * but it can be used outside of this file if verification is desired.
 720    * May be useful if e.g. the data is being populated from a form; developers can generally
 721    * use this function to verify completeness instead of writing custom logic.
 722    *
 723    * @return boolean
 724    */
 725    public function validate() {
 726      if(count($this->to) == 0)
 727        return false;
 728      if($this->from == null || strlen($this->from) == 0)
 729        return false;
 730      if($this->messagetext == null)
 731        return false;
 732      return true;
 733    }
 734  }
 735  
 736  
 737  /**
 738   * Thrown by SimpleEmailService when errors occur if you call
 739   * enableUseExceptions(true).
 740   */
 741  final class SimpleEmailServiceException extends Exception {
 742  
 743  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1