将AWS SES收到的电子邮件转换为人类可读格式

时间:2017-07-22 16:19:01

标签: amazon-web-services amazon-ses

在转发到SNS或S3之前,是否可以将收到的AWS SES中的电子邮件转换为人类可读的格式?如果可能,请分享示例代码。

收到的电子邮件内容示例如下

FW: Test Email 1\r\n\r\nTest 1\r\n\r\nFrom: Ashan \r\nSent: Friday, 
April 21, 2017 2:30 PM\r\nTo: ‘test@example.org' 
<test@example.org<mailto:t=\r\nest@example.org>>\r\nSubject: Test 
Email\r\n\r\nHi,\r\n\r\nThis is a test 
mail.\r\n\r\nRegards,\r\nAshan\r\n\r\n--
_000_HK2PR0302MB2609710D3BC86E413C88D359B81A0HK2P2MB2609_\r\nContent-
Type: text/html; charset=\"us-ascii\"\r\nContent-Transfer-Encoding: 
quoted-printable\r\n\r\n<html xmlns:v=3D\"urn:schemas-microsoft-
com:vml\" xmlns:o=3D\"urn:schemas-micr=\r\nosoft-com:office:office\" 
xmlns:w=3D\"urn:schemas-microsoft-com:office:word\"
    =\r\nxmlns:m=3D\"http://schemas.microsoft.com/office/2004/12/omml\" 
xmlns=3D\"http:=\r\n//www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta 
http-equiv=3D\"Content-Type\" content=3D\"text/html; charset=3Dus-
ascii\"=\r\n>\r\n<meta name=3D\"Generator\" content=3D\"Microsoft Word 
15 (filtered medium)\">\r\n<style><!--\r\n/* Font Definitions
    */\r\n@font-face\r\n\t{font-family:\"Cambria Math\";\r\n\tpanose-
1:2 4 5 3 5 4 6 3 2 4;}\r\n@font-face\r\n\t{font-
family:Calibri;\r\n\tpanose-1:2 15 5 2 2 2 4 3 2 4;}\r\n/* Style 
Definitions */\r\np.MsoNormal, li.MsoNormal, 
div.MsoNormal\r\n\t{margin:0in;\r\n\tmargin-bottom:.0001pt;\r\n\tfont-
size:11.0pt;\r\n\tfont-family:\"Calibri\",sans-serif;}\r\na:link, 
span.MsoHyperlink\r\n\t{mso-style-
priority:99;\r\n\tcolor:#0563C1;\r\n\ttext-
decoration:underline;}\r\na:visited, 
span.MsoHyperlinkFollowed\r\n\t{mso-style-
priority:99;\r\n\tcolor:#954F72;\r\n\ttext-
decoration:underline;}\r\np.msonormal0, li.msonormal0, 
div.msonormal0\r\n\t{mso-style-name:msonormal;\r\n\tmso-margin-top-
alt:auto;\r\n\tmargin-right:0in;\r\n\tmso-margin-bottom-
alt:auto;\r\n\tmargin-left:0in;\r\n\tfont-size:12.0pt;\r\n\tfont-
family:\"Times New Roman\",serif;}\r\nspan.EmailStyle18\r\n\t{mso-
style-type:personal;\r\n\tfont-family:\"Calibri\",sans-
serif;\r\n\tcolor:windowtext;}\r\nspan.EmailStyle19\r\n\t{mso-style-
type:personal;\r\n\tfont-family:\"Calibri\",sans-
serif;\r\n\tcolor:#1F497D;}\r\nspan.EmailStyle20\r\n\t{mso-style-
type:personal;\r\n\tfont-family:\"Calibri\",sans-
serif;\r\n\tcolor:#1F497D;}\r\nspan.EmailStyle21\r\n\t{mso-style-
type:personal;\r\n\tfont-family:\"Calibri\",sans-
serif;\r\n\tcolor:#1F497D;}\r\nspan.EmailStyle22\r\n\t{mso-style-
type:personal-reply;\r\n\tfont-family:\"Calibri\",sans-
serif;\r\n\tcolor:#1F497D;}\r\n.MsoChpDefault\r\n\t{mso-style-
type:export-only;\r\n\tfont-size:10.0pt;}\r\n@page 
WordSection1\r\n\t{size:8.5in 11.0in;\r\n\tmargin:1.0in 1.0in 1.0in
    1.0in;}\r\ndiv.WordSection1\r\n\t{page:WordSection1;}\r\n-->
</style><!--[if gte mso 9]><xml>\r\n<o:shapedefaults v:ext=3D\"edit\" 
spidmax=3D\"1026\" />\r\n</xml><!--><!--[if gte mso 9]>
<xml>\r\n<o:shapelayout v:ext=3D\"edit\">\r\n<o:idmap v:ext=3D\"edit\" 
data=3D\"1\" />\r\n</o:shapelayout></xml><!-->\r\n</head>\r\n<body 
lang=3D\"EN-US\" link=3D\"#0563C1\" vlink=3D\"#954F72\">\r\n<div 
class=3D\"WordSection1\">\r\n<p class=3D\"MsoNormal\"><span 
style=3D\"color:#1F497D\">Test 4 - </span><span
    =\r\nstyle=3D\"color:#1F497D\"><a

预期电子邮件的文字内容是&#39;嗨,这是一封测试邮件&#39;

1 个答案:

答案 0 :(得分:1)

我能够实现结果,而不是转换邮件,将它们存储到S3存储桶并使用S3触发器,调用Lambda函数转发邮件内容。为了简化转发,我使用了NPM库aws-lambda-ses-forwarder,它完成了所有繁重的任务。以下代码显示了电子邮件转发的逻辑。

var LambdaForwarder = require("aws-lambda-ses-forwarder");

exports.handler = function(event, context) {

var overrides = {
    config: {
        fromEmail: "<forwarded-email-address>",
        subjectPrefix: "",
        emailBucket: "ses-bucket",
        emailKeyPrefix: "emails/",
        forwardMapping: {
            "<received-email-address>": [
                "<your-email-address>"
            ]
        }
    }
};
LambdaForwarder.handler(event, context, overrides);
};
  • 转发电子邮件地址 - 您将通过此地址收到发件箱中的电子邮件。
  • 收到的电子邮件地址 - 您将收到发送到此地址的电子邮件,由Lamda转发到您的收件箱。
  • your-email-address - 您希望收到转发电子邮件的电子邮件地址。

此外,请确保Lambda函数具有使用SES从S3读取和转发邮件的正确权限。