In this article we will continue with Python Django user registration emails – showing you how to send system/app emails from you Django app.
At this stage – you should have a running Django Application, with a user profile registration process already designed. Check out our previous articled on this:
Typically when a user creates an account on your web application – you want to send them system emails. There are many reasons why you would want to send a user emails, including:
- Letting them know that you have registered their account successfully
- Giving them useful information about your web applications, telling them the next steps etc
- Letting your users know if any significant changes have been made on their account
- Helping a user reset their password, if they forget it
- Send users regular system updates
- Subscription information
Creating a HTML Email Template
The first step in sending HTML emails from Django – is to find the ideal email template to work with. The main requirements of a HTML email template is: inline CSS. You need to include all the CSS code inside of the HTML file.
You can get some HTML template codes here to start with:
https://github.com/mailchimp/email-blueprints/tree/master/templates
Download the file, save it inside of your templates folder inside of your Django project.
HTML Template Context – Sending data to the template
You can send data to the template using standard templating methods, like so:
Inside your HTML template
<h1>{{object.title}}</h1>
<h2>{{object.subtitle}}</h2>
Data that you will send to the template in JSON format:
{
"object": { "title": "this is a title", "subtitle": "this is a subtitle" }
}
You will pass this entire object as the context, we will get to that later.
Set up the email settings inside of the settings.html file
We need an SMTP mail server that we will use to send our emails. Once the mail service is set-up, get the credentials and add the following to the settings.py file at the bottom of the file:
. . . . . .
#Email settings details
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.careers-portal.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'no-reply@careers-portal.com'
EMAIL_HOST_PASSWORD = '************'
. . . . . . .
Building the Send Email Function
Read the Django documentation on sending emails: https://docs.djangoproject.com/en/3.1/topics/email/
Create a new file called functions.py
File imports:
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
We are going to be using the EmailMultiAlternatives class from django.core.mail. This class allows us to send multiple-alternative type emails. In our case, we will be sending a plain text mail and HTML text mail. Some mail servers only display plain text – in this case, they will display the plain-text alternatives.
Most servers display HTML emails, including gmail. In this case, they will have the option to display the HTML email to the client. HTML is rich in formatting, it allows us to include styling, images, font and all sorts of styles to make our emails look interesting and customised for the user.
Send Email function
Creating the email data:
email = {
"title": "Thank your for registering with Careers Portal",
"shortDescription": "Welcome to Careers Portal, South Africa's Leading Job
Search Engine. These are the next steps.",
"subtitle": "Careers Portal - Start your job search process today",
"message": "You have successfully registered with Careers Portal. You can
now login in to your profile and start creating a profile. We have
thousands of jobs just waiting for you to apply. If you experience any
issues feel free to contact our support at support@careers-portal.co.za.>
}
subject = '[Careers Portal] Welcome to Careers Portal'
to_email = 'anyemail@gmail.com"
This is all the data we will need for now, we then need to create the email function as seen below. The function takes in the data as specified above.
def sendEmail(email, subject, to_email):
from_email = settings.EMAIL_HOST_USER
text_content = """
{}
{}
{}
regards,
Careers Portal Support
""". format(email['shortDescription'], email['subtitle'], email['message'])
html_c = get_template('basic-email.html')
d = { 'email': email }
html_content = html_c.render(d)
msg = EmailMultiAlternatives(subject, text_content, from_email, to_email)
msg.attach_alternative(html_content, 'text/html')
msg.send()
We have to specify the following:
- from_email: The sender of the email. This is us, so we refer to the variable inside of our settings.py file
- text_content: This is the body of the email in plain-text format
- html_c: The loaded html file, using get_template function, which was imported at the top of the file. The name ‘basic-email.html’ must match the name we gave the html file – that we saved inside of the templates folder of our django app.
- d: This is the context. We basically just add the email object with the data that needs to go in to the HTML file.
- html_content: we create this variable by rendering the html_c file with the context d.
- Finally we create the mgs – mail object, attach the HTML and send it off.
This code is suitable for sending one mail, if you are coding it in the view function for example.
For multiple mails – like sending a group email, we need more robust code.
Python Django User Registration Emails
You can now apply this function anywhere in your Django code to integrate sending emails inside of you registration process.
Check out our full video tutorial here:
Leave a Reply