AWS Messaging & Targeting Blog
Build a Two-Way Email-to-SMS Service with Amazon SES and Amazon End User Messaging
Introduction
Businesses and organizations today struggle to effectively communicate with their customers, employees, or other stakeholders across the diverse range of digital channels they now use. One common problem arises when the requirement to exchange information quickly and reliably extends beyond traditional email. This issue challenges organizations where recipients lack immediate access to email. This applies to field workers, remote teams, or customers who prefer to communicate via text messages. It is vital to bridge this gap between email and SMS communication for timely updates, urgent notifications, and seamless collaboration. However, separate management of these disparate channels independently proves cumbersome and leads to inefficiencies.
To address this challenge, one approach is to leverage Amazon Simple Email Service (SES) and Amazon End User Messaging services to create a robust, scalable, and cost-effective messaging system. This system seamlessly bridges the gap between email and SMS, enhances the reach and delivery of your messages and streamlines your communication workflows. Ultimately, this approach delivers a superior experience for your audience, ensuring that critical information reaches recipients through their preferred channels in a timely and efficient manner.
This blog post will delve into the step-by-step process of building a solution that enables both Email-to-SMS and SMS-to-Email communication. This solution allows you to send SMS messages using email and receive any SMS replies on the same email address you used to send the original message. Furthermore, you can continue the conversation by replying to the email you receive in response. By the end, you’ll have the knowledge and tools necessary to revolutionize your communication strategy and deliver a superior experience to your audience.
Here are some of the use cases for this solution:
- Real estate agents can use this solution to send listing updates to clients via SMS, and then receive client inquiries and responses as emails.
- Customer service team can leverage the Email to SMS functionality to proactively reach out to customers with important notifications. Customers are able to respond directly via SMS.
- Retailers can use this solution to send order confirmations, shipping updates, and promotional offers to customers via SMS. Customers are able to respond with inquiries or feedback that are then received as emails.
- Medical practices and hospitals can leverage the Email to SMS functionality to quickly notify patients of appointment reminders, prescription refills, or other time-sensitive information. Patients can then respond via SMS, which gets converted to an email that the healthcare staff can access.
Solution Overview
The following figure shows the high level architecture for this solution.
- Email Users send an email to the email address formatted as “mobile-number@verified-domain”. Amazon SES email receiving receives the email and triggers a receipt rule.
- The email is published to Amazon Simple Notification Service (SNS) topic (EmailToSMS) based on the receipt rule action, which triggers an AWS Lambda function (ConvertEmailToSMS). The ConvertEmailToSMS Lambda function performs the following actions:
- Receives the event from SNS and constructs a text message using the email body content.
- The constructed message is then sent to the “mobile-number” in the destination email address using the “SendTextMessage” API from AWS End User Messaging SMS. This is achieved by using a phone number in AWS End User Messaging SMS as the origination identity.
- The SMS message ID and source email address are stored as items in the Amazon DynamoDB table (MessageTrackingTable). This tracks email addresses for replies from SMS users.
- Mobile Users receive the SMS, and they have the option to reply to the phone number with two-way SMS messaging
- AWS End User Messaging receives the incoming SMS message from the Mobile Users. It then publishes this message to a SNS topic (SMSToEmail) for two-way SMS integration, which triggers a Lambda function (ConvertSMSToEmail). The ConvertSMSToEmail Lambda function performs the following actions:
- Retrieves the item from “MessageTrackingTable” using “previousPublishedMessageId” (SMS message ID) from the SNS event, and locate the corresponding email address.
- Sends the SMS message body to the Email Users using SES. This step uses “mobile-number@verified-domain” as the source email address, and the email address retrieved from the previous step as the destination.
- Email Users receive the email, and they have the option to reply to the email to continue the conversation. Mobile Users will receive the latest reply from Email Users.
Walkthrough
This section will dive into the step-by step process for the deployment. There are 4 steps to deploy this solution:
- Configure SES verified identity for email receiving and sending.
- Deploy the CloudFormation stack for the Email to SMS functionality.
- Deploy the CloudFormation stack for the SMS to Email functionality.
- Set up two-way SMS messaging in AWS End User Messaging SMS.
Note: the Lambda code for this solution is developed based on phone numbers and long code as the supported origination identity in Australia. You need to adjust the Lambda code (“format_phone_number” function) accordingly for this to work in your country.
Refer to this GitHub repository for the solution source code.
Prerequisites
Prerequisites for this walkthrough:
- Administrator-level access to an AWS account
- A domain or subdomain that you own to create SES verified identity
- An origination identity that supports two-way messaging, following Choosing an origination identity for two-way messaging use cases. Simulator phone numbers are available if you are in the US
- A mobile phone to send and receive SMS
- An email address to send and receive emails
Step 1: Configure SES Verified Identity
Follow the steps outlined in Creating a domain identity to create a verified identity for your domain in your AWS account. Confirm your domain identity is in the “Verified” status before proceeding to the next step:
Step 2: Deploy Email to SMS functionality
The following steps create a CloudFormation stack to deploy the required components for Email to SMS functionality:
- Sign in to your AWS account.
- Download the email-to-sms.yaml for creating the stack.
- Navigate to the AWS CloudFormation page.
- Choose Create stack, and then choose With new resources (standard).
- Choose Upload a template file and upload the CloudFormation template that you downloaded earlier: email-to-sms.yaml. Then choose Next.
- Enter the stack name Email-To-SMS.
- Enter the following values for the parameters:
- RuleName: The name of your SES Rule Set and receipt rule.
- Recipient1: Domain name used for recipient condition in the SES Rule Set.
- Recipient2: Domain name used for recipient condition in the SES Rule Set if you need additional recipients.
- PhoneNumberId: Phone number ID of the phone number to send SMS messages.
- Choose Next, and then optionally enter tags and choose Submit. Wait for the stack creation to finish.
Now you have the required components to convert email to text, and sending it as SMS to a phone number using AWS End User Messaging SMS.
Note: if required, modify the following code in email-to-sms.yaml to format your phone numbers accordingly:
def format_phone_number(email_address):
# Extract the local part of the email address (before @)
local_part = email_address.split('@')[0]
# Remove the leading '0' and add '+61' for phone number (Australia)
if local_part.startswith('0'):
formatted_number = '+61' + local_part[1:]
return formatted_number
Step 3: Deploy SMS to Email functionality
The following steps create a CloudFormation stack to deploy the required components for SMS to Email functionality:
- Sign in to your AWS account.
- Download the sms-to-email.yaml for creating the stack.
- Navigate to the AWS CloudFormation page.
- Choose Create stack, and then choose With new resources (standard).
- Choose Upload a template file and upload the CloudFormation template that you downloaded earlier: sms-to-email.yaml. Then choose Next.
- Enter the stack name SMS-To-Email.
- Enter the following values for the parameters:
- EmailDomain: The email domain to use for the SMS-to-Email function
- Choose Next, and then optionally enter tags and choose Submit. Wait for the stack creation to finish.
Note: if required, modify the following code in sms-to-email.yaml to format your phone numbers accordingly:
def format_phone_number(phone_number):
# Replace the '+61' with '0' from the phone number (Australia)
formatted_number = f"0{mobile_number[3:]}"
return formatted_number
Step 4: Set up Two-Way Messaging in AWS End User Messaging SMS
Follow the steps 1 – 5 outlined in Set up two-way SMS messaging for a phone number in AWS End User Messaging SMS.
For step 6:
- For Destination type, choose Amazon SNS.
- Choose Existing SNS standard topic.
- For Incoming messages destination, choose the SNS topic created from Step 3 (default topic name is SMSToEmailTopic).
- For Two-way channel role, choose Use SNS topic policies.
- Choose Save changes.
This allows your origination identity (phone number) to receive incoming messages, which is then published to an SNS topic and converted into emails by Lambda.
Testing
To test the solution, send an email with the destination address of “mobile-number@verified-domain”. You should receive a SMS on your mobile with the following information:
- Source number: AWS End User Messaging phone number
- Message: Email body content
Note: AWS End User Messaging SMS has character limit for SMS messages depending on the type of characters the message contains. This solution takes the first 160 characters of the email body by default, you can adjust the EmailToSMS Lambda function as required.
Reply directly to the SMS, you should receive an email at the same email address that sent the original email, with the following information:
- Subject: Re:mobile-number
- Body: SMS message content
- Source email address: mobile-number@verified-domain
If you are not receiving the email or SMS, check the Lambda CloudWatch logs for troubleshooting.
Cleaning up
To remove unneeded resources after testing the solution, follow these steps:
- In the CloudFormation console, delete the Email-To-SMS stack
- In the CloudFormation console, delete the SMS-To-Email stack
- If applicable, in Amazon SES, delete the verified identities
- If applicable, in AWS End User Messaging, release the unused phone numbers
Additional Consideration
- There are costs associated for AWS End User Messaging Numbers and Inbound/Outbound SMS.
- The Email to SMS and SMS to Email functionality in this solution can be deployed separately depending on your requirements.
- Different countries have different capabilities and limitations for SMS.
- If you require Email to SMS, but not SMS-to-Email, consider using Sender IDs where this option is available. Not all countries support SenderID, and SenderID doesn’t support 2-way SMS (inbound).
- Message Parts per Second (MPS) limits applies depending on the country you are in, and types of origination identity you are using.
- By default, new SES and AWS End User Messaging SMS accounts are placed into sandbox. To move from sandbox to production, follow Request production access (Moving out of the Amazon SES sandbox) and SMS/MMS and Voice sandbox in AWS End User Messaging SMS
Conclusion
This blog has explored how organizations can leverage AWS services to build a flexible, two-way communication solution bridging the gap between email and SMS channels. By integrating Amazon SES and Amazon End User Messaging, businesses can reach their audience through multiple channels, ensuring timely and effective delivery of critical messages.
The detailed guide provided the knowledge to create a scalable, cost-effective system tailored to evolving communication needs – whether facilitating email-to-SMS or SMS-to-email exchanges. This unified approach to email and SMS capabilities empowers companies to address the common challenge of managing disparate communication platforms, streamlining workflows and enhancing responsiveness.
If you run into issues or want to submit a feature request, use the New issue button under the issues tab in the GitHub repository.