Verifying email identities using the Amazon SES API and the AWS SDK for PHP Version 3
When you first start using your Amazon Simple Email Service (Amazon SES) account, all senders and recipients must be verified in the same AWS Region that you are sending emails to. For more information about sending emails, see Sending Email with Amazon SES.
The following examples show how to:
-
Verify an email address using VerifyEmailIdentity.
-
Verify an email domain using VerifyDomainIdentity.
-
List all email addresses using ListIdentities.
-
List all email domains using ListIdentities.
-
Remove an email address using DeleteIdentity.
-
Remove an email domain using DeleteIdentity.
All the example code for the AWS SDK for PHP is available here on
GitHub
Credentials
Before running the example code, configure your AWS credentials, as described in Credentials. Then import the AWS SDK for PHP, as described in Basic usage.
For more information about using Amazon SES, see the Amazon SES Developer Guide.
Verify an email addresses
Amazon SES can send email only from verified email addresses or domains. By verifying an email address, you demonstrate that you’re the owner of that address and want to allow Amazon SES to send email from that address.
When you run the following code example, Amazon SES sends an email to the address you specified. When you (or the recipient of the email) click the link in the email, the address is verified.
To add an email address to your Amazon SES account, use the VerifyEmailIdentity operation.
Imports
require 'vendor/autoload.php'; use Aws\Exception\AwsException;
Sample Code
$SesClient = new Aws\Ses\SesClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $email = 'email_address'; try { $result = $SesClient->verifyEmailIdentity([ 'EmailAddress' => $email, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }
Verify an email domain
Amazon SES can send email only from verified email addresses or domains. By verifying a domain, you demonstrate that you’re the owner of that domain. When you verify a domain, you allow Amazon SES to send email from any address on that domain.
When you run the following code example, Amazon SES provides you with a verification token. You have to add the token to your domain’s DNS configuration. For more information, see Verifying a Domain with Amazon SES in the Amazon Simple Email Service Developer Guide.
To add a sending domain to your Amazon SES account, use the VerifyDomainIdentity operation.
Imports
require 'vendor/autoload.php'; use Aws\Exception\AwsException;
Sample Code
$SesClient = new Aws\Ses\SesClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $domain = 'domain.name'; try { $result = $SesClient->verifyDomainIdentity([ 'Domain' => $domain, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }
List email addresses
To retrieve a list of email addresses submitted in the current AWS Region, regardless of verification status, use the ListIdentities operation.
Imports
require 'vendor/autoload.php'; use Aws\Exception\AwsException;
Sample Code
$SesClient = new Aws\Ses\SesClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); try { $result = $SesClient->listIdentities([ 'IdentityType' => 'EmailAddress', ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }
List email domains
To retrieve a list of email domains submitted in the current AWS Region, regardless of verification status use the ListIdentities operation.
Imports
require 'vendor/autoload.php'; use Aws\Exception\AwsException;
Sample Code
$SesClient = new Aws\Ses\SesClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); try { $result = $SesClient->listIdentities([ 'IdentityType' => 'Domain', ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }
Delete an email address
To delete a verified email address from the list of identities, use the DeleteIdentity operation.
Imports
require 'vendor/autoload.php'; use Aws\Exception\AwsException;
Sample Code
$SesClient = new Aws\Ses\SesClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $email = 'email_address'; try { $result = $SesClient->deleteIdentity([ 'Identity' => $email, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }
Delete an email domain
To delete a verified email domain from the list of verified identities, use the DeleteIdentity operation.
Imports
require 'vendor/autoload.php'; use Aws\Exception\AwsException;
Sample Code
$SesClient = new Aws\Ses\SesClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $domain = 'domain.name'; try { $result = $SesClient->deleteIdentity([ 'Identity' => $domain, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }