在Amazon SES发送邮件时,如果模板中的变量没有被替换为实际的值,则邮件收件人将收到带有未替换变量的电子邮件。要解决这个问题,需要确保替换模板中的变量。以下是示例代码,可用于替换变量:
import boto3
def send_email():
# Create a new SES resource and specify your AWS Region
client = boto3.client('ses', region_name='us-west-2')
# The email body for recipients with non-HTML email clients
text_body = "Hello,\r\nPlease see the attached file for a list of customers to contact."
# The HTML body of the email
html_body = """
Hello!
Please see the attached file for a list of customers to contact.
Here are the values that you wanted to replace:
Variable
Value
{{#values}}
{{variable}}
{{value}}
{{/values}}
"""
# Replace template variables with actual values
values = [{'variable': 'name', 'value': 'John'}, {'variable': 'phone', 'value': '123-456-7890'}]
for value in values:
html_body = html_body.replace('{{' + value['variable'] + '}}', value['value'])
# The email subject
subject = 'Test Email'
# The email body
body = {'Html': {'Charset': 'UTF-8', 'Data': html_body},
'Text': {'Charset': 'UTF-8', 'Data': text_body}}
# The email message
message = {'Body': body, 'Subject': {'Charset': 'UTF-8', 'Data': subject}}