Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Sending Emails to the Outside World

In this lesson, you’ll set up a Mailgun account.

00:00 Sending Emails to the Outside World. At the moment, your application isn’t actually sending emails. They’re just being viewed in the console as Django is using the console email backend set up earlier in the course.

00:15 It would be far more useful to send emails to actual email addresses. One way to do this is using Mailgun. For this step, you’ll need a Mailgun account. The basic version is free and will let you send emails from a rather obscure domain, but it will work for the purpose of this course.

00:37 After you create your account, go to the dashboard page and scroll down until you reach Sending domains. There you will find your sandbox domain. Click on the domain.

00:47 You’ll be redirected to a page where you can select the way you want to send emails. Choose SMTP. Here you’ll find the credentials for your account.

01:02 You should find the following values: SMTP hostname, port, username, and default password. All you need to do is add these values to the settings file. Keep in mind that you should never include any credentials directly in your code.

01:20 Instead, store them as environment variables and read their values in Python. On Linux and macOS, you can add an environment variable in the terminal as seen on-screen.

01:40 On Windows, you can run this in the command prompt.

01:48 Repeat the same process for EMAIL_HOST_PASSWORD, and remember to export the variables in the same terminal window where you run the Django server.

02:01 After both variables are added, update the settings as seen on-screen.

02:17 The values of EMAIL_HOST and EMAIL_PORT should be the same for all sandbox domains, but you have to use your own username and password.

02:33 There’s also one additional value called EMAIL_USE_TLS that you need to set to True. To check if this works, create a new user with your own email address.

02:45 Go to the admin page and log in as the admin user. Go to Users and click ADD USER and select any username and password you like. Click Save and continue editing.

03:03 Then insert the same email address you used for your Mailbox account into the email address field and save the user.

03:13 After creating this user, navigate to the password reset page and enter your email address and press Send. The process of sending an email will take a bit longer than with the local server. After a few moments, the password reset email should arrive in your inbox.

03:31 It may be in your spam folder, so don’t forget to check that too. As you can imagine, Mailgun’s free account has limitations. You can only send emails to five email addresses, which you need to pre-approve.

03:48 To have a fully functional solution, you’d either need to upgrade your account or look for a different email provider that would fulfill your needs. However, by changing the relevant settings you’ve already looked at in this section, you should be able to get up and running quickly with any other email service provider. In the next section of the course, you’ll take a look at a different way to log in to your site by using a GitHub account.

Become a Member to join the conversation.