Convert AWS SES received emails to human readable format

Viewed 718

Is it possible to convert received emails in AWS SES to a human readable format before forwarding to SNS or S3? Please share a example code, if its possible.

A sample email content received is as follows

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

The textual content of the email expected was 'Hi, this is a test mail'

1 Answers

I was able to achieve the outcome, instead of converting the mails, store them to a S3 bucket and using a S3 trigger, invoke a Lambda function which forwards the mail content. To simplify the forwarding, I have used the NPM library aws-lambda-ses-forwarder, which does all the heavy lifting. Following code shows the logic for email forwarding.

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);
};
  • forwarded-email-address - You will receive the email to your inbox from this address.
  • received-email-address - You will be receiving the emails sent to this address forwarded by the Lamda to your inbox.
  • your-email-address - Your email address which you want to receive the forwarded emails.

In addition, make sure, the Lambda function has the right permission to read from S3 and forward mails, using SES.

Related