A Cost-effective, scalable, and customizable email service, Amazon Simple Email Service (Amazon SES) can execute spam checks and virus scans, among other things. Learn how to utilize Amazon SES to automate the processing of emails in real time and interface with an application.
In this article, we’ll look at how to automate customer relationship management (CRM) platforms and how to process emails relating to customer contact or direct email requests (also known as order processing). One use case for this is copying a customer engagement email to Salesforce (or any other database), where it is stored and can later be classified or related to the relevant client account or opportunity. When developing an application that needs to receive emails from a mailbox, a developer would usually have to utilize a mail library (such as JavaMail if the application is written in Java) to make a call to the mailbox, authenticate, and then pull the messages into an object in the application. Essentially, this would entail polling the mailbox every 10 – 15 minutes to check for new messages, handling problems when the mailbox is unavailable, and ensuring that the mailbox is always fully operational. This solution can assist you in implementing automated processing of emails as they arrive in a mailbox without the need to poll the mailbox for new messages. The serverless architecture can be used to implement the entire solution.
With the help of SES, you may automate the processing of emails within an application workflow. The option in SES to save received emails in S3 and activate a Lambda function to process the message will be used to avoid polling a mailbox for the message. Using email, this sample application demo receives simple orders, which are then processed automatically and the information are kept in DynamoDB. The high-level architecture is depicted in the diagram below:
Emails are received by Amazon SES and delivered to an Amazon S3 bucket by a serverless application. Amazon Comprehend is used to detect the prevailing language in the message body of the application. It then searches Amazon DynamoDB for the email address of a support organization that specializes in that subject. The final step is to send the email to the intended recipient via Amazon SES. Incoming emails can be processed or audited further by archiving them on Amazon S3.
An event begins when a consumer sends an email to a generic support address, such as [email protected], indicating that they need assistance. A recipient rule in Amazon SES makes it possible for the service to hear this email. Messages are written to an Amazon S3 bucket with a certain prefix according to the rule.
On object creation events, this bucket and prefix are configured using S3 Events, which triggers a Lambda function in this bucket. After reading the email, parsing its contents, and sending them to Amazon Comprehend for language detection, the Lambda function returns a result. Amazon DynamoDB looks up the detected language code in an Amazon DynamoDB table that contains mappings between language codes and support group email addresses for the languages in which they are detected. One support group may respond to emails in English, while another support group could respond to emails in French. It is the responsibility of the Lambda function to determine and deliver the same email address to several recipients via a resend operation (email forwarding). Assume that the lookup does not produce any results for the destination address, or that the language could not be determined. Alternatively, the email is forwarded to a catch-all email address that was set during the application’s deployment.
Download list of all AWS Services PDF
To utilize Amazon SES to receive email messages, you must first verify that you control the domain that will be used. Consult the manual for instructions on how to validate your domain using the Amazon SES console. Amazon Route 53 will help you to register a domain name if you don’t already have one registered.
Step 1: Create an S3 Bucket for email storage on Amazon Web Services.
Begin by creating an S3 bucket in which all incoming emails will be kept in order for the lambda to be able to process the entire email in its entirety. A policy must be associated to the bucket in order for SES to be able to place objects in the bucket.
The following modifications should be made to the preceding policy illustration:
- If you wish to write to an Amazon S3 bucket, substitute the name of the bucket for myBucket.
- Use your AWS account ID instead of 111122223333.
Step 2: To create a DynamoDB table in order to simulate an application.
Next, create a DynamoDB table using the following parameters: Information about incoming orders is saved in a DynamoDB table. For the purposes of this example, we will use an email address as the partition key for a table. As an example of a data model, consider the following:
Step 3: Create a Lambda Function that is activated by SES and is used to process email messages.
To proceed, write the Lambda function to process the email and transmit data into the DynamoDB table, which you can see in this code example. This function requires an execution role that has permissions to access the S3 bucket, the DynamoDB table, and establish a CloudWatch log group in order for it to be executed successfully. It also requires a Resource-based Policy in order for SES to be able to call the Lambda function. As a final step, when we configure SES to call the lambda function, SES automatically adds the necessary permissions to the function, which are specified in this article. The following is an example of a policy statement:
When you include a Lambda action in a receipt rule, Amazon SES sends an event record to Lambda every time an incoming message is received. While this event contains information about the email headers for the incoming message, it does not contain information about the results of the tests (spam filtering and virus scanning) that Amazon SES performs on incoming messages. Additionally, it does not contain information about the body of the incoming message. The lambda function must process the body of an email that has been stored in S3 as a result of this. Details on the event may be found here. To demonstrate the functionality of this demo app, we assume that the item name is included in the subject and that the amount of each item is included in its body, and that this information is written to the DynamoDB table.
Step 4: Configure SES to send emails to S3 and activate Lambda functions in response to those emails Function
The final step is to set up Amazon SES (Simple Email Service). The first step is to verify a domain name so that SES may use it to send and receive emails. Verifying your domain ownership ensures that you are the legitimate owner of the domain and are therefore authorised to handle the sending and receiving of emails from addresses associated with the domain. To check the validity of your domain name, please do the following:
- Domains can be found under the Identity Management section of the navigation pane in the SES console.
- Select Verify new Domain from the drop-down menu.
- In the Verify new Domain dialogue box, type in the domain name you want to verify.
Select Verify This Domain from the drop-down menu.
- An entry for Domain verification record set will be displayed in the dialogue box. You must add this record to the domain DNS server in order for it to function properly. In addition, you will need to add an email receiving record (MX Record) to your domain DNS server’s configuration.
- As long as your DNS server is Route53, and it is registered under the same account as your SES account, SES will provide you with the option to update it directly from within the SES console.
Once the domain is verified its status switches from “pending verification” to “verified” and now it can used it to send and receive emails.
Create a rule set for the recipients after that. The Rule Set allows you to specify what SES should behave with emails that come from domains that you own. You have the option of creating rules for specific addresses or for any address within a domain. To construct the Rule Set, follow these steps:
- Rule Sets can be found in the Email Receiving section of the left navigation pane.
- Create a rule by selecting it from the drop-down menu.
- Enter the email address of the recipient for whom you wish to configure the rule. You can add up to a maximum of 100 recipient addresses or just set it up for any address in the domain using just the domain name as a wildcard.
Once the addresses have been entered, go to the next step, which is to enter the rules actions. Include the following two actions:
The first is of the type S3, and it is used to save a copy of the email to the S3 bucket that was created in step 1. From the drop-down list, select the bucket name that you created in step 1. As an alternative, you can add a prefix to the filename in order to categorise the output of various rules.
- The second is of type Lambda, and it is used to trigger the lambda that will be responsible for processing the email. From the drop-down list, select the lambda that was created in step 3.
Once the SES Rule has been configured, we will have the complete workflow in operation. The Lambda will now process any emails delivered to the [email protected] address. You can configure email processing to be a part of your application’s workflow in this way, without having to poll the application for updates.
Need help on AWS?
AWS Partners, such as AllCode, are trusted and recommended by Amazon Web Services to help you deliver with confidence. AllCode employs the same mission-critical best practices and services that power Amazon’s monstrous ecommerce platform.
The goal of this post was to demonstrate that it is possible to integrate email processing into an application workflow without the need to poll a mailbox.
It is possible to create a serverless architecture by using SES to receive emails. This allows emails to be processed and checked for spam and viruses, after which the output can be sent to any downstream system or stored in a database for application use.
As a trusted AWS Partner, AllCode’s team of experts is here to help you with any questions you may have about Amazon Web Services. To learn more about other Amazon Web Services, check out our AWS Partner Menu!
When thinking about programming languages, frameworks, and SDKs for mobile web app development, you should consider the front-end (UI) development environment as well as the back-end (server-side) development environment.
An AWS Advanced Technology Partner, Tigera delivers Calico and Calico Enterprise for security and networking on EKS, both of which are AWS Containers Competency certified.
Centro Community Partners (Centro) is a nonprofit organization that provides programs and resources to help underserved entrepreneurs start, develop and grow their small businesses. Centro also offers technology and curriculum to other organizations and trainers through their Entrepreneurship Suite.
Blockchain technology has the potential to be a windfall for musicians, filmmakers, and video game developers. With the advent of new technology, the way we consume entertainment is changing. Vezt assists artists in distributing their tracks on digital channels and in promoting their work.