以下是一个使用AWS SNS发送OTP(一次性密码)电子邮件的代码示例:
import boto3
import random
def generate_otp():
# 生成一个6位数的随机OTP
return random.randint(100000, 999999)
def send_otp_email(email, otp):
# 创建SNS客户端
sns = boto3.client('sns')
# 发送OTP电子邮件
sns.publish(
TopicArn='your_sns_topic_arn',
Message=f'Your OTP is: {otp}',
Subject='OTP Verification',
MessageAttributes={
'AWS.SNS.SMS.SMSType': {
'DataType': 'String',
'StringValue': 'Transactional'
}
}
)
# 生成OTP
otp = generate_otp()
# 发送OTP电子邮件
send_otp_email('example@example.com', otp)
请注意,您需要替换代码中的your_sns_topic_arn
为您自己的SNS主题的ARN。此外,您还需要安装AWS SDK for Python(Boto3)并配置您的AWS凭证来使用此代码。
这段代码将生成一个6位数的随机OTP,并使用AWS SNS发送电子邮件包含OTP。电子邮件的消息正文将包含生成的OTP,并且电子邮件的主题为“OTP Verification”。