Send multiple mails hiding recipent in Python

Question:

How can I send multiple mails with a hidden recipient?

I can currently send mails, but people who receive it can see the recipient.

My code looks like this:

import smtplib
from email.message import EmailMessage

email_subject = "Good morning"
sender_email_address = "[email protected]"
receivers_email_address = ['[email protected]', '[email protected]']
email_smtp = "smtp.office365.com"
email_password = "MyPassword"

# Create an email message object
message = EmailMessage()

# Configure email headers
message['Subject'] = email_subject
message['From'] = sender_email_address
message['To'] = ", ".join(receivers_email_address)

# Read file containing html
with open('message.html', 'r') as file:
   file_content = file.read()

# Add message content as html type
message.set_content(file_content, subtype='html')

# Set smtp server and port
server = smtplib.SMTP(email_smtp, '587')

# Identify this client to the SMTP server
server.ehlo()

# Secure the SMTP connection
server.starttls()

# Login to email account
server.login(sender_email_address, email_password)

# Send email
server.send_message(message)

# Close connection to server
server.quit()
Asked By: Juan Jose Acevedo

||

Answers:

I made a loop to send individually. Instead of sending it to multiple people in a group.

import smtplib
from email.message import EmailMessage

email_subject = "Good morning"
sender_email_address = "[email protected]"
receivers_email_address = ['[email protected]', '[email protected]']
email_smtp = "smtp.office365.com"
email_password = "MyPassword"



# Read file containing html
with open('message.html', 'r') as file:
    file_content = file.read()

# Set smtp server and port
server = smtplib.SMTP(email_smtp, '587')

# Identify this client to the SMTP server
server.ehlo()

# Secure the SMTP connection
server.starttls()

# Login to email account
server.login(sender_email_address, email_password)

# Send email
for receiver in receivers_email_address:
    # Create an email message object
    message = EmailMessage()
    message['Subject'] = email_subject
    message['From'] = sender_email_address
    message['To'] = receiver
    # Add message content as html type
    message.set_content(file_content, subtype='html')
    server.send_message(message)

# Close connection to server
server.quit()
Answered By: Jeson Pun

You don’t need to send multiple messages. Just don’t put the recipients explicitly in the headers.

The following implements this by putting the recipients in the Bcc: header.

import smtplib 
from email.message import EmailMessage 

email_subject = "Good morning" 
sender_email_address = "[email protected]" 
receivers_email_address = ['[email protected]', '[email protected]']

email_smtp = "smtp.office365.com" 
email_password = "MyPassword" 

message = EmailMessage() 
message['Subject'] = email_subject 
message['From'] = sender_email_address
# The message needs to have a To: header
# - putting yourself is an old convention
message['To'] = sender_email_address
message['Bcc'] = ",".join(receivers_email_address)

with open('message.html', 'r') as file:
   file_content = file.read()
message.set_content(file_content, subtype='html')

with smtplib.SMTP(email_smtp, '587') as server:
    server.ehlo() 
    server.starttls() 
    server.login(sender_email_address, email_password) 
    server.send_message(message) 
    server.quit()

Of course, if you really need to To: header to indicate the actual recipient, you will need to generate a unique message for each of them.

This relies on the SMTP server to read and strip off the Bcc: header. If you can’t rely on yours to do that, you can explicitly use the legacy sendmail method of the smtplib module, which lets you explicitly pass in the list of actual recipients separately from the message. Then you can also avoid the pesky copy to yourself.

To briefly recap, SMTP doesn’t really care what’s in the To: or Cc: headers; the actual list of recipients is communicated separately, before you submit the actual message. This is called the SMTP envelope.

Answered By: tripleee
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.