Search code examples
amazon-web-servicesamazon-snsamazon-ses

Can I retrieve tags assigned to an email when sending with AWS SES?


I'm using AMAZON SES to send emails and AMAZON SNS to subscribe to a hook and receive personalized notifications (When an email is delivered, bounced or rejected, basically).

The point here is that I'm trying to filter email types. When sending an email, I'm adding a Tag to it, for example: 'recoverEmail'. With this, when I receive the delivery notification on my hook, I know exactly where I should look for the transaction record to update it properly.

I can't find anything searching, even within the docs. This is What I'm passing as params:

const params = {
  EmailTags: [{
    Name: 'emailType',
    Value: 'recoverPassword',
  }],
  Destination: {
    ToAddresses: process.env.NODE_ENV === 'production' ? [email] : [EmailSender.SANDBOX_EMAIL_TO],
  },
  Content: {
    Simple: {
      Body: {
        Text: {
          Data: text,
        },
      },
      Subject: {
        Data: `Recover your password for ${email} account`,
      },
    },
  },
  FromEmailAddress: EmailSender.EMAIL_FROM,
};

It properly works and send the email, but when I receive the delivery notification, the tag is not present anywhere:

{
  headers: {
    host: 'xxxxxxxxx.ngrok.io',
    'user-agent': 'Amazon Simple Notification Service Agent',
    'content-length': '1682',
    'accept-encoding': 'gzip,deflate',
    'content-type': 'application/json',
    'x-amz-sns-message-id': 'xxxxxxx-670f-5f63-9dc9-xxxxxxxxx',
    'x-amz-sns-message-type': 'Notification',
    'x-amz-sns-subscription-arn': 'arn:aws:sns:us-east-1:xxxxxxxx:xxxxxxxx-email-xxxxxx:yyyyyyyy-df21-478c-aaaaa-xxxxxxxxxx',
    'x-amz-sns-topic-arn': 'arn:aws:sns:us-east-1:xxxxxxxxx:xxxxxxxxxx-email-xxxxxxx',
    'x-forwarded-for': 'xxx.xxx.xxx.xxx',
    'x-forwarded-proto': 'https'
  },
  type: 'Notification',
  timestamp: '2023-04-14T04:45:57.113Z',
  message: {
    notificationType: 'Delivery',
    mail: {
      timestamp: '2023-04-14T04:45:55.792Z',
      source: '[email protected]',
      sourceArn: 'arn:aws:ses:us-east-1:xxxxxxxxx:identity/xxxxxxxx.com',
      sourceIp: 'xxx.xxx.xxx.xxx',
      callerIdentity: 'xxxx-api-xxxxxx',
      sendingAccountId: 'xxxxxxxxxxxxx',
      messageId: 'xxxxxxxxxxxx-xxxxxxxx-7e9a-4bc3-b4f7-xxxxxxxxx-000000',
      destination: [Array]
    },
    delivery: {
      timestamp: '2023-04-14T04:45:57.046Z',
      processingTimeMillis: 1254,
      recipients: [Array],
      smtpResponse: '250 2.0.0 OK  1681447557 i14-xxxxxxxxxxxxxxxxxx.127 - gsmtp',
      remoteMtaIp: 'xxx.xxx.xxx.xxx',
      reportingMTA: 'a8-97.smtp-out.amazonses.com'
    }
  },

If somebody know the trick on this, would be verrrrryyy helpfull for me.

I tried several web search and docs review. I haven't found anything that can help.


Solution

  • There are few ways to receive notifications via AWS SNS. It seems you have configured Amazon SES > Configuration: Identities >...> Notifications, it sends short notification data.

    If you want to handle extended event data (including tags), you need to create Configuration Set and explicitly set it in sendEmail API method.

    See aws docs "Monitor email sending using Amazon SES event publishing".

    Example of the Delivery event:

    {
        "eventType": "Delivery",
        "mail": {
            "timestamp": "2016-10-19T23:20:52.240Z",
            "source": "[email protected]",
            "sourceArn": "arn:aws:ses:us-east-1:123456789012:identity/[email protected]",
            "sendingAccountId": "123456789012",
            "messageId": "EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000",
            "destination": ["[email protected]"],
            "headersTruncated": false,
            "headers": [
                {"name": "From", "value": "[email protected]"},
                {"name": "To", "value": "[email protected]"},
                {"name": "Subject", "value": "Message sent from Amazon SES"},
                {"name": "MIME-Version", "value": "1.0"},
                {"name": "Content-Type", "value": "text/html; charset=UTF-8"},
                {"name": "Content-Transfer-Encoding", "value": "7bit"}
            ],
            "commonHeaders": {
                "from": ["[email protected]"],
                "to": ["[email protected]"],
                "messageId": "EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000",
                "subject": "Message sent from Amazon SES"
            },
            "tags": {
                "ses:configuration-set": ["ConfigSet"],
                "ses:source-ip": ["192.0.2.0"],
                "ses:from-domain": ["example.com"],
                "ses:caller-identity": ["ses_user"],
                "ses:outgoing-ip": ["192.0.2.0"],
                "myCustomTag1": ["myCustomTagValue1"],
                "myCustomTag2": ["myCustomTagValue2"]
            }
        },
        "delivery": {
            "timestamp": "2016-10-19T23:21:04.133Z",
            "processingTimeMillis": 11893,
            "recipients": [ "[email protected]" ],
            "smtpResponse": "250 2.6.0 Message received",
            "reportingMTA": "mta.example.com"
        }
    }