How to dynamically change the mail sender in Django? I use send mail. Now the sender is always one - specified in settings in EMAIL_HOST_USER = 'test@gmail.com'
Use this in your settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
And then send emails this way:
from django.core import mail
from django.core.mail import get_connection
def send_my_email(host, port, username, password, subject, body, from_, to_list):
conn_params = dict(
host=host,
port=port,
username=username,
password=password
)
email_params = dict(
subject=subject,
body=body,
from_email=from_,
to=to_list
)
with get_connection(**conn_params) as connection:
message = mail.EmailMessage(**email_params, connection=connection)
message.send(fail_silently=False)