本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
传入电子邮件事件示例
有两种方法可以将传入的电子邮件事件发送到 Lambda 函数。第一种方法是使用接收规则中的 Lambda 操作将事件记录直接发送到该函数。第二种方法是使用接收规则中的 Amazon SNS 操作将事件记录发送到 Amazon SNS,然后将 Lambda 函数添加为 Amazon SNS 主题的订阅终端节点。
本节包含 Amazon SES 可以发送到 Lambda 的事件记录的示例。您可以使用这些示例来创建和测试 Lambda 函数。
注意
本部分中的示例包括换行符,以便于阅读。如果您复制本节中的示例,则应删除换行符以生成有效的 JSON 对象。
Lambda 操作提供的事件记录
当您向接收规则添加 Lambda 操作时,Amazon SES 会在每次收到传入消息时向 Lambda 发送事件记录。此事件包含有关传入消息的多个电子邮件标头的信息,以及 Amazon SES 对传入消息执行的多个测试的结果。但是,它会省略传入消息的正文。
以下示例显示了这些事件记录通常包含的值。
{ "Records": [{ "eventSource": "aws:ses", "eventVersion": "1.0", "ses": { "mail": { "timestamp": "2019-08-05T21:30:02.028Z", "source": "prvs=144d0cba7=sender@example.com", "messageId": "EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000", "destination": ["recipient@example.com"], "headersTruncated": false, "headers": [{ "name": "Return-Path", "value": "<prvs=144d0cba7=sender@example.com>" }, { "name": "Received", "value": "from smtp.example.com [203.0.113.0]) by inbound-smtp.us-east-1.amazonaws.com
with SMTP id bsvpsoklfhu7u50iur7h0kk9a2ou0r7iexample for recipient@example.com;
Mon, 05 Aug 2019 21:30:02 +0000 (UTC)" }, { "name": "X-SES-Spam-Verdict", "value": "PASS" }, { "name": "X-SES-Virus-Verdict", "value": "PASS" }, { "name": "Received-SPF", "value": "pass (spfCheck: domain of example.com designates 203.0.113.0 as permitted sender)
client-ip=203.0.113.0; envelope-from=prvs=144d0cba42=sender@example.com; helo=
smtp.example.com;" }, { "name": "Authentication-Results", "value": "amazonses.com; spf=pass (spfCheck: domain of example.com designates 203.0.113.0
as permitted sender) client-ip=203.0.113.0; envelope-from=prvs=144d0cba42=
sender@example.com; helo=smtp.example.com; dkim=pass header.i=@example.com;
dmarc=none header.from=example.com;" }, { "name": "X-SES-RECEIPT", "value": "AEFBQUFBQUFBQUFHbFo0VU81VzVuYmRDNm51nhTVWpabDh6J4V2l5cG5PSHFtNzlBeUk90example" }, { "name": "X-SES-DKIM-SIGNATURE", "value": "a=rsa-sha256; q=dns/txt; b=Cm1emU30VcD6example=; c=relaxed/simple; s=6gbrjpgwjs
5zn6fwqknexample; d=amazonses.com; t=1567719002; v=1; bh=DSofsjAoUvyZj6YsBDP5en
pRO1otGb7Nes0Qexample=; h=From:To:Cc:Bcc:Subject:Date:Message-ID:MIME-Version:
Content-Type:X-SES-RECEIPT;" }, { "name": "DKIM-Signature", "value": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=example.com; i=@example.com; q=dns/txt;
s=example12345; t=1567719001; x=1599255001; h=from:to:subject:date:message-id:
references:in-reply-to:mime-version; bh=sjAoUvyZj6YsBDP5enpRO1otGb7s0Qexample=;
b=EQw2D4RLOW2IHE9OgfEA4WXp+AENJtaD2+63wmd5J+d+t/xoaiKUGClOS7WhpyOmlipryOz+iOhxU
v350xJIHjLTi9Jsnlw76mRK8o4770TaUz620joCVN21n4cxsrRZpv+1kS0EcAxaF30pmwlni+XT4ems
Vxn7zO0I8example=;" }, { "name": "Received", "value": "from mail.example.com (mail.example.com [203.0.113.0]) by email-inbound-relay-
1d-9ec21598.us-east-1.example.com (Postfix) with ESMTPS id 57F83A2042 for
<recipient@example.com>; Mon, 5 Aug 2019 21:29:58 +0000 (UTC)" }, { "name": "From", "value": "\"Doe, John\" <sender@example.com>" }, { "name": "To", "value": "\"recipient@example.com\" <recipient@example.com>" }, { "name": "Subject", "value": "This is a test" }, { "name": "Thread-Topic", "value": "This is a test" }, { "name": "Thread-Index", "value": "AQHVZDAaQ58yKI8q7kaAjkhC5stGexample" }, { "name": "Date", "value": "Mon, 5 Aug 2019 21:29:57 +0000" }, { "name": "Message-ID", "value": "<F8098FDD-49A3-442D-9935-F6112example@example.com>" }, { "name": "References", "value": "<1FCED16B-F6B0-4506-A6F0-594DFexample@example.com>" }, { "name": "In-Reply-To", "value": "<1FCED16B-F6B0-4506-A6F0-594DFexample@example.com>" }, { "name": "Accept-Language", "value": "en-US" }, { "name": "Content-Language", "value": "en-US" }, { "name": "X-MS-Has-Attach", "value": "" }, { "name": "X-MS-TNEF-Correlator", "value": "" }, { "name": "x-ms-exchange-messagesentrepresentingtype", "value": "1" }, { "name": "x-ms-exchange-transport-fromentityheader", "value": "Hosted" }, { "name": "x-originating-ip", "value": "[203.0.113.0]" }, { "name": "Content-Type", "value": "multipart/alternative; boundary=\"_000_F8098FDD49A344F6112B195BDAexamplecom_\"" }, { "name": "MIME-Version", "value": "1.0" }, { "name": "Precedence", "value": "Bulk" }], "commonHeaders": { "returnPath": "prvs=144d0cba7=sender@example.com", "from": ["\"Doe, John\" <sender@example.com>"], "date": "Mon, 5 Aug 2019 21:29:57 +0000", "to": ["\"recipient@example.com\" <recipient@example.com>"], "messageId": "<F8098FDD-49A3-442D-9935-F6112B195BDA@example.com>", "subject": "This is a test" } }, "receipt": { "timestamp": "2019-08-05T21:30:02.028Z", "processingTimeMillis": 1205, "recipients": ["recipient@example.com"], "spamVerdict": { "status": "PASS" }, "virusVerdict": { "status": "PASS" }, "spfVerdict": { "status": "PASS" }, "dkimVerdict": { "status": "PASS" }, "dmarcVerdict": { "status": "GRAY" }, "action": { "type": "Lambda", "functionArn": "arn:aws:lambda:us-east-1:123456789012:function:IncomingEmail", "invocationType": "Event" } } } }] }
Amazon SNS 操作提供的事件记录
当您向接收规则添加 Amazon SNS 操作时,通知将包含电子邮件的全部内容。如果您希望 Lambda 函数处理电子邮件的正文,则应该在接收规则中添加 Amazon SNS 操作,然后在 Amazon SNS 中,将您的 Lambda 函数订阅到 Amazon SNS 函数。此配置会导致您的 Lambda 函数在收到 Amazon SNS 主题通知时被激活。
{ 'Records': [ { 'EventSource': 'aws:sns', 'EventVersion': '1.0', 'EventSubscriptionArn': 'arn:aws:sns:us-east-1:123456789012:IncomingEmail:12345678', 'Sns': { 'Type': 'Notification', 'MessageId': 'EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000', 'TopicArn': 'arn:aws:sns:us-east-1:123456789012:IncomingEmail', 'Subject': 'Amazon SES Email Receipt Notification', 'Message':
<message content—see below>
, 'Timestamp': '2019-09-06T18:52:16.076Z', 'SignatureVersion': '1', 'Signature': '012345678901example==', 'SigningCertUrl': 'https://sns.us-east-1.amazonaws.com/SimpleNotificationService-01234567890123456789012345678901.pem', 'UnsubscribeUrl': 'https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&
SubscriptionArn=arn:aws:sns:us-east-1:0123456789012:IncomingEmail:
0b863538-3f32-462e-9c89-8d8e0example', 'MessageAttributes': {} } } ] }
该 Message
属性包含 JSON 编码的字符串。该字符串包含消息的标头和内容。消息正文本身采用 Base64 编码。如果您希望在 Lambda 函数中使用消息正文,则首先必须解码 Message
属性,然后解码 Content
对象。
以下示例显示了 Message
属性中包含的值。
{ "notificationType": "Received", "mail": { "timestamp": "2019-09-06T18:52:14.965Z", "source": "0100016d07eb7477-8e1938ce-475e-4e4b-89cb-example-000000@amazonses.com", "messageId": "12345678901example", "destination": ["recipient@example.com"], "headersTruncated": false, "headers": [{ "name": "Return-Path", "value": "<0100016d07eb7477-8e1938ce-475e-4e4b-89cb-example-000000@amazonses.com>" }, { "name": "Received", "value": "from a1-23.smtp-out.amazonses.com (a1-23.smtp-out.amazonses.com [203.0.113.0]) by
inbound-smtp.us-east-1.amazonaws.com with SMTP id
12345678901example for recipient@example.com; Fri, 06 Sep 2019
18:52:14 +0000 (UTC)" }, { "name": "X-SES-Spam-Verdict", "value": "PASS" }, { "name": "X-SES-Virus-Verdict", "value": "PASS" }, { "name": "Received-SPF", "value": "pass (spfCheck: domain of amazonses.com designates 203.0.113.0 as permitted sender)
client-ip=203.0.113.0; envelope-from=0100016d07eb7477-8e1938ce-475e-4e4b-89cb-example
-000000@amazonses.com; helo=a1-23.smtp-out.amazonses.com;" }, { "name": "Authentication-Results", "value": "amazonses.com; spf=pass (spfCheck: domain of amazonses.com designates 203.0.113.0
as permitted sender) client-ip=203.0.113.0; envelope-from=0100016d07eb7477-8e1938ce
-475e-4e4b-89cb-example-000000@amazonses.com; helo=a1-23.smtp-out.amazonses.com;
dkim=pass header.i=@amazonses.com; dmarc=none header.from=example.com;" }, { "name": "X-SES-RECEIPT", "value": "AEFBQUFBQUFBQUFFQkx0QUJZZENEXAMPLE=" }, { "name": "X-SES-DKIM-SIGNATURE", "value": "a=rsa-sha256; q=dns/txt; b=d5azwgA2iBqAjA4NBm1ARzjJ95raRmy4G84iVdd3x2JzSHeUnQuTuLmJ
AqRrYY3WpMIVRFy01hITaguCVjUPWBR0xF6fCEXH85cf3RNeFQyLfWZqoXKfBdjFRV+13troDterH2MxBUL
8rjzcvdHetl0ImwlaK2PGmePTexample=; c=relaxed/simple; s=EXAMPLE7c191be45-e9aedb9a-02
f9-4d12-a87d-dd0099a07f8a-000000; d=amazonses.com; t=1567795935; v=1; bh=CZ1SghsYaA
6SSCbitzsLISeFoNlpdtH1Pyiexample=; h=From:To:Cc:Bcc:Subject:Date:Message-ID:MIME-
Version:Content-Type:X-SES-RECEIPT;" }, { "name": "DKIM-Signature", "value": "v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=EXAMPLE7c191be45-e9aedb9a-02f9-
4d12-a87d-dd0099a07f8a-000000; d=amazonses.com; t=1567795934; h=From:To:Subject:
MIME-Version:Content-Type:Message-ID:Date:Feedback-ID; bh=CZ1SghsYaA6SSCbitzsLISeFo
NlpdtH1Pyiexample=; b=L6VXqR1PSN/FYqJI/VAfPRKFgtakcHCYJvuJqVYbuJT8I3FOhqOvkbcgHxOgs
woxPfvGrL6S53H8Er5Do/CPvOM4Tx3ilE+a0GTYVLjKmwltNeN09YWlJAoqG5KMQPZUxRYaNvYPInLzUdGi
rdjkbSIgZEnrvq5MzaMWexample=" }, { "name": "From", "value": "sender@example.com" }, { "name": "To", "value": "recipient@example.com" }, { "name": "Subject", "value": "Amazon SES Test" }, { "name": "MIME-Version", "value": "1.0" }, { "name": "Content-Type", "value": "multipart/alternative; boundary=\"----=_Part_869787_396523212.15677example\"" }, { "name": "Message-ID", "value": "<0100016d07eb7477-8e1938ce-475e-4e4b-89cb-example-000000@email.amazonses.com>" }, { "name": "Date", "value": "Fri, 6 Sep 2019 18:52:14 +0000" }, { "name": "X-SES-Outgoing", "value": "2019.09.06-203.0.113.0" }, { "name": "Feedback-ID", "value": "1.us-east-1.ZitRoTk0xziun8WEJevt+cSJ17QNuCwulg2D2v3nrT0=:AmazonSES" }], "commonHeaders": { "returnPath": "0100016d07eb7477-8e1938ce-475e-4e4b-89cb-example-000000@amazonses.com", "from": ["sender@example.com"], "date": "Fri, 6 Sep 2019 18:52:14 +0000", "to": ["recipient@example.com"], "messageId": "<0100016d07eb7477-8e1938ce-475e-4e4b-89cb-example-000000@email.amazonses.com>", "subject": "Amazon SES Test" } }, "receipt": { "timestamp": "2019-09-06T18:52:14.965Z", "processingTimeMillis": 1098, "recipients": ["recipient@example.com"], "spamVerdict": { "status": "PASS" }, "virusVerdict": { "status": "PASS" }, "spfVerdict": { "status": "PASS" }, "dkimVerdict": { "status": "GRAY" }, "dmarcVerdict": { "status": "GRAY" }, "action": { "type": "SNS", "topicArn": "arn:aws:sns:us-east-1:123456789012:IncomingEmail", "encoding": "BASE64" } }, "content": "UmV0dXJuLVBhdGg6IDwwMTAwMDE2ZDA3ZWI3NDc3LThlMTkzOGNlLTQ3NWUtNGU0Yi04OWNiLWV4YW1wbGUtM
DAwMDAwQGFtYXpvbnNlcy5jb20+ClJlY2VpdmVkOiBmcm9tIGExLTIzLnNtdHAtb3V0LmFtYXpvbnNlcy5jb2
0gKGExLTIzLnNtdH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