In this lesson, you’ll set up a Mailgun account.
Sending Emails to the Outside World
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.
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.
The values of
EMAIL_PORT should be the same for all sandbox domains, but you have to use your own username and password.
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.
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?
@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.
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.
Become a Member to join the conversation.
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.