0

I'm using @aws-sdk/client-pinpoint to send an email to a verified user.

async sendEmail(body: any): Promise<void> {
        const fromAddress = 'test@domain.com';
        const toAddress = 'test@domain.com';
        const projectId = 'XXX-XXXX-XXXX';
        const subject = 'Amazon Pinpoint Test (AWS SDK for JavaScript in Node.js)';

        const body_text = `Amazon Pinpoint Test (SDK for JavaScript in Node.js)`;

        const charset = 'UTF-8';
        const params = {
            ApplicationId: projectId,
            MessageRequest: {
                Addresses: {
                    [toAddress]: {
                        ChannelType: 'EMAIL',
                    },
                },
                MessageConfiguration: {
                    EmailMessage: {
                        FromAddress: fromAddress,
                        SimpleEmail: {
                            Subject: {
                                Charset: charset,
                                Data: subject,
                            },
                            HtmlPart: {
                                Charset: charset,
                                Data: 'body_html',
                            },
                            TextPart: {
                                Charset: charset,
                                Data: body_text,
                            },
                        },
                    },
                },
            },
        };

        try {
            const data = await this.pinpointClient.send(new SendMessagesCommand(params));

            const { MessageResponse } = data;

            if (!MessageResponse || !MessageResponse.Result) throw Error('Failed!');

            const recipientResult = MessageResponse?.Result[toAddress];

            if (recipientResult.StatusCode !== 200) {
                throw new Error(recipientResult.StatusMessage);
            } else {
                console.log(recipientResult.MessageId);
            }
        } catch (err) {
            console.log(err.message);
        }
    }

And everything is working fine. But when I try to use a pre-defined template, it is not being send for some reason and no errors were shown as well! I'm lost on how to pass template Name/ARN with substitution. Any idea on how to achieve that?

Cheers!

1 Answers1

0

use Template Configuration in message configuration

TemplateConfiguration: { EmailTemplate: { 'Name': 'template name', 'Version': 'latest' } }

async sendEmail(body: any): Promise<void> {
            const fromAddress = 'test@domain.com';
            const toAddress = 'test@domain.com';
            const projectId = 'XXX-XXXX-XXXX';
            const subject = 'Amazon Pinpoint Test (AWS SDK for JavaScript in Node.js)';
        
            const body_text = `Amazon Pinpoint Test (SDK for JavaScript in Node.js)`;
        
            const charset = 'UTF-8';
            const params = {
                ApplicationId: projectId,
                MessageRequest: {
                    Addresses: {
                        [toAddress]: {
                            ChannelType: 'EMAIL',
                        },
                    },
                    MessageConfiguration: {
                        EmailMessage: {
                            FromAddress: fromAddress,
                            SimpleEmail: {
                                Subject: {
                                    Charset: charset,
                                    Data: subject,
                                },
                                HtmlPart: {
                                    Charset: charset,
                                    Data: 'body_html',
                                },
                                TextPart: {
                                    Charset: charset,
                                    Data: body_text,
                                },
                            },
                        },
                    },
                    TemplateConfiguration: {
                        EmailTemplate: {
                            'Name': 'template name',
                            'Version': 'latest'
                        }
                    }
                },
            };
        
            try {
                const data = await this.pinpointClient.send(new SendMessagesCommand(params));
        
                const { MessageResponse } = data;
        
                if (!MessageResponse || !MessageResponse.Result) throw Error('Failed!');
        
                const recipientResult = MessageResponse?.Result[toAddress];
        
                if (recipientResult.StatusCode !== 200) {
                    throw new Error(recipientResult.StatusMessage);
                } else {
                    console.log(recipientResult.MessageId);
                }
            } catch (err) {
                console.log(err.message);
            }
        }