Sending emails in Django using Mailtrap

Video thumbnail

Sending emails from Django is one of those tasks that sooner or later we all need to implement. Whether it's to confirm accounts, recover passwords, or notify the user of actions, configuring email sending correctly is essential as well as other more advanced tasks such as creating chats with full-duplex communications using Websockets and Django.

We will need to send emails to test the password reset module; configuring an email server is outside the scope of this book and because not all readers are able to set up their own or already have an email server, we will use a service that allows simulating email sending:

https://mailtrap.io/

Why do you need to send emails in a Django application?

Django makes communication with users via email incredibly easy. Some common scenarios are:

  • Registration confirmation or account verification.
  • Password recovery or reset.
  • Notifications of events, orders, or important changes.

Initial configuration for sending emails in Django

Once registered and a development server created, we must configure the following variables:

customlogin\settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.mailtrap.io'
EMAIL_HOST_USER = '<YourUserMailTrap>'
EMAIL_HOST_PASSWORD = '<YourPasswordMailTrap>'
EMAIL_PORT = '2525'
EMAIL_USE_TLS = True

Which determine the host, user, password, and port of the email server respectively; with this, we are ready for the application to be able to send emails.

Using Mailtrap for email sending tests

Mailtrap is ideal for testing sending without affecting real users. What it does is intercept the emails and display them in a development inbox.

Quick steps:

  • Register at mailtrap.io
  • Create an Inbox or development environment.
  • Copy the SMTP credentials it provides you.

Paste them into your project's settings.py (as in the block above).

This way, any email Django tries to send will arrive in your test inbox in Mailtrap, without risk or the need for a real server.

Sending your first email from Django

Once the server is configured, you can send your first email with the send_mail() function.

from django.core.mail import send_mail
send_mail(
   subject='Prueba de correo desde Django',
   message='¡Hola! Este es un correo de prueba enviado desde Django.',
   from_email='no-reply@miapp.com',
   recipient_list=['usuario@correo.com'],
   fail_silently=False,
)

What each parameter does:

  • subject: email subject.
  • message: message content.
  • from_email: sender's address.
  • recipient_list: list of recipients.
  • fail_silently: if True, does not show errors upon failure.

✅ Tip: always test with several fake recipients and check that Mailtrap receives them correctly.

Sending HTML emails and with attachments

If you want to take a step further, Django also allows sending emails with HTML format or attachments using EmailMessage or EmailMultiAlternatives.

from django.core.mail import EmailMessage
email = EmailMessage(
   subject='Welcome',
   body='<h1>¡Hi!</h1><p>thanks for register in the app.</p>',
   from_email='no-reply@miapp.com',
   to=['user@email.com']
)
email.content_subtype = 'html'
email.send()

Tips and best practices for production environments

When your application is ready to send real emails, keep the following in mind:

  • Use environment variables (for example, with python-decouple) to avoid exposing credentials in the code.
  • Enable TLS or SSL as required by your SMTP provider.
  • Control bulk sending to avoid being marked as spam.
  • Recommended services: SendGrid, Amazon SES, Gmail SMTP (with App Passwords).

Common errors when sending emails in Django (and how to fix them)

Error    Probable Cause    Solution
smtplib.SMTPAuthenticationError    Incorrect username or password    Check credentials or use environment variables
ConnectionRefusedError    Incorrect port or server down    Verify EMAIL_PORT and connection
TimeoutError    Network or firewall issues    Try in another environment or change the port
Email doesn't arrive    Gmail or real server block    Use Mailtrap or check SMTP logs

Conclusion and next steps

Sending emails in Django is simpler than it seems if you understand the settings.py structure and use appropriate tools for each phase.

Mailtrap is your best ally for safe testing; then you can move to a real SMTP service in production.

Keep practicing with:

  • Attachments and custom templates.
  • Asynchronous messages with Celery.
  • Integration with external services like SendGrid or SES.

In summary, with a few lines of code and a well-configured environment, you can professionalize email sending in Django without complications.

Frequently Asked Questions

1. Can I use Gmail to send emails from Django?
Yes, but you must generate an App Password and enable TLS on port 587.

2. What is the difference between send_mail and EmailMessage?
send_mail is simpler; EmailMessage allows HTML, attachments, and greater customization.

3. How to test without a real server?
With Mailtrap: it intercepts and displays development emails without risk.

4. Why isn't my email being sent in Django?
Verify your credentials, SMTP port, and that you don't have network blocks.

Next step, implementing a search field and filters with forms in Django so you can filter those emails.

I agree to receive announcements of interest about this Blog.

We will use the Mailtrap service to send test messages, we will configure Mailtrap and the Django app to test password recovery.

| 👤 Andrés Cruz

🇪🇸 En español