我正在尝试像这里一样实现营销电子邮件订阅https://docs.aws.amazon.com/ses/latest/dg/sending-email-subscription-management.html
我正在尝试一个节点 lambda 函数的最小示例:
这是我的代码
import { SESClient, SendEmailCommand } from "@aws-sdk/client-ses";
const sesClient = new SESClient({ region: "us-east-1" });
export const handler = async (event, context) => {
// Define email parameters
const params = {
Source: '[email protected]', // verified in SES
Destination: {
ToAddresses: [
'[email protected]' // Recipient email address
]
},
Message: {
Subject: {
Data: 'Test HTML Email from AWS SES'
},
Body: {
Html: {
Data: '<h1>Welcome</h1><p>This is a test HTML email sent using AWS SES from a Lambda function. Click <a href="{{amazonSESUnsubscribeUrl}}">here</a> {{amazonSESUnsubscribeUrl}}</p>'
}
},
Headers: [
{
"Name": "List-Unsubscribe",
"Value": "[email protected]"
},
{
"Name": "List-Unsubscribe-Post",
"Value": "https://kjhkjh.com"
},
{
"Name": "ListManagementOptions",
"Value": true
}
],
ListManagementOptions: {
ContactListName: 'MarketingEmails', // Specify your contact list name here
TopicName: 'Topic' // Specify your topic name here
}
}
};
try {
// Send the email using SES
const data = await sesClient.send(new SendEmailCommand(params));
console.log("Email sent!", data);
return data; // Return success response
} catch (err) {
console.error("Failed to send email.", err);
return err; // Return error response
}
};
我通过命令行创建了主题
aws sesv2 create-contact-list --cli-input-json file://contact_list.json
当我测试它时,我收到一封电子邮件,但链接未填充(而是空白)。我在这里呆了几个小时,却不知所措。有人有这方面的经验吗
解决方案是使用 Amazon SES API v2。因此,为此,您需要将
import
语句更新为以下内容:
import { SESv2Client, SendEmailCommand } from "@aws-sdk/client-sesv2";
const sesClient = new SESv2Client({ region: "us-east-1" });
您还需要根据以下链接中给出的新 API 规范更新 params 对象:
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/sesv2/command/SendEmailCommand/