Send an email with a user other than EMAIL_HOST_USER written in settings in django

Use send_mail.

python


from django.core.mail import send_mail

send_mail(subject, message, from, [to], fail_silently=False, auth_user=ANOTHER_HOST_USER, auth_password=ANOTHER_HOST_PASSWORD)

However, this can only be sent by To.

I want to send it by BCC ~ ~.

Use EmailMessage class

send_mail () is also a light wrapper over the EmailMessage class.

python


from django.core.mail import EmailMessage

email = EmailMessage(subject, message, from, [to], [bcc])
email.send(fail_silently=False)

I was able to send it with BCC! !! However, if you do this, it will be sent by EMAIL_HOST_USER in settings.

Manually open the connection

Normally, send_mail () will open it when you send it, but you should open it yourself.

python


from django.core.mail import EmailMessage, get_connection

connection = get_connection(fail_silently=False, username=ANOTHER_HOST_USER, password=ANOTHER_HOST_PASSWORD)
connection.open()
email = EmailMessage(subject, message, from, [to], [bcc], connection=connection)
email.send()
#If you want to send more than one
# connection.send_messages([email])
#Since it was opened manually, it is also manually closed.
connection.close()

Then, a user other than the user described in settings was able to send the mail of the destination BCC!

Are you wrong? correct? ?? ??

Recommended Posts

Send an email with a user other than EMAIL_HOST_USER written in settings in django
Note: Send an email with Django
Send an email with Excel attached in Python
[Django] Manage settings like writing in settings.py with a model
[Python] Send an email with outlook
I tried to send a registration completion email from Gmail with django.
Send an email with Amazon SES + Python
Implement a Custom User Model in Django
To automatically send an email with an attachment using the Gmail API in Python
Send an email to Spushi's address with python
Start Django in a virtual environment with Pipenv
Build a Django environment with Vagrant in 5 minutes
Configure a module with multiple files in Django
How to use fixture in Django to populate sample data associated with a user model
Create a flag in settings that will be True only when testing with Django
Extract elements other than a specific index with Numpy
Get a list created by a user other than yourself
Run pandas-highcharts display_charts in an environment other than jupyter
Stop an instance with a specific tag in Boto3
Send email with Python
Try running python in a Django environment created with pipenv
[Python] Send an email from gmail with two-step verification set
Until I return something with a line bot in Django!
How to extract other than a specific index with Numpy
Create an authentication feature with django-allauth and CustomUser in Django
Create a Todo app with Django ① Build an environment with Docker
What to do if pip --user returns an error in a virtual environment created with pyenv