Commit 0501b019 authored by davisben's avatar davisben
Browse files

Issue #3278501: Support for SesV2Client

parent 7de92450
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ services:
    arguments: ['@amazon_ses.client', '@logger.channel.amazon_ses', '@messenger', '@event_dispatcher', '@config.factory']

  amazon_ses.client:
    class: Aws\Ses\SesClient
    class: Aws\SesV2\SesV2Client
    factory: 'amazon_ses.client_factory:createInstance'
    arguments: ['%amazon_ses_client.options%', '@config.factory']

+3 −3
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@
namespace Drupal\amazon_ses;

use Aws\Exception\CredentialsException;
use Aws\Ses\SesClient;
use Aws\SesV2\SesV2Client;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
@@ -39,7 +39,7 @@ class AmazonSesClientFactory {
   * @param \Drupal\Core\Config\ConfigFactory $configFactory
   *   The config factory.
   *
   * @return \Aws\Ses\SesClient
   * @return \Aws\SesV2\SesV2Client
   *   The client instance.
   */
  public function createInstance(array $options, ConfigFactory $configFactory) {
@@ -56,7 +56,7 @@ class AmazonSesClientFactory {
    }

    try {
      $client = new SesClient($options);
      $client = new SesV2Client($options);

      return $client;
    }
+30 −64
Original line number Diff line number Diff line
@@ -4,8 +4,8 @@ namespace Drupal\amazon_ses;

use Aws\Exception\CredentialsException;
use Aws\Result;
use Aws\Ses\SesClient;
use Aws\Ses\Exception\SesException;
use Aws\SesV2\SesV2Client;
use Aws\SesV2\Exception\SesV2Exception;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Messenger\MessengerInterface;
@@ -23,7 +23,7 @@ class AmazonSesHandler implements AmazonSesHandlerInterface {
  /**
   * The AWS SesClient.
   *
   * @var \Aws\Ses\SesClient
   * @var \Aws\SesV2\SesV2Client
   */
  protected $client;

@@ -58,7 +58,7 @@ class AmazonSesHandler implements AmazonSesHandlerInterface {
  /**
   * Constructs the service.
   *
   * @param \Aws\Ses\SesClient $client
   * @param \Aws\SesV2\SesV2Client $client
   *   The AWS SesClient.
   * @param \Drupal\Core\Logger\LoggerChannelInterface $logger
   *   The logger factory service.
@@ -69,7 +69,7 @@ class AmazonSesHandler implements AmazonSesHandlerInterface {
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(SesClient $client, LoggerChannelInterface $logger, MessengerInterface $messenger, EventDispatcherInterface $event_dispatcher, ConfigFactoryInterface $config_factory) {
  public function __construct(SesV2Client $client, LoggerChannelInterface $logger, MessengerInterface $messenger, EventDispatcherInterface $event_dispatcher, ConfigFactoryInterface $config_factory) {
    $this->client = $client;
    $this->logger = $logger;
    $this->messenger = $messenger;
@@ -82,10 +82,12 @@ class AmazonSesHandler implements AmazonSesHandlerInterface {
   */
  public function send(Email $email) {
    try {
      $result = $this->client->sendRawEmail([
        'RawMessage' => [
      $result = $this->client->sendEmail([
        'Content' => [
          'Raw' => [
            'Data' => $email->toString(),
          ],
        ],
      ]);

      $event = new MailSentEvent($result['MessageId'], $email);
@@ -103,7 +105,7 @@ class AmazonSesHandler implements AmazonSesHandlerInterface {
      $this->logger->error($e->getMessage());
      return FALSE;
    }
    catch (SesException $e) {
    catch (SesV2Exception $e) {
      $this->logger->error($e->getAwsErrorMessage());
      return FALSE;
    }
@@ -116,8 +118,7 @@ class AmazonSesHandler implements AmazonSesHandlerInterface {
   *   The time to sleep in microseconds.
   */
  protected function getSleepTime() {
    $result = $this->client->getSendQuota();
    $results = $this->resultToArray($result);
    $results = $this->getSendQuota();
    $per_second = ceil(1000000 / $results['MaxSendRate']);

    return intval($per_second);
@@ -130,29 +131,30 @@ class AmazonSesHandler implements AmazonSesHandlerInterface {
    $identities = [];

    try {
      $results = $this->client->listIdentities();
      $results = $this->client->listEmailIdentities();

      foreach ($results->toArray()['Identities'] as $identity) {
        $result = $this->client->getIdentityVerificationAttributes([
          'Identities' => [$identity],
      foreach ($results['EmailIdentities'] as $emailIdentity) {
        $identity = $emailIdentity['IdentityName'];
        $result = $this->client->getEmailIdentity([
          'EmailIdentity' => $identity,
        ]);
        $attributes = $result->toArray()['VerificationAttributes'];
        $attributes = $result['DkimAttributes'];

        $domain = array_key_exists('VerificationToken', $attributes[$identity]);
        $domain = $result['IdentityType'] == 'DOMAIN';
        $item = [
          'identity' => $identity,
          'status' => $attributes[$identity]['VerificationStatus'],
          'status' => $result['VerifiedForSendingStatus'] ? 'Success' : 'Not verified',
          'type' => $domain ? 'Domain' : 'Email Address',
        ];

        if ($domain) {
          $item['token'] = $attributes[$identity]['VerificationToken'];
          $item['token'] = reset($attributes['Tokens']);
        }

        $identities[] = $item;
      }
    }
    catch (SesException $e) {
    catch (SesV2Exception $e) {
      $this->logger->error($e->getMessage());
      $this->messenger->addError($this->t('Unable to list identities.'));
    }
@@ -163,28 +165,18 @@ class AmazonSesHandler implements AmazonSesHandlerInterface {
  /**
   * {@inheritdoc}
   */
  public function verifyIdentity($identity, $type) {
    switch ($type) {
      case 'domain':
        $this->client->verifyDomainIdentity([
          'Domain' => $identity,
        ]);
        break;

      case 'email':
        $this->client->verifyEmailIdentity([
          'EmailAddress' => $identity,
  public function verifyIdentity($identity) {
    $this->client->createEmailIdentity([
      'EmailIdentity' => $identity,
    ]);
        break;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteIdentity($identity) {
    $this->client->deleteIdentity([
      'Identity' => $identity,
    $this->client->deleteEmailIdentity([
      'EmailIdentity' => $identity,
    ]);
  }

@@ -192,35 +184,9 @@ class AmazonSesHandler implements AmazonSesHandlerInterface {
   * {@inheritdoc}
   */
  public function getSendQuota() {
    $result = $this->client->getSendQuota();
    $results = $this->resultToArray($result);

    return array_map('number_format', $results);
  }

  /**
   * {@inheritdoc}
   */
  public function getSendStatistics() {
    $statistics = [
      'DeliveryAttempts' => 0,
      'Bounces' => 0,
      'Complaints' => 0,
      'Rejects' => 0,
    ];

    $result = $this->client->getSendStatistics();
    $results = $this->resultToArray($result);

    foreach ($results['SendDataPoints'] as $data) {
      unset($data['Timestamp']);

      foreach ($data as $key => $value) {
        $statistics[$key] += (int) $value;
      }
    }
    $result = $this->client->getAccount();

    return array_map('number_format', $statistics);
    return array_map('number_format', $result['SendQuota']);
  }

  /**
+1 −11
Original line number Diff line number Diff line
@@ -33,10 +33,8 @@ interface AmazonSesHandlerInterface {
   *
   * @param string $identity
   *   The identity to verify.
   * @param string $type
   *   The type of the identity, domain or email.
   */
  public function verifyIdentity($identity, $type);
  public function verifyIdentity($identity);

  /**
   * Delete a verified identity.
@@ -54,12 +52,4 @@ interface AmazonSesHandlerInterface {
   */
  public function getSendQuota();

  /**
   * Get sending statistics.
   *
   * @return array
   *   An array of statistics.
   */
  public function getSendStatistics();

}
+0 −30
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ class AmazonSesController extends ControllerBase {
   */
  public function statistics() {
    $quota = $this->handler->getSendQuota();
    $statistics = $this->handler->getSendStatistics();

    return [
      'quota' => [
@@ -52,35 +51,6 @@ class AmazonSesController extends ControllerBase {
            emails/second', ['@send_rate' => $quota['MaxSendRate']]),
        ],
      ],
      'statistics' => [
        '#type' => 'details',
        '#title' => $this->t('Sending statistics'),
        '#open' => TRUE,
        'sent' => [
          '#markup' => $this->t('<strong>Sent:</strong> @sent', [
            '@sent' => $statistics['DeliveryAttempts'],
          ]) . '<br />',
        ],
        'bounces' => [
          '#markup' => $this->t('<strong>Bounces:</strong> @bounces', [
            '@bounces' => $statistics['Bounces'],
          ]) . '<br />',
        ],
        'complaints' => [
          '#markup' => $this->t('<strong>Complaints:</strong> @complaints', [
            '@complaints' => $statistics['Complaints'],
          ]) . '<br />',
        ],
        'rejected' => [
          '#markup' => $this->t('<strong>Rejected:</strong> @rejected', [
            '@rejected' => $statistics['Rejects'],
          ]),
        ],
        'description' => [
          '#markup' => '<p>' . $this->t('Sending statistics are compiled
            over the previous two weeks.') . '</p>',
        ],
      ],
    ];
  }

Loading