以下是使用AWS SESV2向取消订阅所有主题的联系人发送重要邮件的代码示例:
import boto3
def send_email(subject, body, recipients):
ses_client = boto3.client('sesv2', region_name='us-west-2') # 根据实际情况选择合适的区域
response = ses_client.send_email(
FromEmailAddress='your-email@example.com',
Destination={
'ToAddresses': recipients
},
Content={
'Simple': {
'Subject': {
'Data': subject
},
'Body': {
'Text': {
'Data': body
}
}
}
}
)
print('Email sent! Message ID:', response['MessageId'])
def unsubscribe_all_topics():
sns_client = boto3.client('sns', region_name='us-west-2') # 根据实际情况选择合适的区域
response = sns_client.list_topics()
for topic in response['Topics']:
topic_arn = topic['TopicArn']
response = sns_client.list_subscriptions_by_topic(TopicArn=topic_arn)
for subscription in response['Subscriptions']:
if subscription['Protocol'] == 'email':
email_address = subscription['Endpoint']
unsubscribe_response = sns_client.unsubscribe(SubscriptionArn=subscription['SubscriptionArn'])
print('Unsubscribed', email_address)
# 发送重要邮件
subject = 'Important message'
body = 'This is an important message regarding your subscription.'
send_email(subject, body, [email_address])
unsubscribe_all_topics()
这段代码使用了AWS SDK for Python (Boto3)。首先,它使用boto3.client()
方法创建了一个SESv2客户端和一个SNS客户端。然后,它使用SNS客户端的list_topics()
方法列出了所有主题。接下来,它使用SNS客户端的list_subscriptions_by_topic()
方法列出了每个主题的订阅,并检查每个订阅的协议是否为电子邮件。如果是电子邮件订阅,它使用SNS客户端的unsubscribe()
方法取消订阅,并发送一封重要邮件给取消订阅的联系人。
请注意,你需要将代码中的region_name
、FromEmailAddress
和重要邮件的主题和内容替换为你自己的值。你还需要安装并配置AWS CLI,并使用aws configure
命令设置你的凭证和默认区域。
下一篇:AWS SES未收到邮件