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:
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()
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 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.
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowMindbowser played a crucial role in helping us bring everything together into a unified, cohesive product. Their commitment to industry-standard coding practices made an enormous difference, allowing developers to seamlessly transition in and out of the project without any confusion....
CEO, MarketsAI
I'm thrilled to be partnering with Mindbowser on our journey with TravelRite. The collaboration has been exceptional, and I’m truly grateful for the dedication and expertise the team has brought to the development process. Their commitment to our mission is...
Founder & CEO, TravelRite
The Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork