EmailBackend for sending email through multiple SM

2019-02-01 21:24发布

问题:

Getting Django to send an email is nicely explained here using standard settings as shown below.

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = "mail.mysmtpserver.somewhere"
#EMAIL_PORT
EMAIL_HOST_USER = "my@login"
EMAIL_HOST_PASSWORD = "mypassword"
#EMAIL_USE_TLS = True

Then using django.core.mail.EmailMessage to send it of.

How ever, what if you are running multiple sites and need each of these to send email through their own SMTP server (or just different login in the same SMTP server)?

Searching for a EmailBackend like this or a way to do it with the current backend did not produce any satisfactory results.

回答1:

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.



回答2:

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



回答3:

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).



回答4:

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.



回答5:

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