【发布时间】:2019-06-22 01:22:47
【问题描述】:
我正在尝试编写一个 python 代码来向已注册列表的人发送一些时事通讯。 我的问题是标题“To:”部分!我无法将电子邮件作为“收件人:”地址放入列表中,当收件人打开电子邮件时,他们看不到“收件人:”标题中的电子邮件地址。这是我正在谈论的截图:http://tinypic.com/r/zlr7sl/9
我不是程序员,我只是想学习一些新东西。我的英语并不完美。我希望你能理解我。
from smtplib import SMTP
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
host = 'smtp.server.com'
port = 587
usr = 'USERNAME'
pwd = 'PASSWORD'
from_email = 'SENDER)EMAIL'
my_list = open('slist.txt', 'r')
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Subject'
msg['From'] = from_email
msg['To'] = '' # <<<<<I want to put emails in slist.txt in this header one by one while sending the emails.
msg.add_header('reply-to','reply-to')
plain_text = 'Testing Message'
html_text = '''\
>>> HTML CODE<<
'''
part1 = MIMEText(plain_text, 'plain')
part2 = MIMEText(html_text, 'html')
msg.attach(part1)
msg.attach(part2)
server = SMTP(host, port)
server.ehlo()
server.starttls()
server.login(usr, pwd)
try:
for emails in my_list:
server.sendmail(from_email, emails, msg.as_string())
print('!!!YEAHH!!!')
except:
print('***OPS***')
server.close()
【问题讨论】:
-
我已经尝试过 msg['To'] = ' '.join(my_list)。使用此选项,我没有收到任何错误,但没有发送电子邮件
-
slist.txt中有什么内容?my_list的值是多少?emails应该是电子邮件列表。 -
这部分代码可以解决我的问题,谢谢,我在你分享的链接上找到了。 my_list = open('slist.txt', 'r').readlines() 对于 my_list 中的行: emails = rows.rstrip() to = emails msg['To'] = Header(to, 'utf-8')
标签: python email html-email bulk