You are here

public function AmazonSesHandler::send in Amazon SES 2.0.x

Send an email using the AWS SDK.

Parameters

array $message: An array of message data.

Return value

string|bool The message ID tf successful, or FALSE if an error occurs.

Overrides AmazonSesHandlerInterface::send

File

src/AmazonSesHandler.php, line 58

Class

AmazonSesHandler
Amazon SES service.

Namespace

Drupal\amazon_ses

Code

public function send(array $message) {
  if (!$message['from']) {
    $message['from'] = $this->config
      ->get('from_address');
  }
  if (!$message['reply-to']) {
    $message['reply-to'] = $message['from'];
  }
  $params = [
    'Destination' => [
      'ToAddresses' => [
        $message['to'],
      ],
    ],
    'ReplyToAddresses' => [
      $message['reply-to'],
    ],
    'Source' => $message['from'],
    'Message' => [
      'Subject' => [
        'Data' => $message['subject'],
      ],
    ],
  ];
  if (isset($message['headers']) && isset($message['headers']['Content-Type'])) {
    $content_type_parts = explode(';', $message['headers']['Content-Type']);
    $content_type = trim($content_type_parts[0]);
  }
  else {
    $content_type = 'text/plain';
  }
  switch ($content_type) {
    case 'text/plain':
      $params['Message']['Body']['Text']['Data'] = $message['body'];
      break;
    case 'text/html':
      $params['Message']['Body']['Html']['Data'] = $message['body'];
      break;
    case 'multipart/mixed':
      $parts = $this
        ->getParts($message);
      $params['Message']['Body']['Text']['Data'] = $parts['plain'];
      $params['Message']['Body']['Html']['Data'] = $parts['html'];
      break;
    default:
      $params['Message']['Body']['Text']['Data'] = $message['body'];
      $warning = $this
        ->t('Unsupported content type: @type', [
        '@type' => $content_type,
      ]);
      $this->logger
        ->warning($warning);
      break;
  }
  try {
    $result = $this->client
      ->sendEmail($params);
    $throttle = $this->config
      ->get('throttle');
    if ($throttle) {
      $sleep_time = $this
        ->getSleepTime();
      usleep($sleep_time);
    }
    return $result['MessageId'];
  } catch (CredentialsException $e) {
    $this->logger
      ->error($e
      ->getMessage());
    return FALSE;
  } catch (SesException $e) {
    $this->logger
      ->error($e
      ->getAwsErrorMessage());
    return FALSE;
  }
}