smtplib 主题未显示,消息中已编码主题
smtplib subject not showing, encoded subject in message
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import pdb
def emailSender(emailId, password, input):
fromaddr = emailId
emailFile = open(input, "r") # open the generated email file with read only access
msgTxt = ""
server = smtplib.SMTP('outbound.___.com')
server.starttls()
server.login(fromaddr, password)
for line in emailFile:
if line.__contains__("$to$"):
toaddr = line[4:]
elif line.__contains__("$cc$"):
cc = line[4:]
elif line.__contains__("$bcc$"):
bcc = line[5:]
elif line.__contains__("$subj$"):
subject = line[6:]
elif line.__contains__("$%$"):
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
subject = subject.encode('utf-8')
msg['Subject'] = "hello world"
msg['Cc'] = cc
msg.attach(MIMEText(msgTxt, 'plain'))
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
msgTxt = ""
elif not line.__contains__("--------------------------------------------------------------------"):
msgTxt += line
server.quit()
if __name__ == '__main__':
password = "****"
emailId = "****"
input = "input"
emailSender(emailId, password, input)
总体目标是获取一个包含电子邮件、抄送、主题和消息的文本文件并发送该电子邮件,并对文本文件中生成的几封电子邮件执行此操作。
我正在阅读来自 msgTxt 文本文件的电子邮件。我还从文本文件中获取主题 toaddr & cc。它可以很好地发送带有来往显示的电子邮件,但不会显示电子邮件的主题。我试过调试 smtplib 和 ssl 文件,但似乎无法弄清楚。调试时,味精已正确设置所有属性,但未传输到电子邮件。任何帮助深表感谢。我已经坚持了一段时间了。感谢您的帮助。
--毕然
我要冒险了,但是:
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "hello world"
..."fromaddr" 来自代码,而不是文件。因此,"subject" 是您定义的来自文本文件的第一个键之后定义的第一个键。
假设文件中的文本行有错误的行终止符,这被附加到 toaddr
。然后 From 起作用了,因为它是硬编码的。也可以工作,因为行终止符尚未出现。它紧跟在 To 之后。
此处留下两行 terminator-equivalents,因此 SMTP 停止处理 headers 并且主题现在被视为邮件的一部分 body。
要验证,请尝试 hard-coding "To" 地址一次。如果可行,您需要 trim() 从文件中获取的行。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import pdb
def emailSender(emailId, password, input):
fromaddr = emailId
emailFile = open(input, "r") # open the generated email file with read only access
msgTxt = ""
server = smtplib.SMTP('outbound.___.com')
server.starttls()
server.login(fromaddr, password)
for line in emailFile:
if line.__contains__("$to$"):
toaddr = line[4:]
elif line.__contains__("$cc$"):
cc = line[4:]
elif line.__contains__("$bcc$"):
bcc = line[5:]
elif line.__contains__("$subj$"):
subject = line[6:]
elif line.__contains__("$%$"):
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
subject = subject.encode('utf-8')
msg['Subject'] = "hello world"
msg['Cc'] = cc
msg.attach(MIMEText(msgTxt, 'plain'))
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
msgTxt = ""
elif not line.__contains__("--------------------------------------------------------------------"):
msgTxt += line
server.quit()
if __name__ == '__main__':
password = "****"
emailId = "****"
input = "input"
emailSender(emailId, password, input)
总体目标是获取一个包含电子邮件、抄送、主题和消息的文本文件并发送该电子邮件,并对文本文件中生成的几封电子邮件执行此操作。
我正在阅读来自 msgTxt 文本文件的电子邮件。我还从文本文件中获取主题 toaddr & cc。它可以很好地发送带有来往显示的电子邮件,但不会显示电子邮件的主题。我试过调试 smtplib 和 ssl 文件,但似乎无法弄清楚。调试时,味精已正确设置所有属性,但未传输到电子邮件。任何帮助深表感谢。我已经坚持了一段时间了。感谢您的帮助。
--毕然
我要冒险了,但是:
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "hello world"
..."fromaddr" 来自代码,而不是文件。因此,"subject" 是您定义的来自文本文件的第一个键之后定义的第一个键。
假设文件中的文本行有错误的行终止符,这被附加到 toaddr
。然后 From 起作用了,因为它是硬编码的。也可以工作,因为行终止符尚未出现。它紧跟在 To 之后。
此处留下两行 terminator-equivalents,因此 SMTP 停止处理 headers 并且主题现在被视为邮件的一部分 body。
要验证,请尝试 hard-coding "To" 地址一次。如果可行,您需要 trim() 从文件中获取的行。