ses-lambda-send-email
ses-lambda-send-email

In this tutorial, we are going to learn “How to use Amazon AWS SES service to send emails using nodejs/lambda functions”. SES stands for Simple Email Service to send emails using Amazon service. AWS Lambda is another service provided by Amazon to write cloud functions. These function can be invoked with the help of aws api gateway(HTTP calls). This is the step by step tutorial for beginners to understand the actual setup of SES with lambda. Let’s get started by “send emails using SES, lambda, Nodejs” tutorial.

 

Following are the steps to implement AWS SES with lambda.

  1. Make sure you have Amazon AWS account. Sign in to AWS console.
  2. Goto SES service to add email and verify it. After verifying email, it will be the source email to send emails using SES.
  3. Goto lambda service
  4. Create lambda function i.e sendEmail
    var aws = require('aws-sdk');
    var ses = new aws.SES();
    
    exports.handler = (event, context, callback) => {
        // TODO implement
    
        console.log("Incoming: ", event);
    
        var eParams = {
            Destination: {
                ToAddresses: ["email@email.com"]
            },
            Message: {
                Body: {
                    Text: {
                        Data: "asdfasdf"
                    }
                },
                Subject: {
                    Data: "Ses Test Email"
                }
            },
            Source: "source@gmail.com"
        };
    
        console.log('===SENDING EMAIL===');
        var email = ses.sendEmail(eParams, function (err, data) {
            callback(null, {err: err, data: data});
            if (err) {
                console.log(err);
                context.fail(err);
            } else {
                console.log("===EMAIL SENT===");
                console.log("EMAIL CODE END");
                console.log('EMAIL: ', email);
                console.log(data);
                context.succeed(event);
            }
        });
    };
  5. Give permission to lambda function to use SES service for sending emails.
    Goto IAM service -> Policies -> search for ‘AmazonSESFullAccess‘ -> select and attach to project
  6. After creating lambda function with SES permission, we are ready to invoke this function.
    Invoke sendEmail lambda function from another lambda function
    var AWS = require('aws-sdk')
    
    AWS.config.update({
      accessKeyId: 'myAccessKey',
      secretAccesskey: 'mySecretKey',
      region: 'eu-west-1' 
    });
    
    var lambda = new AWS.Lambda();
    
    var params = {
        FunctionName: 'sendEmail', // the lambda function we are going to invoke
        InvocationType: 'RequestResponse',
        LogType: 'Tail',
        Payload: '{ "name" : "Alex" }'
    };
    
    lambda.invoke(params, function (err, data) {
        if (err) {
            console.log(err);
        } else {
            console.log(data.Payload); //Response from sendEmail functions
        }
    });

    Please comment if you have any issues while implementation.

You have already voted.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.