当使用AWS SES(Simple Email Service)发送邮件时,可能会遇到"Invalid parameter value: Local address contains illegal characters."的错误。这个错误通常表示您在发送电子邮件时使用了一个非法的本地地址。
以下是一个使用AWS SDK for Python(Boto3)解决此问题的示例代码:
import boto3
def send_email(sender, recipient, subject, body):
# 创建SES客户端
ses_client = boto3.client('ses')
# 验证邮箱地址格式
if '@' not in sender or '@' not in recipient:
raise ValueError("Invalid email address format.")
# 发送电子邮件
response = ses_client.send_email(
Source=sender,
Destination={
'ToAddresses': [
recipient,
]
},
Message={
'Subject': {
'Data': subject
},
'Body': {
'Text': {
'Data': body
}
}
}
)
# 输出响应信息
print(response)
# 示例调用
send_email('sender@example.com', 'recipient@example.com', 'Test Email', 'This is a test email.')
在上面的代码示例中,我们首先创建了一个SES客户端。然后,我们验证了发送者和接收者的电子邮件地址是否符合格式要求。最后,我们使用send_email
方法发送电子邮件,并打印出响应信息。
请注意,此示例仅提供了基本的错误检查和发送电子邮件的功能。您可以根据您的需求进行修改和扩展。