9

I am fresh in implementing Amazon Web services. I am working on implementing an application for sending bulk emails from a queue. I have to check emails and remove non-verified emails from the queue before sending.

My question is: Is there any method available in Amazon to check whether emails are valid or not?

Arafat Nalkhande
  • 11,078
  • 9
  • 39
  • 63
Kavikant Singh
  • 93
  • 1
  • 1
  • 4
  • You can use regular expression to validate an email address before adding it in queue or sending mail on that address. – Pawan Sharma Feb 21 '13 at 13:13

4 Answers4

9

You can use the "getIdentityVerificationAttributes" operation to check whether emails are valid or not. You can use this as shown below:

var params = {
    Identities: arr // It is a required field (array of strings).
};
ses.getIdentityVerificationAttributes(params, function(err, data) {
    if(err)
        console.log(err, err.stack); // an error occurred
    else
        console.log(data);           // successful response
});

And the Response will be:

{ ResponseMetadata: { RequestId: '7debf2356-ddf94-1dsfe5-bdfeb-efsdfb5b653' },
  VerificationAttributes: 
   { 'abc@gmail.com': { VerificationStatus: 'Pending' },
     'xyz@gmail.com': { VerificationStatus: 'Success' } } } 

If there is an email-id which is not sent previously for email verification request, then there is no key present in 'VerificationAttributes' object.

Tanmay Verma
  • 263
  • 1
  • 3
  • 12
8

From your question, it is not clear whether you want to:
1-avoid sending messages to malformed email addresses; or
2-avoid sending messages to email addresses which are not verified under your AWS account.

The answer for 1 is spread in different forms accross forums, SO, etc. You either do it simple, i.e., craft a short and clear regular expression which validates roughly 80% of the cases, or you use a very complex regular expression (in order to validate against the full compliance -- good luck, check this example), check whether the domain is not only valid but also up and running, and, last but not least, check if the account is valid under that domain. Up to you. I'd go with a simple regex.

The answer for 2 is available at Verifying Email Addresses in Amazon SES -- the Amazon SES API and SDKs support the operations below, so you should be covered in any case:

Using the Amazon SES API

You can also manage verified email addresses with the Amazon SES API. The following actions are available:

VerifyEmailIdentity
ListIdentities
DeleteIdentity
GetIdentityVerificationAttributes

Note
The API actions above are preferable to the following older API actions, which are deprecated as of the May 15, 2012 release of Domain Verification.

VerifyEmailAddress
ListVerifiedEmailAddresses
DeleteVerifiedEmailAddress

You can use these API actions to write a customized front-end application for email address verification. For a complete description of the API actions related to email verification, go to the Amazon Simple Email Service API Reference.

Viccari
  • 9,029
  • 4
  • 43
  • 77
2
AmazonSimpleEmailServiceClient ses= new AmazonSimpleEmailServiceClient(credentials);

    List lids = ses.listIdentities().getIdentities();
    if (lids.contains(address)) {
        //the address is verified so            
          return true;
    }
user3165739
  • 166
  • 2
  • 3
0

Building on @Viccari 's answer and above answers considering that none provide a full code snippet to accomplish the OP's tasks of getting verified email identities and deleting them:

import boto3

#establish ses client
ses_client = boto3.client('ses')

#get email identities
identities = ses_client.list_identities(
    IdentityType='EmailAddress', 
    NextToken='',
    MaxItems=123
)['Identities']

#get email verification statuses of identities
response = ses_client.get_identity_verification_attributes(
    Identities=identities
)['VerificationAttributes']


ver_emails = []
for email in response:
    if response[email]['VerificationStatus'] == 'Success':
        ver_emails.append(email)
    else:
        ses_client.delete_identity(
            Identity=email
        )

'''
Do stuff with verified emails...
ses_client.send_email(...)
'''

(yes this is an old question but hopefully it can save someone even more time now)

AWS SES boto3 docs