Sending Emails using AWS SES [NodeJS]
- March 19, 2018
AWS have tons of services for everything. SES stands for Simple Email Service (Yeas it sends Emails). In this article I will go through
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.
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)
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
ToAddresses: This param takes an array of email addresses that you want to send your mail.
Body.Html.Data: HTML message template. All the styles have to be inline
Body.Subject.Data: Subject of the email
Source: Email address of the sender
ConfigurationSetName: Configuration Set name that we created in previous topic (Ignore if you don’t want to receive any notifications)
2. Send emails using templates
In SES you can define a email template which contains Subject line, text and Html parts of the email body. Also we can include replacement variables to send personalized emails.I feel like this is very clean and neat approach since we don’t have to maintain html parts inside our project code.
I. Using AWS CLI
aws ses create-template --cli-input-json <<path to template.json>>
(Make sure your CLI version is up to date to execute above command)
II. Using SES API
Run above command and if you put everything right you will be getting a templateId and you can view the templates you created in the SES Console.One annoying thing about this console is you can’t edit or view the templates from console it self(I know such a pain).
TemplateName — Name of the email template,When we are sending emails we will be referring to this.
SubjectPart — Subject of the email.What’s in the curly braces are the replacement tags. Once we are sending the email we can tell what values should be in these tags.
HtmlPart — Html part of the email. You can use most of the css as far as they are inline.replacement tags can be added here too.
TextPart — If the receiver is not able to render the HTML tags this will be shown.
Once you have completed the above step you can see your template name Here
Alright now all we have to do is to send the email.
Source — Email address of the sender
ConfigurationSetName — Configuration Set name that we created in previous topic (Ignore if you don’t want to receive any notifications)
Template — Name of the email template we created earlier
Destination.ToAddresses: This param takes an array of email addresses that you want to send your mail.
TemplateData — A JSON object that contains key-value pairs. The keys must be replacement tag name in the template.
Once you send out the emails you will receive a messageId and a requestId.Also if you have added the configuration steps you should be receiving events about email as well.
So that’s all folks,Please let me know if you come across any issues in comments I will try to help out as far as I can 😀