Testing Advanced Emails

I recently ran into a problem when I was trying to send test emails with PDFs attached to them.

In Django, the default development email backend which prints the sent emails to the console works fine for most cases, but doesn't really work for more complex ones; eg. sending attachments or displaying HTML formatted emails.

I found that the simplest solution was to use a test email server called Mailpit (a fork of mailhog).

How to use it?

  • Install it on MacOS with: brew install mailpit
  • To start the server run: mailpit --smtp-auth-allow-insecure --smtp-auth-accept-any
  • Go to http://localhost:8025/ where you will find a dashboard with all the emails which are sent and received by the server
  • Change your Django settings like this:
    # Change this
    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
    
    # To this
    EMAIL_USE_TLS = False
    EMAIL_HOST = 'localhost'
    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_PORT = 1025