A few of the programs I’ve created rely on an automated emailer script to notify myself and others of updates. The script uses Python MIME submodules. An issue I’ve been having to date has been keeping the recipient email addresses anonymous.
When sending emails where I’m happy for all email addresses to be visible to recipients (such as to myself and Bob), the code looks like the following:
from_addr = '[email protected]' to_addr = ['[email protected]', '[email protected]'] msg = MIMEMultipart() msg['From'] = from_addr msg['To'] = ", ".join(to_addr) msg['Subject'] = 'Test Automation Email'
This is the header information code. When the email sends, both Bob and I will be able to see one another on the email.
After some reading and searching online, my understanding was that to make the recipients anonymous, I could use the BCC field as follows:
from_addr = '[email protected]' to_addr = ‘email@example.com’ recipients = ['[email protected]', '[email protected]'] msg = MIMEMultipart() msg['From'] = from_addr msg['To'] = to_addr msg['Bcc'] = ", ".join(recipients) msg['Subject'] = 'Test Automation Email'
Note the differences. I’ve set the
To address to be the same as the
from_addr and created a new item
['Bcc'] which then joins the emails listed in
This code actually works. The problem is that I don't get the expected functionality of BCC (recipients aren’t listed). That is, Bob can see that I was BCC’d on the email and vice versa! In the email he receives, the header actually shows “bcc” and shows my email address, completely defeating the purpose!
Further research was required. As I’ve mentioned, we’re building the header information of the email when we use the
msg['To'] and other options for MIME.
It turns out anything you put in the header is, by design, visible to the recipient. Any email address I assign to `msg['Bcc']’ is again, by design, supposed to be visible to all recipients.
The workaround I’ve found that works for me is to completely omit my list of recipients from the MIME header information altogether and instead include it in the
The current sendmail parameters are:
smtp_server.sendmail(from_addr, to_addr, text)
With the workaround it’s:
from_addr = '[email protected]' to_addr = ‘firstname.lastname@example.org’ recipients = ['[email protected]', '[email protected]'] msg = MIMEMultipart() msg['From'] = from_addr msg['To'] = to_addr msg['Subject'] = 'Test Automation Email' smtp_server.sendmail(from_addr, [to_addr] + recipients, text)
Note: You can find the full emailer script in this article.
The idea is that the only information visible to the recipient when they receive the email is the information we populate MIME
The information in sendmail is not visible to recipients so any addresses we decide to tack on to the “To” argument of sendmail remain hidden.
And THAT is the behaviour I wanted all along!
Keep Calm and Code in Python!
Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.
"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H
"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B
"This is now my favourite first Python go-to reference." - Anthony L
"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S