Configure AWS WorkMail and lambda function to read email content in nodeJs

Shruti Tirpude
3 min readJan 6, 2023

Ever wondered if we can integrate lambda function with AWS WorkMail to process the email messages and take actions on it i.e extract email body, subject, read attachments ?

We can trigger a lambda function when we get a new email in Amazon Workmail. The lambda function can read the email in MIME format and we can extract email body, subject, read attachments and process it.

What is AWS WorkMail :
Amazon WorkMail is a secure, managed business email and calendar service with support for existing desktop and mobile email client applications. Amazon WorkMail gives users the ability to seamlessly access their email, contacts, and calendars using the client application of their choice, including Microsoft Outlook, native iOS and Android email applications, any client application supporting the IMAP protocol, or directly through a web browser.

Follow the steps to integrate lambda function with Amazon WorkMail

Step 1 : Setup Workmail for your organization :
Configure AWS WorkMail: https://docs.aws.amazon.com/workmail/latest/adminguide/getting_started.html

Step 2 :
We can use SAM cli to create a lambda function, IAM policies as a resource locally as well as in AWS.
The benefit of using sam cli is we can test the application locally by invoking the lambda locally and don’t have to deploy first and test in AWS.

In the code, in template.yaml file, we are configuring the lambda function, the IAM policy for the lambda function and the permissions for workmail to access the lambda function.

Link for setting up lambda through SAM cli : https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html

Step 3:
Attach the lambda to the WorkMail
Link : https://docs.aws.amazon.com/workmail/latest/adminguide/lambda.html

Step 4 :
Write the code for retrieving message content of Email with AWS Lambda

The following example shows how we can read the email body by lambda in NodeJs.

In this example, we are creating a handler lambda function called ‘workMailLambda’ , and when we get a new email in WorkMail, this lambda is triggered with event parameter.

  1. Using WorkMailMessageFlow AWS package to get the raw message content and parsing the email MIME object using mailparser npm package.

2. Create getRawMessageContent function to get the raw email message content in MIME format

3. Parse the MIME email object using mailparser npm package to extract the email body text. Here in the following code, the function parseMimeEmail parses the email string MIME object and prints the email message body.

Link for using mailparser npm package : https://nodemailer.com/extras/mailparser/

We can read attachments (i.e excel attachments ) and process it. Hope this article makes it easier to integrate AWS WorkMail and lambda functions.

Github Link to the code : https://github.com/stirpude/AWS-ServerLess-Examples/tree/main/work-mail-lambda

--

--

Shruti Tirpude

Software Engineer with an enthusiasm for AWS technologies like serverless.I enjoy solving real world complex problems through technology.