[Django]-EmailBackend for sending email through multiple SMTP in Django

47👍

If you want to override the provided settings you can just create your own connection and provide it to send_email or EmailMessage

from django.core.mail import get_connection, send_mail
from django.core.mail.message import EmailMessage
# TODO: Insert clever settings mechanism
my_host = ''
my_port = 587
my_username = ''
my_password = ''
my_use_tls = True
connection = get_connection(host=my_host, 
                            port=my_port, 
                            username=my_username, 
                            password=my_password, 
                            use_tls=my_use_tls) 

send_mail('diditwork?', 'test message', 'from_email', ['to'], connection=connection)
# or
EmailMessage('diditwork?', 'test message', 'from_email', ['to'], connection=connection).send(fail_silently=False)

Update: Make sure to close the connection after use, @michel.iamit answer points to code showing the connection is cached for smpt. @dhackner answer shows how to automatically close a connection using with statement.

23👍

I would slightly modify Daniel Backman’s answer to use a context manager like in the docs. It will take care of open() and close() calls by itself.

from django.core.mail import get_connection, send_mail
from django.core.mail.message import EmailMessage

with get_connection(
    host=my_host, 
    port=my_port, 
    username=my_username, 
    password=my_password, 
    use_tls=my_use_tls
) as connection:
    EmailMessage(subject1, body1, from1, [to1],
                 connection=connection).send()

https://docs.djangoproject.com/en/dev/topics/email/#email-backends

2👍

Sorry, can’t comment directly on the rest of the answers, not enough rep.

Just one addition:

connection = get_connection(host=my_host, 
                                port=my_port, 
                                username=my_username, 
                                password=my_password, 
                                user_tls=my_use_tls)

should be

connection = get_connection(host=my_host, 
                                port=my_port, 
                                username=my_username, 
                                password=my_password, 
                                use_tls=my_use_tls)

It’s only a problem if you are NOT using TLS (and you should), since it defaults to True.

1👍

It might be an idea to have a look at the email backend of django, and create a customised one.

here is the code: https://github.com/django/django/blob/master/django/core/mail/backends/smtp.py

This example shows how you can make a custom one (this one uses SMTP_SSL in stead of SMTP):

https://gist.github.com/jsummerfield/1486891

and I forked that one, to combine both (so I can use one backend for different sites with different mail settings):

https://gist.github.com/iamit/5415771

Not sure if that will work, but as you see all of the codes above returns on opening the connection if there is already a connection…
So maybe there is the start for your solution…. don’t do that, close the open connection and reopen with different settings… (not sure if that works).

0👍

use a different configuration for each site.

one way is to have, in addition to your settings.py, a settings_SITENAME.py for each site.
these files will have the custom mail server settings.
each site will have settings_local.py be a symlink to it’s own custom settings file

then put this stanza at the bottom of settings.py:

try:                                                                                                                                                                            
    from settings_local import *                                                                                                                                                
except ImportError:                                                                                                                                                             
    pass  

now you have per-site custom settings

0👍

In case it helps someone. If you need to send emails from different accounts within the same HOST (the one defined in the settings.py) you can make it simpler without having to create a new connection:

send_mail(subject, message, from_email, recipient_list, auth_user="my_secondary_email@example.com", auth_password="mypassword")

0👍

I created a lib for this cases…

https://pypi.org/project/django-multi-mail-backend/

allow multiples backends (including smtp servers)

Leave a comment