EmailBackend for sending email through multiple SMTP in Django

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)