Loading amazon_ses.services.yml +1 −1 Original line number Diff line number Diff line Loading @@ -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'] Loading src/AmazonSesClientFactory.php +3 −3 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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) { Loading @@ -56,7 +56,7 @@ class AmazonSesClientFactory { } try { $client = new SesClient($options); $client = new SesV2Client($options); return $client; } Loading src/AmazonSesHandler.php +30 −64 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -23,7 +23,7 @@ class AmazonSesHandler implements AmazonSesHandlerInterface { /** * The AWS SesClient. * * @var \Aws\Ses\SesClient * @var \Aws\SesV2\SesV2Client */ protected $client; Loading Loading @@ -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. Loading @@ -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; Loading @@ -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); Loading @@ -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; } Loading @@ -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); Loading @@ -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.')); } Loading @@ -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, ]); } Loading @@ -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']); } /** Loading src/AmazonSesHandlerInterface.php +1 −11 Original line number Diff line number Diff line Loading @@ -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. Loading @@ -54,12 +52,4 @@ interface AmazonSesHandlerInterface { */ public function getSendQuota(); /** * Get sending statistics. * * @return array * An array of statistics. */ public function getSendStatistics(); } src/Controller/AmazonSesController.php +0 −30 Original line number Diff line number Diff line Loading @@ -30,7 +30,6 @@ class AmazonSesController extends ControllerBase { */ public function statistics() { $quota = $this->handler->getSendQuota(); $statistics = $this->handler->getSendStatistics(); return [ 'quota' => [ Loading @@ -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 Loading
amazon_ses.services.yml +1 −1 Original line number Diff line number Diff line Loading @@ -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'] Loading
src/AmazonSesClientFactory.php +3 −3 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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) { Loading @@ -56,7 +56,7 @@ class AmazonSesClientFactory { } try { $client = new SesClient($options); $client = new SesV2Client($options); return $client; } Loading
src/AmazonSesHandler.php +30 −64 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -23,7 +23,7 @@ class AmazonSesHandler implements AmazonSesHandlerInterface { /** * The AWS SesClient. * * @var \Aws\Ses\SesClient * @var \Aws\SesV2\SesV2Client */ protected $client; Loading Loading @@ -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. Loading @@ -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; Loading @@ -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); Loading @@ -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; } Loading @@ -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); Loading @@ -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.')); } Loading @@ -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, ]); } Loading @@ -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']); } /** Loading
src/AmazonSesHandlerInterface.php +1 −11 Original line number Diff line number Diff line Loading @@ -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. Loading @@ -54,12 +52,4 @@ interface AmazonSesHandlerInterface { */ public function getSendQuota(); /** * Get sending statistics. * * @return array * An array of statistics. */ public function getSendStatistics(); }
src/Controller/AmazonSesController.php +0 −30 Original line number Diff line number Diff line Loading @@ -30,7 +30,6 @@ class AmazonSesController extends ControllerBase { */ public function statistics() { $quota = $this->handler->getSendQuota(); $statistics = $this->handler->getSendStatistics(); return [ 'quota' => [ Loading @@ -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