每当我运行我的代码时,收件人都在bcc字段而不是 To 字段(当我检查我的Gmail收件箱时)
我不知道可能是什么原因。 如何阻止收件人进入BCC字段。并使它们出现在TO字段中 这是我的代码
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")
答案 0 :(得分:0)
smtplib
不会为您创建邮件标头。 from
和to
参数仅用于信封。如果您希望它们出现在标题中,您必须自己完成。
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())