Sending Email Templates Using Amazon SES With Django Rest Framework

We can send email through multiple ways, but in this blog, I will be covering, sending an email by using Amazon SES (Simple Email Service). Concerning this, I will also add about templates, how to send emails by creating our own templates and how it is helpful to send an email by using templates.

This blog will cover all the points required to send mail using templates, points required are mentioned and explained below:

  1. Creating and adding templates to settings
  2. Sending dynamic data to HTML templates
  3. Setting up the data required to send an email
  4. Creating SMTP credentials to AWS, SES and login function
  5. Creating sender email and domain to AWS, SES and function to send an email
  6. Creating And Adding Templates To Settings

Related Post: Integrating SendGrid With Django Rest Framework

Creating And Adding Templates To Settings

Creating a template is not a difficult task, for that you only need to create a template folder in your project directory so that you can add your HTML file inside it. As shown below, I have created sample.html inside the templates folder.

We need to create an HTML file to design your email. After creating a template folder you need to add it in Django settings, In Django settings, you need to modify your TEMPLATES and add your folder path in DIRS. Check how to send email with django templates.

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR+'/templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

Sending Dynamic Data To HTML Templates

Now we have created a template and HTML file, so we have to send dynamic data to email templates and add templates to email. We have to use the render_to_string function to send dynamic data and add templates to email.

django.template.loader import render_to_string
render_to_string("file_name.html", {“dynamic_data”':”dynamic_data”})

Setting Up The Data Required To Send An Email

For sending an email, you need to set up the data means you have to add subject, body, recipient required to send an email, we are required to add following methods in it.

-MIMEMultipart:- It is used when sending HTML and TEXT data; It simply means when we have to send multiple types of data. How to use this method is explained below:

To use this method, we need to import it.we have stored it in one variable to use it to add subject and recipient.

from email.mime.multipart import MIMEMultipart

msg = MIMEMultipart('alternative')

msg['Subject'] = subject

msg['From'] = email.utils.formataddr((sender_name, sender_email))

msg['To'] = recipient_email

–MIMEText:- It is used to send a significant type of text and HTML data as we have an email body to send an email, so we require this function.

We have to add HTML data that we have created to send in an email.

from email.mime.text import MIMEText

MIMEText(body_text, 'plain')

MIMEText(body_html, 'html')

 

-Setting up a server: To set up a server you will require server name, you can get as shown in below image.

Smtplib is required to set up with a server

import smtplib

server = smtplib.SMTP(host_server_name, port)

– EHLO:- EHLO command is used to identify the domain name of the sending host to SMTP.

server.ehlo()

– StartTLS:- StartTLS is a protocol command used to inform the email server that the email client wants to upgrade from an insecure connection to a secure one.

server.starttls()

 

Creating SMTP credentials is important to step in the email sending, for that you need to follow below steps:
-Go to AWS

-Select AWS SES service

-Go to SMTP settings

-Click on Create my SMTP credentials as shown in below image

-Then you will get SMTP credentials Those credentials are used to login as shown below

server.login(smtp_username, smtp_password)

Creating Sender Email And Domain To AWS SES And Function To Send Email

The last step of email sending is to send email using email function but it requires sender email so we have to create and verify sender email as shown in below steps:
-Go to AWS

-Select AWS SES service

-Select Email addresses

-Click on verify email address as shown in below image

To send an email you will require sender email, message, recipient email address as shown below:

server.sendmail(sender_email, recipient_email, msg.as_string())

-Last step is to close the server after sending the email

server.close()

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from django.core.mail import send_mail

import smtplib

import email.utils

def send_email():

sender_email = 'abcd@gmail.com'

sender_name = 'abcd'

recipient_email = 'xyz@gmail.com'

smtp_username = "abcxyz"

smtp_password = "aabbcc"

host_server_name= "email-smtp.us-east-1.amazonaws.com"

port = 587

subject = 'temp subject'

body_text = ("add body text")

body_html = render_to_string("file_name.html", {'url': url})

msg = MIMEMultipart('alternative')

msg['Subject'] = subject

msg['From'] = email.utils.formataddr((sender_name, sender))

msg['To'] = recipient

part1 = MIMEText(body_text, 'plain')

part2 = MIMEText(body_html, 'html')

msg.attach(part1)

msg.attach(part2)

server = smtplib.SMTP(host_server_name, port)

server.ehlo()

server.starttls()

server.ehlo()

server.login(smtp_username, smtp_password)

server.sendmail(sender_email, recipient_email, msg.as_string())

server.close()
coma

Conclusion

Sending email in the Django framework is one of the simple processes. This blog is helpful to design our own templates, reducing the cost of creating the templates. We securely send different types of data and reduce efforts to send an email.

Rishikesh D

Software Engineer

Rishikesh is a Full-stack developer with 3+ years of experience. He has experience in web technologies like AngularJS, ReactJS. His expertise is building Python integrated web applications, creating REST APIs with well-designed, testable and efficient and optimized code. He loves to learn new technologies.

Discuss Your Ideas With Our Django Expert

Keep Reading

Keep Reading

Launch Faster with Low Cost: Master GTM with Pre-built Solutions in Our Webinar!

Register Today!
  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?