0

Whenever I run my code the recipients are in the bcc field rather than the To field (When I check my gmail inbox)

I have no idea what can be the reason. How can I stop the recipients being in the BCC field. and make them appear in the TO field Here is my code

import smtplib 
import getpass
import time
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

smtpVar = smtplib.SMTP ("smtp.gmail.com", 587)
fromEmail = "myemail@gmail.com"
toEmails = ["myemail@gmail.com", "secondemail@gmail.com"]

msgVar = MIMEMultipart()
msgVar ["From"] = fromEmail
msgVar ["To"] = toEmails

# I am getting the contents of the body of the email from a text file
fileOpen1 = open ("EmailBody.txt", "r")
msgBody = MIMEText (fileOpen1.read())
fileOpen1.close()

smtpVar.ehlo()
smtpVar.starttls()

passwordVar = getpass.getpass (prompt="Enter Password  : ")
smtpVar.login ("myemail@gmail.com", passwordVar)

smtpVar.sendmail (msgVar ["From"], msgVar ["To"] , "Subject: Test Email-.......[Email Date/Time] " + time.strftime("%Y" + "-" + "%m" + "-" + "%d" + "_" + "%H" + ":" + "%M" + ":" + "%S") + "\n" + msgBody.as_string())

smtpVar.quit()
print ("\n\n.....message sent successfully!!!\n\n")
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I am genuinely curious, why write `"%Y" + "-" + "%m" + "-" + "%d" + "_" + "%H" + ":" + "%M" + ":" + "%S"` instead of `"%Y-%m-%d_%H:%M:%S"`? Seems lie a lot of extra work without any benefit of clarity. – SethMMorton Oct 30 '17 at 19:59
  • Looks like you are losing the "headers" (`From` and `To`) [18.1.11. email: Examples](https://docs.python.org/2/library/email-examples.html#email-examples) – crashmstr Oct 30 '17 at 20:04
  • @SethMMorton Thanks for pointing that out..Didnt know you could do that in the way you showed..Thanks will change code! crashmstr Amazingly enough that link isnt very comprehensive for a beginner like myself I am afraid. I can understand just bit and pieces of it. Anyway Barmar explained it really well below.. Thanks for your help as well! – Michael Hilburt Oct 30 '17 at 21:34

1 Answers1

0

smtplib doesn't create the message headers for you. The from and to arguments are just used for the envelope. If you want them to appear in the header, you have to do that yourself.

smtpVar.sendmail (msgVar ["From"], msgVar ["To"] , "From: " + msgVar["From"] + "\nTo: " + msgVar["To"].join(", ") + "\nSubject: Test Email-.......[Email Date/Time] " + time.strftime("%Y-%m-%d_%H:%M:%S") + "\n" + msgBody.as_string())
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hi @barmar THANKS for your help!...your script had the **bold**.split**bold** in the wrong place I am afraid but I fixed it anyway. Here it is `code` smtpVar.sendmail (msgVar ["From"], msgVar ["To"].split(",") , "From: " + msgVar["From"] + "\nTo: " + msgVar["To"] + "\nSubject: Test Email-.......[Email Date/Time] " + time.strftime("%Y-%m-%d_%H:%M:%S") + "\n\n" + msgBody.as_string())`code` Anyway my other problem now is that in the message body some additional text has come up. how can I get rid of this?.. This is the additional text=Content-Type: text/plain; charset="us-ascii" MIME-Version: – Michael Hilburt Oct 30 '17 at 21:29
  • There's no need for `split`, `msgVar["To"]` is a list, not a string. – Barmar Oct 30 '17 at 21:31
  • The extra text is because of the extra newline I added. I thought `msgBody` was just the body, I didn't know it included header lines. – Barmar Oct 30 '17 at 21:32
  • **bold**THANKS VERY MUCH**bold** again @Barmar.. yeh I removed all instances of `code`split`code` and removed 1 of the \n. Everything works well now! Just out of curiosity, where can I learn these things online for myself so that I dont have to post here and bother other people for help with my code? ..**bold**thanks again!**bold** – Michael Hilburt Oct 30 '17 at 21:42
  • You're welcome. Why do you write the word **bold** instead of putting the bold markup around the words? – Barmar Oct 30 '17 at 21:47
  • Ha ha sorry I am a bit new hear..I misread the instructions. I thought you had to put the ** bold ** ** bold ** instead of putting it like **this**..thanks for helping out on that too...really appreciate it! – Michael Hilburt Oct 30 '17 at 21:55