Guides / Django

Test Django emails locally

RustMail is a local SMTP mail catcher. Your Python app keeps sending mail the way it does in production, and every message lands in a web UI instead of a real inbox. Four steps, about two minutes.

  1. Start RustMail

    One container, SMTP on port 1025 and the web UI on 8025.

    docker run -p 1025:1025 -p 8025:8025 smyile/rustmail:latest
  2. Point Django at it

    In settings.py, send mail to localhost:1025 with no TLS and no credentials.

    EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
    EMAIL_HOST = "localhost"
    EMAIL_PORT = 1025
    EMAIL_USE_TLS = False
  3. Send a test email

    Then open localhost:8025: the message is already in the inbox, with its HTML, headers and links.

    python manage.py sendtestemail [email protected]
  4. Assert on it in CI

    In GitHub Actions, start RustMail, run tests that send mail with the settings.py settings from step 2, then fail the job when the email never arrives.

    - name: Start RustMail on localhost:1025
      uses: rustmailapp/rustmail-action@v1
      with:
        mode: start
    
    - name: Run the tests that send mail over SMTP
      run: pytest e2e/
    
    - name: Fail if the welcome email never arrived
      uses: rustmailapp/rustmail-action@v1
      with:
        mode: assert
        assert-count: 1
        assert-subject: "Welcome"

Good to know

  • Django's test runner always swaps in the locmem backend, so manage.py test never reaches SMTP. Assert on mail from end-to-end tests that drive a running server, such as Playwright with pytest.

Other frameworks

LaravelRuby on RailsNode.jsSpring Boot