Configure and Send Email in Django (for both Development and Production)

·

0 min read

In some case, you might need to send email to users for different purposes. For me, the top use cases of sending emails to user are to verify user's email, reset password and send notification.

Configuring SMTP Server and Django Settings

In order to get your email delivered to intended recipients, you have to first get a SMTP server working. It doesn't have to be locally deployed, you can use email service providers that you have subscription with. In this section, I will walk you through configuration for both dummy SMTP server, and Gmail SMTP Server.

Use Django's built-in Dummy Backend

Django provide a Dummy email backend, as the name suggests, it simply work as a stub to your email backend. To enable this, add the following into your settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.dummy.EmailBackend'

Django Dummy Backend provides no access to requests received, there is no way to view the emails your app being delivered.

Second Alternative: Mailhog

Mailhog is a email testing till for developers. It provides intuitive Web UI and API for easy access to emails being sent. How I use it is usually coupled with Docker as they provide docker image mailhog/mailhog. The Docker image expose port 1025 for SMTP and port 8025 as Web UI.

One good thing of Mailhog is you can optionally relay your email to real email address.

If you want to add it into your docker-compose.yml, simply add the following:

smtp-server:
  image: mailhog/mailhog
  expose:
    - 1025
    - 8025
  ports:
    - '1025:1025'
    - '8025:8025'

The hostname of your Mailhog will be smtp-server and is accessible within your containers specified in your __docker-compose.yml-

Now, we will add the following codes into our settings.py:

# Don't forget to remove Dummy Email Backend if you set it previously
EMAIL_HOST = 'smtp-server'  # Your Mailhog Host
EMAIL_PORT = '1025'

Use Gmail SMTP Server

You can use Gmail SMTP server if you have subscription to their service (G Suite or your personal Gmail account).

Add the following settings into your Django:

# Gmail SMTP Server
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = '587'
EMAIL_HOST_USER = '<your-gmail-address>'
EMAIL_HOST_PASSWORD = '<your-gmail-password>'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

Don't forget to replace the <> placeholder with your Gmail address and password. I strongly suggest you set your password with Environment Variable instead of exposing raw password in your settings.py.

Use Environment Variable for Password

Set your password in system environment, in most Linux system, simply use:

export MY_PASSWORD="<your_password_here>" # Remove spaces before and after '='

or in Dockerfile (if you are using Docker):

ENV MY_PASSWORD = '<your_password_here>'

And access it with:

EMAIL_HOST_PASSWORD = os.environ.get('MY_PASSWORD')   # Replace MY_PASSWORD with your ENV variable.

Use send_mail() in Django

Django django.core.mail module is a tiny wrapper of smtplib in Python. It provides handy methods to make sending email easier.

Suppose we want to send an email to , we will be using the send_mail() method:

from django.core.mail import send_mail

send_mail(
    subject = "Test Email",
    message = "This is a test email",
    from_email = None,   # This will have no effect is you have set DEFAULT_FROM_EMAIL in settings.py
    recipient_list = ['<your_recipient_email>'],    # This is a list
    fail_silently = False     # Set this to False so that you will be noticed in any exception raised
)

send_mail() wraps the EmailMultiAlternatives object, which is a subclass of EmailMessage. You can refer to Django's documentations or source code for more control over sending email in Django.

First posted on 2017-10-07