AWS – Using SES to Send Email within a Lambda Function

Step 1: Click on the Lambda function to figure out the Lambda execution role

Step 2: Create a policy to grant permission to SES

Step 3: Attach the newly created policy to the Lambda execution role

Step 4: Lambda function code

var AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-1'});
exports.handler = (event, context, callback) => {
    var params = {
        Destination: {
            ToAddresses: [
                event.request.userAttributes.email
            ]
        },
        Message: {
            Body: {
                Html: {
                    Charset: "UTF-8",
                    Data: `TEST EMAIL BODY`
                }
            },
            Subject: {
                Charset: 'UTF-8',
                Data: 'TEST EMAIL SUBJECT'
            }
        },
        Source: 'john@example.com',
        ReplyToAddresses: [
            'no-reply@example.com'
        ]
    };

// Create the promise and SES service object
    var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();

// Handle promise's fulfilled/rejected states
    sendPromise.then(
        function(data) {
            console.log(data.MessageId);
        }).catch(
        function(err) {
            console.error(err, err.stack);
        });

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *