Home > database >  Send multiple mails hiding recipent in python
Send multiple mails hiding recipent in python

Time:09-21

I need to send multiple mails with 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()

Thanks in advance!

CodePudding user response:

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.

CodePudding user response:

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

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()
  • Related