pythonmime-mail

python mails being sent out have a blank TO field


I have a python script that sends out emails. The emails are being sent out with no issues.

However, when I get the mails and open them up, their "to:" field do not show the recipients. It's just blank. I want to show all the recipients. And since I'm not using any "bcc" configuration in my script, I'm befuddled as to how this could be.

Here's the code I'm using:

#!/usr/bin/env python

you = [ "MyEmail1@MyEmail.com", "MyEmail2@MyEmail.com" ]
for eachrecord in fformatErrMessage:
    # Preparing all variables #
    msg = MIMEMultipart()
    msg['Subject'] = subjectMsg
    part1 = MIMEText(text, 'plain')
    part2 = MIMEText(html, 'html')
    msg.attach(part1)
    msg.attach(part2)
    s = smtplib.SMTP('localhost')
    s.sendmail(me, you, msg.as_string())
    s.quit()

I have tried replacing the you with:

you = "MyEmail1@MyEmail.com"

thinking maybe the list is the problem - but that did not work.


Solution

  • When sending emails, servers do not look at the To:-Field. Instead, email servers have a separate way of transmitting recipient addresses, sometimes called the "envelope", which they use to route and deliver mail. The client never receives that envelope, it instead uses the To:-field in the message headers.

    The sendmail() call simply sets the addresses of the SMTP envelope. If you want the To:-field to show up in the mail itself, you must set the appropriate header:

    msg['To'] = "foo@bar.com"

    This is, btw, how BCC works: The addresses on the envelope is simply not repeated in the message headers.