1
votes

I have a static form-based HTML contact page, where the user needs to drop their Name, email address, subject and message and ultimately my script will reroute all the required value via AWS API --> Lambda --> my Gmail.

So, for that, I have a verified my GMAIL account in SES. And my AWS Lambda function is as below, (here, I used the same confirmed email address for to and send to deliver the email.)

'use strict';
console.log('Loading function');
const AWS = require('aws-sdk');
const sesClient = new AWS.SES();
const sesConfirmedAddress = "[email protected]";

/**
 * Lambda to process HTTP POST for a contact form with the following body
 * {
      "email": <contact-email>,
      "subject": <contact-subject>,
      "message": <contact-message>
    }
 *
 */
exports.handler = (event, context, callback) => {
    console.log('Received event:', JSON.stringify(event, null, 2));
    var emailObj = JSON.parse(event.body);
    var params = getEmailMessage(emailObj);
    var sendEmailPromise = sesClient.sendEmail(params).promise();

    var response = {
        statusCode: 200
    };

    sendEmailPromise.then(function(result) {
        console.log(result);
        callback(null, response);
    }).catch(function(err) {
        console.log(err);
        response.statusCode = 500;
        callback(null, response);
    });
};

function getEmailMessage (emailObj) {
    var emailRequestParams = {
        Destination: {
          ToAddresses: [ sesConfirmedAddress ]  
        },
        Message: {
            Body: {
                Text: {
                    Data: emailObj.message
                }
            },
            Subject: {
                Data: emailObj.subject
            }
        },
        Source: sesConfirmedAddress,
        ReplyToAddresses: [ emailObj.email ]
    };

    return emailRequestParams;
}

Now in IAM, I have created a policy and attached to this lambda function. Then I have created API to use this function to serve my purpose.

Now when I click the TEST button in API, it gives me return code 200. which is great. But the problem is I don't see any email in my verified Gmail account.

I'm using, this as my test message,

{
      "email": [email protected],
      "subject": Test,
      "message": This is a Test message
    }

I tried also using postman and postman says everything is fine with 200 as return code. But still no email to my Gmail account. I checked in cloud watch logs everything is Green. So no idea why my verified Gmail is not receiving any kind of test message. Can anyome shed any lights here?

customized Role looks like this:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "ses:SendEmail",
                "ses:SendTemplatedEmail",
                "ses:SendRawEmail"
            ],
            "Resource": "*"
        }
    ]
}
2
Found the problem, there is no problem. It's was dumping in the SPAM folder.change198

2 Answers

1
votes

I would you recommend two things, first try to deploy your code using serverless framework which can help you with the dependencies like IAM roles.

Second, As an alternative and complementary steps you may use the following reference from AWS:

  1. Open the Amazon SES console at https://console.aws.amazon.com/ses/.
  2. In the column on the left, choose either Email Addresses or Domains.
  3. Select a verified email address or domain, and then choose Send a

  4. Test Email. For To:, type [email protected]. For Subject and Body, type some sample text. Choose Send Test Email.

  5. Repeat steps 3 and 4 to create another test message, but this time, for To:, type [email protected].

  6. Open the Amazon SQS console at https://console.aws.amazon.com/sqs/. The Messages Available column should indicate that 2 messages are available.
1
votes

can you print the complete response to see if you get message id in the response ? if so, there seems to be no problem with the permission. 1. You can enable SNS notification for Bounce/Delivery/complaint for the verified identity in SES console and see if you receive any notification after making sendmail api call. https://docs.aws.amazon.com/ses/latest/DeveloperGuide/notifications-via-sns.html

  1. Additionally, also check recipient spam box.