Lemon Kazi
Lemon Kazi

Reputation: 3311

lambda function email not sending through nodeJS

After checking some tutorial I tried to implement a nodeJS function. And after compiling that code I didn't find any error and it's passing email sent message. But after checking my inbox I didn't find any email. Please suggest me where I did mistake.

console.log('Version 0.1.3');

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

var ses = new aws.SES();
var s3 = new aws.S3();

exports.handler = function (event, context) {

console.log("Event: " + JSON.stringify(event));

// Check required parameters
if (event.email == null) {
    context.fail('Bad Request: Missing required member: email');
    return;
}

var config = require('./config.js');

// Make sure some expected results are present
if (event.name == null) {
    event.name = event.email;
}

// Make sure we have a subject.
// If the event didn't include it, then
// pull it from the configuration.
// If we still don't have a subject, then
// just make one up.
if (event.subject == null) {
    event.subject = config.defaultSubject;

    if (event.subject == null) {
        event.subject = "Mail from {{name}}";
    }
}

console.log('Loading template from ' + config.templateKey + ' in ' + config.templateBucket);

// Read the template file
s3.getObject({
    Bucket: config.templateBucket, 
    Key: config.templateKey
}, function (err, data) {
    if (err) {
        // Error
        console.log(err, err.stack);
        context.fail('Internal Error: Failed to load template from s3.')
    } else {
        var templateBody = data.Body.toString();
        console.log("Template Body: " + templateBody);

        // Convert newlines in the message
        if (event.message != null) {
            event.message = event.message
            .replace("\r\n", "<br />")
            .replace("\r", "<br />")
            .replace("\n", "<br />");
        }

        // Perform the substitutions        
        var subject = event.subject;
        console.log("Final subject: " + subject);
        var message = templateBody;
        console.log("Final message: " + message);

        var params = {
            Destination: {
                ToAddresses: [
                    config.targetAddress
                ]
            },
            Message: {

                Subject: {
                    Data: subject,
                    Charset: 'UTF-8'
                }
            },
            Source: config.fromAddress,
            ReplyToAddresses: [
                event.name + '<' + event.email + '>'
            ]
        };

        var fileExtension = config.templateKey.split(".").pop();
        if (fileExtension.toLowerCase() == 'html') {
            params.Message.Body = {
                Html: {
                    Data: message,
                    Charset: 'UTF-8'
                }
            };
        } else if (fileExtension.toLowerCase() == 'txt') {
            params.Message.Body = {
                Text: {
                    Data: message,
                    Charset: 'UTF-8'
                }
            };
        } else {
            context.fail('Internal Error: Unrecognized template file extension: ' + fileExtension);
            return;
        }

        // Send the email
        ses.sendEmail(params, function (err, data) {
            if (err) {
                console.log(err, err.stack);
                context.fail('Internal Error: The email could not be sent.');
            } else {
                console.log(data);           // successful response
                context.succeed('The email was successfully sent to ' + event.email);
            }
        });
    }
  });

};

This is testing json data.

{
 "email": "[email protected]",
 "name": "Customer Name",
 "message": "Hello World!\nGoing to the moon!"
}

In config.js I placed sender email and template file

"use strict";

var config = {
    "templateBucket" : "XXXXX",
    "templateKey" : "Template.html",
    "targetAddress" : "[email protected]",
    "fromAddress": "Me <[email protected]>",
    "defaultSubject" : "Email From {{Lemon}}"
};
module.exports = config;

Upvotes: 1

Views: 1430

Answers (1)

Anuruddha
Anuruddha

Reputation: 3245

The repository you pointed out hasn't mentioned about SES setup. You need to verify the email sender before being able to send emails.

Here's a quick start guide from Amazon on SES on how to do that. https://docs.aws.amazon.com/ses/latest/DeveloperGuide/quick-start.html

You should be able to send emails after verifying the sender.

Upvotes: 2

Related Questions