3

I'm trying to use the service on a in order to send an email to verified recipients.

I'm following this simple tutorial which shows a simple function:

var aws = require('aws-sdk');
var ses = new aws.SES({region: 'us-east-1'});

exports.handler = (event, context, callback) => {

     var params = {
        Destination: {
            ToAddresses: ["recipientEmailAddress"]
        },
        Message: {
            Body: {
                Text: { Data: "Test" }
            },
            Subject: { Data: "Test Email" }
        },
        Source: "sourceEmailAddress"
    };

     ses.sendEmail(params, function (err, data) {
        callback(null, {err: err, data: data});
        if (err) {
            console.log(err);
            context.fail(err);
        } else {                
            console.log(data);
            context.succeed(event);
        }
    });
};

The following policy is part of the lambda's role:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ses:SendEmail"
            ],
            "Resource": "*"
        }
    ]
}

For some reason, this lambda function fails to send any emails and it doesn't provide any status information inside the function's CloudWatch Log group:

REPORT RequestId: XXX   Duration: 534.59 ms Billed Duration: 600 ms Memory Size: 128 MB Max Memory Used: 117 MB 


Any help would be appreciated.



EDIT: I work in sandbox-mode and both source and recipient are verified emails from the region mentioned in the code (It is also reflected in the logs - no errors being thrown).


I Found the following questions in SO - but no relevant answer to my case:

Sending email via AWS SES within AWS Lambda function

AWS SES send email lambda not sending every time

SES email not sending

python error sending mail with amazon ses with aws lambda

Rot-man
  • 18,045
  • 12
  • 118
  • 124

2 Answers2

1

I think context.succeed is deprecated. Also you can use async/await:

exports.handler = async (event, context) => {

 var params = {
    Destination: {
        ToAddresses: ["recipientEmailAddress"]
    },
    Message: {
        Body: {
            Text: { Data: "Test" }
        },
        Subject: { Data: "Test Email" }
    },
    Source: "sourceEmailAddress"
};

const data = ses.sendEmail(params).promise()
return data
};
Rot-man
  • 18,045
  • 12
  • 118
  • 124
Arun Kamalanathan
  • 8,107
  • 4
  • 23
  • 39
1

Your code is perfectly fine. I just tried using your code and it has no problem.

I would recommend you check your identities setting and sandbox-mode.

If you are still in a sandbox-mode, it only allows you to send an email from a verified email address to other verified email addresses.

This documentation will be helpful.

Rot-man
  • 18,045
  • 12
  • 118
  • 124
piljoong
  • 371
  • 1
  • 3
  • Thanks @pilijoong - Both the source and recipient are verified emails (if one of them wasn't verified , it was reflected in the log). – Rot-man Nov 30 '19 at 08:26
  • Don't you see any other log messages other than the line you posted. Just in case did you check the spam ,junk folders in your email account – Arun Kamalanathan Nov 30 '19 at 09:11
  • I mean, If there was a problem with the emails not being verified, a relevant error was appearing in the log (Something like Error: The XXX email is not being verified). Anyway, +1 for your answer because your'e pointing out on an important issue. – Rot-man Nov 30 '19 at 09:19
  • And yes, The Spam / Junk folders were checked. – Rot-man Nov 30 '19 at 09:20