Send message via existing and configured Pinpoint project
Service limitations
There are limitations related to email channel configuration. Check this link
Lambda configuration
It should be AWS Key and Secret be passed as environment variables
PINPOINT_API_ACCESS_KEY_ID, PINPOINT_API_SECRET_ACCESS_KEY
otherwise lambda IAM should be configured with
mobiletargeting:SendMessages
https://docs.aws.amazon.com/pinpoint/latest/developerguide/permissions-actions.html
Implementation
import * as AWS from 'aws-sdk';
export async function handler(event: any) {
try {
// Configure access to Pinpoint
const pinpoint = new AWS.Pinpoint({
accessKeyId: process.env.PINPOINT_API_ACCESS_KEY_ID,
secretAccessKey: process.env.PINPOINT_API_SECRET_ACCESS_KEY});
// Configure required params that could be passed as parameters to lambda
const appId = '<your pinpoint application Id>';
const subject = 'Amazon Pinpoint test message';
const body_text = `Amazon Pinpoint Test`;
const body_html = '<html>' +
'<head></head>' +
'<body>' +
' <h1>Amazon Pinpoint Test</h1>' +
' <p>This email was sent with' +
` <a href='https://aws.amazon.com/pinpoint/'>the Amazon Pinpoint API</a> using the`+
` <a href='https://aws.amazon.com/sdk-for-node-js/'>`+
' AWS SDK for JavaScript in Node.js</a>.</p>'+
'</body>'+
'</html>`';
const charset = 'UTF-8';
const toAddress = 'to Address';
const senderAddress = 'from Address';
const params = {
ApplicationId: appId,
MessageRequest: {
Addresses: {
[toAddress]:{
ChannelType: 'EMAIL'
}
},
MessageConfiguration: {
EmailMessage: {
FromAddress: senderAddress,
SimpleEmail: {
Subject: {
Charset: charset,
Data: subject
},
HtmlPart: {
Charset: charset,
Data: body_html
},
TextPart: {
Charset: charset,
Data: body_text
}
}
}
}
}
};
// Send request
const result = await pinpoint.sendMessages(params).promise();
} catch (e) {
const errorDetails = `Unexpected exception: ${e.message} | stack: ${e.stack}`;
console.error(errorDetails);
}
}
Comments
0 comments
Please sign in to leave a comment.