Testing email functionality using DebugMail on Django

October 05 2023

This article is for beginners and assumes some basic knowledge of Python (Django).

Cover for post. DebugMail - Fake SMTP server for testing emails

Preparing the project for integration with DebugMail
Setup and data for capturing mail with DebugMail
In conclusion

One of the tasks in the software testing process is testing and debugging email functionality. Having a good and reliable tool for this purpose is essential, and that’s where DebugMail comes to the rescue.

DebugMail is a virtual mail server that allows you to test applications and websites without the need to install an SMTP server. To start using it, all you need to do is change the host, port, username and password settings in your mail configuration to match the values provided by the DebugMail service. 

More details of the integration steps are described below.

Preparing the project for integration with DebugMail

Step 1. Install the virtual environment of the project:

python -m venv venv

Step 2. Activate the virtual environment:

command for Windows

venv\Scripts\activate

Step 3. After setting up the virtual environment, let’s proceed with installing Django:

pip install Django

Step 4. Let’s create a project:

django-admin startproject mysite

Step 5. Create the templates directory in the root of mysite.

Screenshot of the created templates directory in the process of DebugMail integration for testing the project's email in the staging environment.

Step 6. Create the main.html file (for now, without any inheritance, just for displaying mail submission).

Screenshot of the main.html file creation in the DebugMail integration process for testing project emails in the staging environment.

Step 7. Write the main template for mail submission:

<!DOCTYPE html>
<html>
<head>
    <title>Testing email with DebugMail via Django</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
</head>
<body>
    <div class="container py-5">
        <h1>Testing email with DebugMail via Django</h1>
        <br>
        <form action="{% url 'main_send_email_view' %}" method="post">
            {% csrf_token %}
            <div class="mb-3">
                <label for="email" class="form-label">Email:</label>
                <input type="email" class="form-control" aria-describedby="emailHelp" id="email" name="email" required>
            </div>
            <br>
            <div class="mb-3">
                <label class="form-label" for="subject">Subject:</label>
                <input class="form-control" type="text" id="subject" name="subject" required>
            </div>
            <br>
            <div class="mb-3">
                <label for="exampleFormControlTextarea1" class="form-label">Message:</label>
                <textarea class="form-control" id="exampleFormControlTextarea1" name="message" rows="4" cols="50" rows="3" required></textarea>
            </div>
            <br>
            <button type="submit" class="btn btn-primary mb-3">Send</button>
        </form>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
</body>
</html>

Step 8. Create the views.py file in the main directory of mysite, where our urls.py is located.

Screenshot of the created views.py file in the mysite directory during the DebugMail integration process to test the project's email in staging environment.

Step 9. Write the necessary code in the views.py file to display the page:

from django.shortcuts import render
from django.core.mail import send_mail
from django.http import HttpResponse

def main_send_email_view(request):
    if request.method == 'POST':
        email = request.POST.get('email')
        subject = request.POST.get('subject')
        message = request.POST.get('message')

        # Here you can add additional logic for data processing

        try:
            send_mail(subject, message, 'your_email@example.com', [email])
            return HttpResponse('Email successfully sent!')
        except Exception as e:
            return HttpResponse(f'Oops! Something went wrong: {str(e)}')

    return render(request, 'main.html')

Step 10. Add the required path in urls.py file.

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.main_send_email_view, name="main_send_email_view"),
]

Step 11. Customize the settings.py file to output templates.

import os 

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Step 12. Copy the snippet from DebugMail with the necessary details and paste it at the end of settings.py.

Setup and data for capturing mail with DebugMail

Firstly, let’s go to DebugMail first and create an account.

Screenshot of the landing page of DebugMail, an fake SMTP service for testing project emails in the staging environment.
Try DebugMail for free or a 2-week trial of the Silver plan, depending on your needs and project.
Screenshot of the login screen in the fake SMTP service DebugMail for testing project emails in the staging environment.
Create an account using your Google, Github, or email credentials.

Secondly, create a team and create a project for that team. The project will display test emails from your Python project.

Screenshots of service elements to create a team and project in DebugMail to test project emails in the staging environment.
On the left side of the workspace, the team and project creation elements will open one by one.

Thirdly, in the created project, open the settings and copy the SMTP data for connecting to the staging project.

Screenshots of the project settings path in DebugMail for testing project emails in the staging environment.
Each project has its own individual settings.
Screenshots of the technology selection in the project settings within DebugMail for testing project emails in the staging environment.
Select Python (Django) as the technology to integrate with DebugMail.

Copy a similar snippet (with individually generated username and password) to a convenient location of your choice:

EMAIL_HOST = "app.debugmail.io"
EMAIL_HOST_USER = "0b942c89-5270-47e9-bb84-6bf178951feb"
EMAIL_HOST_PASSWORD = "88eaae8d-3a43-4240-bc52-56e321739f0b"
EMAIL_PORT = "25"

Scrolling down below the project settings page, you can find the SMTP credentials.

Screenshot of the location of project credentials in the settings of DebugMail for testing project emails in a staging environment.
Select the integration method that is most convenient for you.

Take these credentials and paste them into the appropriate fields in the code:

# EMAIL
EMAIL_HOST = "app.debugmail.io"
EMAIL_HOST_USER = ""
EMAIL_HOST_PASSWORD = ""
EMAIL_PORT = ""

Step 13. Navigate to mysite in the console:

cd mysite

Step 14. Run the project:

python manage.py runserver

Step 15. After that, go to http://127.0.0.1:8000/. And test it out!

Screenshot of the confirmation of sending an email from the development environment.

Step 16. Check the results.

Finally, we have done all the steps. And now is the time to check the results. If you check your DebugMail project, you have received the email successfully as below.

Screenshot of the entire DebugMail workspace.
DebugMail workspace.
Screenshots of different ways to view email content in the DebugMail service.
View the content of the email in a way that is is most convenient for you.

In conclusion

I hope this guide has helped you successfully integrate DebugMail into your project, making it easier to test functionality related to sending email. If you have any questions or need help with integration, just email us. We are always ready to help!

PS. If you need more information to compare DebugMail with other popular email services, we recommend checking out our blog articles on DebugMail vs. Google SMTP and DebugMail vs. Yahoo Mail SMTP.

Subscribe to DebugMail blog and receive notifications only about new articles

    What’s New

    Testing email functionality using DebugMail with Rust

    How to integrate DebugMail and test outgoing emails in a Rust project.
    Cover of the article Testing email functionality using DebugMail on Golang. Golang programming language mascot - bear.

    Testing email functionality using DebugMail on Golang

    How to integrate DebugMail and test outgoing emails in a Golang project

    Testing email functionality using DebugMail on Django

    This article is for beginners and assumes some basic knowledge of Python.

    To report a bug or an issue please email us.

    We are happy to get any feedback.