1

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;
4
  • check the spam folder as well Commented Jan 6, 2018 at 17:19
  • @PranavCBalan Yes I already checked that. I followed this git repository to build my system in AWS lambda github.com/eleven41/aws-lambda-send-ses-email Commented Jan 6, 2018 at 17:27
  • Did you check if your SES resource is in sandbox mode? Commented Jan 6, 2018 at 23:26
  • @TomMelo Tx for your comment. I am trying but not sure from where I can check that. Commented Jan 7, 2018 at 4:55

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes you are right. There were email verification required. I applied and email sent :) . Can you give me anoyher suggestion how can I use API to integrate this in my project contact form?
You only need to confirm the sender which is a onetime task. There is no need to verify the receiver.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.