SMTP guides
Django
Point Django's send_mail at usend over SMTP.
Django's mail framework needs only settings changes. Settings used on this page: host smtp.usend.et, port 587 (STARTTLS) or 465 (implicit TLS), username usend, password your API key. Reference: Send with SMTP.
Settings
import os
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.usend.et"
EMAIL_PORT = 587
EMAIL_USE_TLS = True # STARTTLS on 587
EMAIL_HOST_USER = "usend"
EMAIL_HOST_PASSWORD = os.environ["USEND_API_KEY"]
DEFAULT_FROM_EMAIL = "Acme <[email protected]>"
SERVER_EMAIL = "Acme <[email protected]>" # sender for error emails to ADMINS
EMAIL_TIMEOUT = 30For implicit TLS use EMAIL_PORT = 465, EMAIL_USE_SSL = True and remove EMAIL_USE_TLS. Set only one of the two; Django refuses both.
DEFAULT_FROM_EMAIL and SERVER_EMAIL must be on a verified domain, or usend refuses the message with 554.
Send
from django.core.mail import EmailMultiAlternatives, send_mail
send_mail("Hello from usend", "It works!", None, ["[email protected]"])
msg = EmailMultiAlternatives("Your invoice", "Attached.", None, ["[email protected]"])
msg.attach_alternative("<p>Attached.</p>", "text/html")
msg.attach_file("invoice.pdf")
msg.send()Passing None as the sender uses DEFAULT_FROM_EMAIL.
Authentication emails
Django's password reset, and packages such as django-allauth, use the same backend, so they send through usend with no extra work.
Tips
- Run it under a task queue for bulk mail. Django opens a new SMTP connection per
send_mailcall unless you share one withget_connection()andsend_messages([...]). - Locally, switch to
django.core.mail.backends.console.EmailBackendso development never sends real mail. - Want the email
idback, scheduling or tags? Use the API: Send with Django.