如何为AWS SES邮件模板配置多行长HTML内容?
Hey there, I've run into this exact issue before when setting up SES templates with longer HTML content. The key thing to catch first: your example snippet has HtmlPart as an array, but AWS SES requires this to be a single string value. Once you fix that, adding full-length HTML is straightforward — here are the best approaches depending on your workflow:
1. Use Native Multi-Line Strings (Recommended for Code)
Most modern languages support multi-line string literals that let you write your HTML exactly as you would in a .html file, without manual escaping. This is way easier to maintain than squishing everything into one line.
Example with Python (Boto3)
import boto3 # Initialize SES client ses_client = boto3.client('ses', region_name='us-east-1') # Write your full HTML content with proper structure, styles, and template variables invite_html = """ <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Team Invitation</title> <style> .container { max-width: 600px; margin: 0 auto; padding: 20px; font-family: Arial, sans-serif; } .header { background: #2385bb; color: white; padding: 15px; text-align: center; } .body { margin-top: 20px; line-height: 1.6; } .cta-button { display: inline-block; padding: 10px 20px; background: #2385bb; color: white; text-decoration: none; border-radius: 4px; } </style> </head> <body> <div class="container"> <div class="header"> <h2>You’re Invited to Join {{group_name}}!</h2> </div> <div class="body"> <p>Hi {{recipient_name}},</p> <p>{{who}} has invited you to become part of our team. We’re excited to have you on board!</p> <p>Click below to accept your invitation:</p> <a href="{{invite_link}}" class="cta-button">Join the Team</a> <p>If you didn’t request this, feel free to ignore this email.</p> </div> </div> </body> </html> """ # Define your template structure template_params = { "Template": { "TemplateName": "Group_Invitation", "SubjectPart": "{{who}} has invited you to join team {{group_name}}", "TextPart": "Hi {{recipient_name}}, {{who}} invited you to join team {{group_name}}. Accept here: {{invite_link}}", "HtmlPart": invite_html # Pass the multi-line string directly } } # Create the template response = ses_client.create_template(**template_params) print(f"Template created successfully: {response['ResponseMetadata']['HTTPStatusCode']}")
Example with Node.js (AWS SDK v3)
import { SESClient, CreateTemplateCommand } from "@aws-sdk/client-ses"; const client = new SESClient({ region: "us-east-1" }); const inviteHtml = ` <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Team Invitation</title> <style> /* Add your styles here */ </style> </head> <body> <!-- Full HTML content with template variables --> </body> </html> `; const command = new CreateTemplateCommand({ Template: { TemplateName: "Group_Invitation", SubjectPart: "{{who}} has invited you to join team {{group_name}}", TextPart: "Hi {{recipient_name}}, {{who}} invited you to join team {{group_name}}. Accept here: {{invite_link}}", HtmlPart: inviteHtml } }); try { const response = await client.send(command); console.log("Template created:", response); } catch (err) { console.error(err); }
2. Use a File for HTML (Great for Large Templates)
If your HTML is super long or you want to edit it separately, read the content from a .html file and pass it into the SES template.
Python Example with File Input
# Read HTML from a file with open("invite_template.html", "r") as f: invite_html = f.read() # Then use the same template_params as above, passing invite_html to HtmlPart
AWS CLI Example with File Input
Create a template.json file (make sure HtmlPart is a string, not an array):
{ "Template": { "TemplateName": "Group_Invitation", "SubjectPart": "{{who}} has invited you to join team {{group_name}}", "TextPart": "Hi {{recipient_name}}, {{who}} invited you to join team {{group_name}}. Accept here: {{invite_link}}", "HtmlPart": "<!doctype html><html>...</html>" } }
Then run:
aws ses create-template --cli-input-json file://template.json
Key Notes to Avoid Errors
- No Arrays for HtmlPart: SES will throw an error if you pass an array here — it must be a single string.
- Escape Where Necessary: If you’re writing HTML directly in a JSON file (not using code), make sure to escape double quotes (
") as\"and newlines as\n. Using multi-line strings in code avoids this manual work. - Include TextPart: Always add a plain-text version of your email for compatibility with email clients that don’t support HTML.
内容的提问来源于stack exchange,提问作者Allen Tian

