Sending Emails using AWS SES [NodeJS]
AWS have tons of services for everything. SES stands for Simple Email Service (Yeas it sends Emails). In this article I will go through
- Setting up Email for SES
- Setting up SNS to get notified about Email events from SES (Delivery , errors etc.)
- Using templates to send emails using aws-sdk for javascript
Setup
By default AWS SES only provides us a sandbox environment in order to prevent spamming. In this you will only be able to send and receive emails from verified email addresses.
Go to Simple Email Service (SES) >> Email Addresses
Click on Verify a new Email Address and enter the email you want to use. Now go to your email provider and click on the verification link. You should see your verified email , under email address identities.
That’s it. If you want to leave from sandbox env. to production you need to contact Support center and create a case.There you will get a form to fill it’s pretty straight forward. So I am not going to dig into that part.
Setup Event Notifications for SES
- [These steps are optional , But I highly recommend you to configure the SNS . I skipped this step at first and end up pulling my hair to figure what happened to the emails I sent :P]
Even if we get a successful callback from the SES api , it only means that email message has been accepted by the SES. It doesn’t guarantee that message will be delivered.For this reason we need to configure SES to send event notifications through Amazon SNS(Simple Notification Service)
- Create a topic in SNS
Go to SNS Console click on create a topic , give your topic a name and a display name.
Copy topic ARN because we require it for the next step.
2 . Subscribe to a topic
In order to receive the messages published to a topic, we have to subscribe an endpoint to that topic.This could be a mobile app, server , lambda or email. For this article I am going to use email as an endpoint.
open SNS Console and click on Create Subscription
Paste the topic ARN we got in the previous step.In the protocol choose email. As you can see we have many options to get notified here.
In the endpoint box, type an email you can use to receive the notification. Ideal approach will be using a lambda as an endpoint and log the messages in the CloudWatch. But for this article I am using email to keep things simple.
Once you successfully create the subscription you will receive an email to the address you provided with a confirmation link.
3. Set up SNS Event Destination for SNS Event Publishing
We need to enable SNS Event Destination service to get notified about specific email events . We can only use this within a configuration set only. Configuration Set allows us to publish email sending events such as bounces, complaints, deliveries, sent emails, and rejected emails
- Open SES Console, Choose Configuration Sets from left pane.
- Click on Create Configuration Set , give it a name
- Choose the configuration set you created.For Add Destination choose SNS as the destination type.
- Make sure you select Enabled , Give a name and choose the event types that you want to get notified. Rendering Failure event will be triggered only if you are sending your emails using Templates. I will explain about this in a while.
- Select the topic that you created in the previous steps.
To read more about email sending please refer following link to my medium article
(since I can't properly add my code snippets here )