Sending email in Django using GMail

Another day, another beginner wanting to use GMail for free email sending, while this is entirely valid for a small project, please don't do this for your next startup. These days use a cloud service like AWS SES, Sendgrid or Mailgun.

With the above disclaimer out the way, this is how you send email using Gmail. First your email settings in settings.py needs to look as follows:

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = "your.app.email.replace.me@gmail.com"
EMAIL_HOST_PASSWORD = '<see next paragraph for this>'

If you use your default password that you sign in with you will get an error like this:

SMTPAuthenticationError at /
(535, b'5.7.8 Username and Password not accepted. For more information, go to\n5.7.8  https://support.google.com/mail/?p=BadCredentials l9-20020a17090615c900b00a555be38aaasm7938755ejd.164 - gsmtp')

To solve this you first need to ensure that the Google account has multi-factor authentication setup correctly, either with a device and/or authenticator app codes. Then you need and "App Password", to create one visit this link, add a name for your password, click "Create", then copy the 16 characters that appear on the screen in to the password setting with no spaces.

EMAIL_HOST_PASSWORD = 'abcdefghijklmnop'

You should then be able to send email via GMail. Finally revisiting my first sentence, below are a list of email providers that I would recommend instead, expect to pay some money at some point as with most things when it comes to hosting an application: