可以使用AWS SDK for Python(Boto3)将电子邮件发送到SNS主题。
以下是示例代码:
import boto3
# Create an SNS client
sns = boto3.client('sns')
# Create a topic with name 'MyTopic'
response = sns.create_topic(Name='MyTopic')
# Get the topic ARN (Amazon Resource Name)
topic_arn = response['TopicArn']
# Subscribe an email address to the topic
response = sns.subscribe(
TopicArn=topic_arn,
Protocol='email',
Endpoint='email@example.com'
)
# Print out the subscription ARN
print(response['SubscriptionArn'])
# Send an email to the topic with subscriptions pending confirmation
response = sns.publish(
TopicArn=topic_arn,
Message='Hello World!',
Subject='Test message'
)
# Print out the message ID
print(response['MessageId'])
使用此代码,您可以创建一个名为“MyTopic”的主题并将电子邮件地址“email@example.com”订阅到该主题。然后,您可以使用“sns.publish”方法向主题发送电子邮件。如果有等待确认的订阅,电子邮件将发送到这些地址以进行确认。