Skip to main content
Resend Docs

Search documentation

Type to search this documentation.

On this pageOverview

Send emails using Django with SMTP

Before you start, you'll need:

  1. Setup your environment

    Create and activate your new virtualenv.

    Bash
    virtualenv venv
    source venv/bin/activate

    Install dependencies.

    Shell
    pip install -r requirements.txt

    Set your RESEND_API_KEY environment variable by running.

    Shell
    export RESEND_API_KEY="re_xxxxxxxxx"
  2. Send email using Django's SMTP EmailMessage

    Set the necessary attributes in your settings.py file.

    Python
    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    RESEND_SMTP_PORT = 587
    RESEND_SMTP_USERNAME = 'resend'
    RESEND_SMTP_HOST = 'smtp.resend.com'

    Use Django's get_connection and EmailMessage

    Python
    import os
    from django.conf import settings
    from django.http import JsonResponse
    from django.core.mail import EmailMessage, get_connection
    
    # Sample Django view
    def index(request):
    
        subject = "Hello from Django SMTP"
        recipient_list = ["delivered@resend.dev"]
        from_email = "onboarding@resend.dev"
        message = "<strong>it works!</strong>"
    
        with get_connection(
            host=settings.RESEND_SMTP_HOST,
            port=settings.RESEND_SMTP_PORT,
            username=settings.RESEND_SMTP_USERNAME,
            password=os.environ["RESEND_API_KEY"],
            use_tls=True,
            ) as connection:
                r = EmailMessage(
                      subject=subject,
                      body=message,
                      to=recipient_list,
                      from_email=from_email,
                      connection=connection).send()
        return JsonResponse({"status": "ok"})

Django SMTP Example

See the full source code.

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu