如何在通过 Amazon SES 发送之前检查电子邮件是否有效

问题描述 投票:0回答:4

我刚开始实施 Amazon Web 服务。我正在实现一个用于从队列发送批量电子邮件的应用程序。在发送之前,我必须检查电子邮件并从队列中删除未验证的电子邮件。

我的问题是:亚马逊有什么方法可以检查电子邮件是否有效?

amazon-web-services amazon-ses
4个回答
8
投票

从你的问题来看,不清楚你是否想要:
1-避免向格式错误的电子邮件地址发送消息;或
2-避免将消息发送到未在您的 AWS 账户下验证的电子邮件地址。

1 的答案在论坛、SO 等中以不同的形式传播。您要么简单地做,即制作一个简短而清晰的正则表达式来验证大约 80% 的情况,要么您使用非常复杂的正则表达式(为了验证是否完全合规——祝你好运,检查这个例子),检查域是否不仅有效而且正常运行,最后但同样重要的是,检查帐户在该域下是否有效.由你决定。我会选择一个简单的正则表达式。

2 的答案可在 Verifying Email Addresses in Amazon SES -- Amazon SES API 和 SDK 支持以下操作,因此在任何情况下都应包括在内:

使用亚马逊 SES API

您还可以使用 Amazon SES API 管理经过验证的电子邮件地址。可以执行以下操作:

验证电子邮件身份
列表身份
删除身份
获取身份验证属性

注意
上述 API 操作优于以下较旧的 API 操作,后者自 2012 年 5 月 15 日发布的域验证起已弃用。

验证电子邮件地址
列出已验证的电子邮件地址
删除已验证的电子邮件地址

您可以使用这些 API 操作编写用于电子邮件地址验证的自定义前端应用程序。有关与电子邮件验证相关的 API 操作的完整描述,请转至 Amazon Simple Email Service API Reference。


8
投票

您可以使用“getIdentityVerificationAttributes”操作来检查电子邮件是否有效。您可以如下所示使用它:

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
});

响应将是:

{ ResponseMetadata: { RequestId: '7debf2356-ddf94-1dsfe5-bdfeb-efsdfb5b653' },
  VerificationAttributes: 
   { '[email protected]': { VerificationStatus: 'Pending' },
     '[email protected]': { VerificationStatus: 'Success' } } } 

如果有一个电子邮件 ID 以前没有为电子邮件验证请求发送过,那么 'VerificationAttributes' 对象中没有密钥。


1
投票
AmazonSimpleEmailServiceClient ses= new AmazonSimpleEmailServiceClient(credentials);

    List lids = ses.listIdentities().getIdentities();
    if (lids.contains(address)) {
        //the address is verified so            
          return true;
    }

0
投票

基于@Viccari 的回答和以上回答,考虑到没有人提供完整的代码片段来完成 OP 获取经过验证的电子邮件身份 删除它们的任务:

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(...)
'''

(是的,这是一个老问题,但希望它现在可以为某人节省更多时间)

AWS SES boto3 文档

© www.soinside.com 2019 - 2024. All rights reserved.