如何在Node.js及Lambda调用Amazon Pinpoint邮件时添加自定义请求头
Answer to Your Amazon Pinpoint Email Questions
1. Can you add custom headers like an EmailID to Amazon Pinpoint emails?
Absolutely! While you can't add arbitrary HTTP request headers directly to the Pinpoint API call itself, you can include custom email headers in the outgoing message payload. These headers will be embedded in the actual email sent to recipients, perfect for tracking identifiers like your requested EmailID.
2. How to add custom headers in Node.js
Since you’re already using pinpoint.sendMessages, here’s a step-by-step implementation tailored for your Lambda + JSON forwarding workflow:
Using AWS SDK v2 (callback-style, matching your existing code)
const AWS = require('aws-sdk'); // Initialize Pinpoint client (Lambda will use the execution role's permissions automatically) const pinpoint = new AWS.Pinpoint({ region: 'us-east-1' }); // Replace with your region exports.handler = (event) => { // Parse the incoming JSON payload from your trigger const incomingData = JSON.parse(event.body); const params = { ApplicationId: 'YOUR_PINPOINT_APP_ID', // Replace with your Pinpoint application ID MessageRequest: { Addresses: { // Map recipients from your JSON payload, or hardcode as needed [incomingData.recipientEmail]: { ChannelType: 'EMAIL' } }, MessageConfiguration: { EmailMessage: { FromAddress: 'sender@your-verified-domain.com', // Must be verified in Pinpoint/SES SimpleEmail: { Subject: { Data: incomingData.emailSubject || 'Default Subject' }, HtmlPart: { Data: incomingData.emailHtmlContent || '<p>Default email content</p>' }, TextPart: { Data: incomingData.emailTextContent || 'Default email content' } }, // Add your custom headers right here Headers: { 'X-Email-ID': incomingData.emailID, // Pull the ID from your JSON payload 'X-Source': 'Lambda-Forwarded' // Add any other custom headers you need } } } } }; // Send the email with callback handling pinpoint.sendMessages(params, (err, data) => { if (err) { console.error('Failed to send email:', err); // Add your Lambda error response logic here } else { console.log('Email sent successfully:', data.MessageResponse); // Add your Lambda success response logic here } }); };
Using AWS SDK v3 (modern async/await approach)
If you’re open to updating to the newer SDK version, here’s a cleaner promise-based implementation:
const { PinpointClient, SendMessagesCommand } = require('@aws-sdk/client-pinpoint'); const pinpoint = new PinpointClient({ region: 'us-east-1' }); exports.handler = async (event) => { const incomingData = JSON.parse(event.body); const sendEmailCommand = new SendMessagesCommand({ ApplicationId: 'YOUR_PINPOINT_APP_ID', MessageRequest: { Addresses: { [incomingData.recipientEmail]: { ChannelType: 'EMAIL' } }, MessageConfiguration: { EmailMessage: { FromAddress: 'sender@your-verified-domain.com', SimpleEmail: { Subject: { Data: incomingData.emailSubject }, HtmlPart: { Data: incomingData.emailHtmlContent }, TextPart: { Data: incomingData.emailTextContent } }, Headers: { 'X-Email-ID': incomingData.emailID } } } } }); try { const response = await pinpoint.send(sendEmailCommand); return { statusCode: 200, body: JSON.stringify({ message: 'Email sent', messageId: response.MessageResponse.Result[incomingData.recipientEmail].MessageId }) }; } catch (err) { console.error('Email error:', err); return { statusCode: 500, body: JSON.stringify({ error: err.message }) }; } };
3. Key Tips for Using Amazon Pinpoint API to Send Emails
- Verified Identities: Your sender email/domain must be verified in Amazon Pinpoint (or Amazon SES, since Pinpoint relies on SES for email delivery). This ensures your emails don’t get flagged as spam.
- IAM Permissions: Make sure your Lambda execution role has the
pinpoint:SendMessagespermission. Attach a policy like this:{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "pinpoint:SendMessages", "Resource": "arn:aws:pinpoint:YOUR_REGION:YOUR_ACCOUNT_ID:apps/YOUR_PINPOINT_APP_ID/*" } ] } - Response Handling: The
SendMessagesresponse includes aMessageResponseobject with delivery status for each recipient. Use this to track failed sends or log successful deliveries. - Header Rules: Avoid using reserved email headers (like
From,To, orSubject) as custom headers, and ensure your header names/values follow RFC standards.
内容的提问来源于stack exchange,提问作者user3795493
相关产品推荐
相关产品推荐

