Articles → NODE.JS → Send An Email By Calling Amazon SES Using Node.Js

Send An Email By Calling Amazon SES Using Node.Js






Steps




  1. Generate access and secret keys of the user.
  2. Verify email addresses (sender and receiver) in SES.
  3. Add user credential in the "credentials" file.
  4. Write code to send an email.
  5. Output.

Generate Access And Secret Keys Of The User





Verify Email Addresses (Sender And Receiver) In SES




Picture showing verifying the sender and recipient email addresss
Click to Enlarge




Add User Credential In The “Credentials” File




c:\users\<username>\.aws\credentials




Picture showing credentials file for storing credentials
Click to Enlarge


Write Code To Send An Email




npm install aws-sdk




var aws = require('aws-sdk');

aws.config.getCredentials(function (err) {
    if (err) console.log(err.stack);    
    else {
        //console.log("Access key:", aws.config.credentials.accessKeyId);
    }
});

var ses = new aws.SES({
    region: 'ap-south-1'
});

handler = function () {
    //console.log("Incoming: ", event);
    var eParams = {
        Destination: {
            ToAddresses: ["recipient@gmail.com"]
        },
        Message: {
            Body: {
                Text: {
                    Data: "Lambda is working"
                }               
            },
            Subject: {
                Data: "Mail From SES"
            }

        },
        Source: "sender@gmail.com"
    };
    console.log('===Sending Email====');
    var email = ses.sendEmail(eParams, function (err, data) {

        //console.log(eParams);
        if (err) console.log(err);
        else {
            console.log('===Email Sent====');
            //context.succeed(event);
        }
    });
};

handler();



Output




Picture showing the email sent through SES using node.js
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Monday, November 29, 2021

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250