AWS SES S3 处理入站电子邮件

AWS SES S3 process inbound email

我正在开发一个基于 AWS SES 的电子邮件系统发布。对于所有传入的电子邮件,我已将路由设置为将消息保存在 S3 存储桶中,以便我可以异步处理它们。我遇到的问题是消息以原始格式保存在 S3 存储桶中:headers、电子邮件 body 等 + 加密附件(一个巨大的字符串)- 全部在一个文件中。

有没有办法将电子邮件从附件中分离出来,并在 AWS SES 级别将两者保存在单独的文件中?我正在尝试直接从 AWS 获取我需要的格式的数据,并避免在流程中添加另一个处理步骤。

如果 AWS SES 不提供这样的功能,那么处理这些消息以获得上述结果的正确方法是什么?

似乎不​​可能让 SES 自动为您拆分电子邮件。根据文档 here:

Amazon SES provides you the raw, unmodified email, which is typically in Multipurpose Internet Mail Extensions (MIME) format.

每当 SES 将新电子邮件文件放入 S3 时,我都会使用 S3 或 SNS 来触发 Lambda 函数。 Lambda 函数可以按照您的意愿拆分文件,然后将这些新文件写入另一个 S3 存储桶。

关于如何编写Lambda的问题。这是我们的 Lambda 的一部分。从中提取的主要内容是 parseEvent 函数。和 data.event.Records[0] 将为您提供详细信息

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

    var AWS = require('aws-sdk');

    // Validate characteristics of a SES event record.
    if (!event ||
      !event.hasOwnProperty('Records') ||
      event.Records.length !== 1 ||
      event.Records[0].hasOwnProperty('eventSource') ||
      event.Records[0].eventSource !== 'aws:ses' ||
      event.Records[0].eventVersion !== '1.0') {
      callback(null, {'disposition':'STOP_RULE_SET'});      
    }

    email = data.event.Records[0].ses.mail;
    subjectLine = event.Records[0].ses.mail.commonHeaders.subject;
}

关键是event.Record[0].ses.mail。不幸的是,我无法通过 Google 搜索找到它的结构,我确定我以前见过它。

对于稍后回到此问题的任何人,这是从 SES 调用 Lambda 函数时获得的 JSON 结构的 link。

http://docs.aws.amazon.com/ses/latest/DeveloperGuide/receiving-email-notifications-examples.html

经过一些搜索才到达该页面 ;-)

从 link 开始,Lambda 通知如下所示,

{
"notificationType": "Received",
"receipt": {
    "timestamp": "2015-09-11T20:32:33.936Z",
    "processingTimeMillis": 406,
    "recipients": [
        "recipient@example.com"
    ],
    "spamVerdict": {
        "status": "PASS"
    },
    "virusVerdict": {
        "status": "PASS"
    },
    "spfVerdict": {
        "status": "PASS"
    },
    "dkimVerdict": {
        "status": "PASS"
    },
    "action": {
        "type": "S3",
        "topicArn": "arn:aws:sns:us-east-1:012345678912:example-topic",
        "bucketName": "my-S3-bucket",
        "objectKey": "\email"
    }
},
"mail": {
    "timestamp": "2015-09-11T20:32:33.936Z",
    "source": "0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com",
    "messageId": "d6iitobk75ur44p8kdnnp7g2n800",
    "destination": [
        "recipient@example.com"
    ],
    "headersTruncated": false,
    "headers": [
        {
            "name": "Return-Path",
            "value": "<0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com>"
        },
        {
            "name": "Received",
            "value": "from a9-183.smtp-out.amazonses.com (a9-183.smtp-out.amazonses.com [54.240.9.183]) by inbound-smtp.us-east-1.amazonaws.com with SMTP id d6iitobk75ur44p8kdnnp7g2n800 for recipient@example.com; Fri, 11 Sep 2015 20:32:33 +0000 (UTC)"
        },
        {
            "name": "DKIM-Signature",
            "value": "v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=ug7nbtf4gccmlpwj322ax3p6ow6yfsug; d=amazonses.com; t=1442003552; h=From:To:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Date:Message-ID:Feedback-ID; bh=DWr3IOmYWoXCA9ARqGC/UaODfghffiwFNRIb2Mckyt4=; b=p4ukUDSFqhqiub+zPR0DW1kp7oJZakrzupr6LBe6sUuvqpBkig56UzUwc29rFbJF hlX3Ov7DeYVNoN38stqwsF8ivcajXpQsXRC1cW9z8x875J041rClAjV7EGbLmudVpPX 4hHst1XPyX5wmgdHIhmUuh8oZKpVqGi6bHGzzf7g="
        },
        {
            "name": "From",
            "value": "sender@example.com"
        },
        {
            "name": "To",
            "value": "recipient@example.com"
        },
        {
            "name": "Subject",
            "value": "Example subject"
        },
        {
            "name": "MIME-Version",
            "value": "1.0"
        },
        {
            "name": "Content-Type",
            "value": "text/plain; charset=UTF-8"
        },
        {
            "name": "Content-Transfer-Encoding",
            "value": "7bit"
        },
        {
            "name": "Date",
            "value": "Fri, 11 Sep 2015 20:32:32 +0000"
        },
        {
            "name": "Message-ID",
            "value": "<61967230-7A45-4A9D-BEC9-87CBCF2211C9@example.com>"
        },
        {
            "name": "X-SES-Outgoing",
            "value": "2015.09.11-54.240.9.183"
        },
        {
            "name": "Feedback-ID",
            "value": "1.us-east-1.Krv2FKpFdWV+KUYw3Qd6wcpPJ4Sv/pOPpEPSHn2u2o4=:AmazonSES"
        }
    ],
    "commonHeaders": {
        "returnPath": "0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com",
        "from": [
            "sender@example.com"
        ],
        "date": "Fri, 11 Sep 2015 20:32:32 +0000",
        "to": [
            "recipient@example.com"
        ],
        "messageId": "<61967230-7A45-4A9D-BEC9-87CBCF2211C9@example.com>",
        "subject": "Example subject"
    }
}
}