Email not being sent / No errors AWS/SES
Asked Answered
M

4

9

I have an AWS Lambda function:

sendEmail.js

const AWS = require('aws-sdk');
const ses = new AWS.SES();
 
function sendEmail(subject, message, senderEmail) {
  const params = {
    Destination: {
        ToAddresses: [
            process.env.VERIFIED_EMAIL
        ]
      },
    Message: {
        Body: {
            Text: {
                Data: message,
                Charset: 'UTF-8'
            }
        },
        Subject: {
            Data: subject,
            Charset: 'UTF-8'
        }
    },
    Source: process.env.VERIFIED_EMAIL,
    ReplyToAddresses: [senderEmail]
  }
  return ses.sendEmail(params).promise();
}
 
module.exports = sendEmail;

Called from

index.js

const sendEmail = require('./sendEmail');
exports.handler = async (event, context) => {
    return sendEmail(event.subject, event.message, event.email).then((response) => { context.done(null, 'Email sent')});
   
};

I have the env. variable VERIFIED_EMAIL set to a personal e-mail that is verified by AWS/SES.

My test case:

{
  "email": "[email protected]",
  "subject": "desc",
  "message": "Hello!"
}

Which passes and returns "Email sent" but I don't actually receive an e-mail. I've also deployed the API-GATEWAY and I can call the API w/ Postman and I receive the same "Email sent" message but no e-mail is actually sent.

I don't think it should be an issue with being in sandbox mode because the email I am sending it from is verified.

PS:

When Looking at the SES management console it says that the emails are being sent (they take up part of the 200 daily quota) and then that none of them were bounced or rejected but simply deliveries.

Microsporangium answered 5/12, 2020 at 2:51 Comment(2)
I believe the issue is with Outlook, as I added a personal Gmail account and everything worked as expected.Microsporangium
If you use Outlook, always assume the issue is from Outlook ...Eddie
M
3

A few things you should check with your SES before diving deeper.

  1. In sandbox mode both "source email address" and "destination email address" have to be verified. Instead a mail won't be delivered.

  2. In case you verify Email Domain so appropriate dns and DKIM records have to be added in your domain. Plus additional whitelist clearance is assumed if you use corporate domains.

  3. Your IAM AWS user should be permitted to execute SES api calls. Check your policies and roles. Check Secret Key and Secret Id you use.

  4. There might be problems when you use inappropriate email types and email sending endpoints (eg you try to send "SingleTemplatedMessage" via "BulkTemplatedMessage" endpoint)

Check all this things first.

Then you might try something else. In my project we use AWS SDK based on java to interact between BE and SES. It provides logs containing message statuses (if a message was sent successfully, rejected, its id and other errors if they occurred)

Additionally to keep track on every single message you send you can set up SES-SNS to exchange with notifications. It's described in the following article

https://sysgears.com/articles/how-to-track-email-status-with-amazon-ses-and-amazon-sns/

Other problems might occur in your mail client (spam filters etc)

Magneto answered 5/12, 2020 at 12:49 Comment(4)
In my use case, a simple contact us form, the email being sent was from a verified to a verified (itself) and just changing the "reply-to" field. That is why I didn't think it was a Sandbox issue. One thing I am unsure of is - I was using the SES Console which has features similar to your java-based SDK in terms of message statuses and it said every message was sent - it just didn't seem like they were showing up in my inbox (checked spam folders).Microsporangium
I would recommend to have a look at official AWS doc describing how to check actual connection to smtp server. docs.aws.amazon.com/ses/latest/DeveloperGuide/… Alternatively try to send message using not only RAW format. Some mail clients require both formats - plain text and htmlMagneto
it would look like thisMagneto
Message={ 'Subject': { 'Data': 'string', 'Charset': 'string' }, 'Body': { 'Text': { 'Data': 'string', 'Charset': 'string' }, 'Html': { 'Data': 'string', 'Charset': 'string' } } }Magneto
C
1

If you are sending the email with a Template (even if this not applies directly to the code of the question) if there is some error it won't warn you or give any error. Something I've found very useful is to check that the template is well formed before sending it. For that you can use something like this:

import { SESv2Client, SendEmailCommand, TestRenderEmailTemplateCommand } from "@aws-sdk/client-sesv2";


/**
 * Used to discover potential issues with the templates before sending them
 * @param templateName the name of the template to render
 * @param templateData the data to replace tags in the template
 */
async function testRenderEmailTemplate(templateName, templateData) {
  const command = new TestRenderEmailTemplateCommand({
    TemplateName: templateName,
    TemplateData: JSON.stringify(templateData),
  });

  const response = await client.send(command);
  return response;
}

async function sendEmail(
  templateName,
  templateData,
  recipient,
) {
  const templateIsValid = await testRenderEmailTemplate(templateName, templateData); // this will fail and the error will show you what's wrong with your template
  const command = new SendEmailCommand({
    ReplyToAddresses: ["[email protected]],
    Destination: {
      ToAddresses: [recipient],
    },
    Content: {
      Template: {
        TemplateName: templateName,
        TemplateData: JSON.stringify(templateData),
      },
    },
    FromEmailAddress: "[email protected]",
  });

  const response = await client.send(command);
  return response.$metadata.httpStatusCode === 200;
}
Curch answered 5/3, 2023 at 10:58 Comment(0)
M
0

Follow the check list to troubleshoot SES email sending:

  1. Check the SMTP credential is ok or not using code shared by AWS
  2. Same credential using in multiple application/IP may not work
  3. Use minimum mail configuration, if you do not know setting value remove it, do not leave it with NULL.
  4. Set, $config['charset'] = 'UTF-8'; // // 'UTF-8', 'ISO-8859-15'
  5. Set, $config['newline'] = "\r\n"; // "\r\n" or "\n" or "\r"

Additional Check

  1. Make sure trying to send email to the same domain if SES account is Sandbox
  2. Check all outbound with mail TCP port is open
  3. If using NATgateway Check both inbound and outbound with mail TCP port is open in open for the mail sending instance in it.
Microcosm answered 24/1, 2022 at 3:27 Comment(0)
A
0

After adding an event destination to the configuration set, all my test e-mails arrived.

enter image description here

Annia answered 10/12, 2023 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.