Tutorial

Send Email with Python (Django) via SMTP

smtplib for a plain script, or the Django email backend for an app — both over the same authenticated SMTP relay.

By the Sendcorex team·Updated Sep 21, 2026·8 min read

Option 1: Plain Python with smtplib

import smtplib, os
from email.mime.text import MIMEText

api_key = os.environ["SENDCOREX_API_KEY"]

msg = MIMEText("You are in.", "html")
msg["Subject"] = "Welcome!"
msg["From"] = "Your App <hello@yourdomain.com>"
msg["To"] = "customer@example.com"

with smtplib.SMTP("smtp.sendcorex.com", 587) as server:
    server.starttls()
    server.login(api_key, api_key)
    server.send_message(msg)

Option 2: Django's email backend

Point Django's built-in backend at the relay in settings.py:

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.sendcorex.com"
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ["SENDCOREX_API_KEY"]
EMAIL_HOST_PASSWORD = os.environ["SENDCOREX_API_KEY"]
DEFAULT_FROM_EMAIL = "Your App <hello@yourdomain.com>"

Then send from anywhere in your app with send_mail() or, for HTML, EmailMultiAlternatives:

from django.core.mail import EmailMultiAlternatives

email = EmailMultiAlternatives("Welcome!", "You are in.", to=["customer@example.com"])
email.attach_alternative("<h1>You are in.</h1>", "text/html")
email.send()

Common errors

SymptomFix
SMTPAuthenticationErrorConfirm the API key env var is set and used for both user and password.
SMTPServerDisconnectedCall starttls() before login(), not after.

Send your first email in minutes

Create a free account, verify your domain and paste in the code above.

No credit card required