How to Set up and Send Email with Django



Python


In this article, we show how to set up and send email with Django.

In order to send email, we need use the SMTP (Simple Mail Transfer Protocol).

In order to do this, you will need an email service account.

In this example, we are going to use the gmail email service.

So, if you don't have a gmail account, it's easy and free to sign up for one. If you are using another email service provider, then you just have to look up the SMTP domain, whether it uses TLS connection, and the email port number that email service provider uses.

In order for this gmail email account to work, you'll probably have to allow the account to take on less secure apps. This is can be found at the following link on the gmail website: Gmail Less Secure Apps.

So the first thing we have to do is define a few variables in the settings.py file of our Django project.


Settings.py File

So we're going to define a few variables in our setting.py file that will allow us to send email out from our Django server.

We must define whether the email service provider uses TLS (transport layer security). TLS is a cryptographic protocol that provides communications security over a computer network.

We then must define the email server of the email service provider we are using. For gmail, this is, smtp.gmail.com

We then must define which email account we are going to use to send out outgoing email.

We then must specify the password for this email account.

Lastly, we need to define the port (of communication) that the email service uses. For gmail, this is port 587.

All of this shown below.



So now we have all of our email settings set up within our settings.py file.

Now let's create an app that contains a form in which a user enters in an email and receives an email upon entering the submit button.

Let's call this page contact.html

You can create this page out of an existing app that you have or you can just create a new app and set this up.

So this page will have the following code. We will just ask for the user's name and email address.



So this creates the form that we will use.

Now let's go to the forms.py page and create the form.


forms.py File

We need to create our form now in the forms.py file.

In this form, we create 3 fields: name, comment, and email.



So we've now created a ContactForm in the forms.py file of our app.

The form has 3 fields: name, email, and comment. If you want, you can make certain fields optional, but in this code, I chose to make all 3 required.

Now we just have to go into the views.py file to get the data from these fields and send out an email.

The content of the views.py file is shown below.



So the first thing we have to do is import render, since we are rendering template files.

Next, we must import the ContactForm we created in the forms.py file.

Since we are sending email in this view, we must import the send_mail() function from django.core.mail

We then define our function-based view, contactview

We create 3 variables, name, email, and comment, which will eventually contain the values of the form fields that the user enters in.

We create a variable, form, and set it equal to ContactForm(request.POST or None)

This form variable now contains the ContactForm.

If the form is valid (if all fields have been entered in and the email entered is a valid email), then we store the data from the form fields into the variables, name, email, and comment.

The comment is the most complex. We want to extract the user's name and the email from the form and put it in the comment. This is because we want to be able to contact the user back. We then put in the user's comment after a few newline characters.

We then send out the email with the send_mail() function.

The sendmail() function takes in 4 parameters. The first is the title, in this case, "Comment by a User"

The second is the body of the email.

The third is the email account that is sending out the email. This is the account specified in the settings.py file. The email you put into this account must match the email in the settings.py file.

The fourth is the recipient of the email. In the case of a Contact form, we are the recipient, so I put in the same email address.

Once all of these are in place, we can now send email.

And this is how we can send email with Django.



Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...