How To Send Email In A Django Application
At some points, most web applications need to send out mails to their users to improve interaction with clients. It could be an automated mail to notify the user when an order is placed, or a welcome mail after a successful registration, etc.
Sending mails in a Django application is quite easy because only a few lines of code are needed. Let’s dive into coding!
For us to be able to send out mails, we will need an email service. You can use any email service using SMTP. For simplicity, we would be using a gmail account to send out emails to our recipient in this tutorial.
Let’s start configuring our Django Application to send emails.
Checkout Getting Started With Django In This Tutorial
Head over to the settings.py
file in your root project directory and add the following code snippets
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'mygmail@gmail.com' # your gmail account
EMAIL_HOST_PASSWORD = 'password' # your password
EMAIL_PORT = 587
EMAIL_USE_TLS = True
Up here, we have added some lines of code:
The SMTP backend is the default configuration inherited by Django. EMAIL_HOST
is different for different mail provider. Here are are using 'smtp.gmail.com' because we Gmail is our email provider.
Note that we are having some sensitive information up here. Like EMAIL_HOST_PASSWORD
. You should never commit this to a public repository. You should environment variables instead.
Send An Actual Email.
To send an actual email, we will need to import send_mail
in the beginning of the file, most like in our views.py
. The send_mail
module is then used for sending out emails.
from django.core.mail import send_mail
send_mail('subject', 'body of the message', 'sender@example.com', ['receiver@example.com'], fail_silently=False)
Look closely up here, you would notice that we pass some parameters into the send_mail
function. Those parameters were hardcoded, meaning they are passed as constant values. In a real life application, you would want to pass in variables for dynamic contents.
Check this:
emailSubject = "Hi, " + (request.user) "
senderEmail="order@example.com"
receiverEmail = (request.user.email)
send_mail(emailSubject, EmailBody, senderEmail, [receiverEmail], fail_silently=False)
When fail_silently is False, send_mail() will raise an Exception if an error occurs.
Note that the receiver parameter passed into the send_mail is a Python List. This means we can add more that one recipient for our email:
send_mail(emailSubject, EmailBody, senderEmail, ['admin1@example.com', 'buyeremail@gmail.com', 'admin2@example.com'], fail_silently=False)
Quick Note: For You To send out mail with your gmail account, You need to make some settings to your Gmail account which you are using to send out emails.
Follow these two links: Allow Less Secure App and Allow Access To Your Gmail Account
Ready to elevate 🚀 your Python 🐍 skills? Check out YouTube for exclusive content. Consider subscribing to stay updated.
Using send_mass_mail()
to handle mass emailing
send_mail()
opens a connection to the mail server each time it’s executed, while send_mass_mail() uses a single connection for all of its messages. This makes send_mass_mail() slightly more efficient.
With send_mass_mail() only one connection to the mail server would be opened:
To Use send_mass_mail() we need to import in our file (views.py)
as we did earlier for send_mail()
from django.core.mail import send_mass_mail
message1 = ('Subject here', 'Here is the message', 'from@example.com', ['first@example.com', 'other@example.com'])
message2 = ('Another Subject', 'Here is another message', 'from@example.com', ['second@test.com'])
send_mass_mail((message1, message2), fail_silently=False)