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.
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.
https://docs.djangoproject.com/en/dev/topics/email/#email-backends
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).
Sorry, can't comment directly on the rest of the answers, not enough rep.
Just one addition:
should be
It's only a problem if you are NOT using TLS (and you should), since it defaults to True.
If you want to override the provided settings you can just create your own connection and provide it to
send_email
orEmailMessage
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.
use a different configuration for each site.
one way is to have, in addition to your
settings.py
, asettings_SITENAME.py
for each site. these files will have the custom mail server settings. each site will havesettings_local.py
be a symlink to it's own custom settings filethen put this stanza at the bottom of
settings.py
:now you have per-site custom settings