Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

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.

Avatar image for brianashley

brianashley on Nov. 20, 2022

Thanks for another good course Darren. I had issues before coming across this lecture and learning that accounts is a preserved Django authentication path. I have a few questions. I’m trying to confirm the properties of User fields in the django.contrib.auth.models User. /admin and the python shell give me the values easily enough from which I’m inferring that username is required and probably unique. And email, which we added, and other names aren’t required. Is this correct? And about the previous chapter of testing email reset. I tried running (venv) $ python -m smtpd -n -c DebuggingServer localhost:1025 from my venv after changing the settings file EMAIL_BACKEND = ‘django.core.mail.backends.console.EmailBackend’ But no luck with any output to the screen. I’m just wondering if this is something others have found.

Avatar image for brianashley

brianashley on Nov. 21, 2022

Did you miss a step with the mailgun set up Darren? I had to register my email for receiving the reset. Maybe I overlooked that in your video. My question is what happens in production? Do I still need a 3rd party redirect service?

Avatar image for Darren Jones

Darren Jones RP Team on Nov. 23, 2022

@brianashley - On the mailgun setup, I didn’t have to register my email other than in the regular way to set the account up, but it may vary depending on the email used with mailgun, and it’s possible that their setup has changed since the course was made - on logging in now I have to confirm a code which was sent to the email which I didn’t have to do previously.

On the other comment, if I’m reading that right, I’d say you are correct - username is generally unique (although there are some exceptions depending on the user model and authentication that you have set up), and the email field will be required (for obvious reasons!) - the others will be down to app preferences but shouldn’t alter the functionality.

On the email setup front, I would check that the other email settings in settings.py are what you need for the local SMTP Daemon to work with, if you’re seeing nothing from it.

Avatar image for Craig

Craig on Feb. 9, 2023

Hi there,

Whilst sending the password reset I keep getting:

SMTPRecipientsRefused (550, b‘5.7.1 Relaying denied’)}

I tried adding different users with various emails I have.

I can’t seem to get past this message…

Any help would be greatly appreciated.

Avatar image for Nancy

Nancy on July 24, 2023


I just wanted to say that I’m loving the course so far, but I hit a snag with Mailgun. It looks like I set up a Mailgun account a long time ago with some other course so they disabled my free account. When I reached out to them to get the account re-enabled they refused to re-enable the account without one verified domain. I don’t have my own domain so I won’t be able to use Mailgun. I’ll skip this section and continue on with the next.

Thank you! Nancy

Avatar image for william

william on Sept. 29, 2023

You can use sendgrid as an option. You need to create an account, get the API key, the HOST_USER is ‘apikey’. Documentation that might be helpful, docs.sendgrid.com/for-developers/sending-email/django.


LOGIN_REDIRECT_URL = 'dashboard'

# EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'

Become a Member to join the conversation.