3
votes

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

2
Put callback(null, {err: err, data: data}); to the last, after else block to see what will be throw out.hoangdv
Seems like the function is getting timed out, have you launched it in VPC ? Try to launch it without VPC ?James Dean
No Timeout - the function returns successfully after 534.59 [ms] (see in the log above). Launched without VPC.RtmY
any solution here? I suppose not...osullic

2 Answers

1
votes

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.

1
votes

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
};