我有以下代码,它从 email.txt 创建一个列表,然后向每个项目发送电子邮件。我使用列表而不是循环。
#!/usr/bin/python
# -*- coding= utf-8 -*-
SMTPserver = ''
import sys
import os
import re
import email
from smtplib import SMTP
from email.MIMEText import MIMEText
body="""\
hello
"""
with open("email.txt") as myfile:
lines = myfile.readlines(500)
to = [line.strip() for line in lines]
try:
msg = MIMEText(body.encode('utf-8'), 'html', 'UTF-8')
msg['Subject']= 'subject'
msg['From'] = email.utils.formataddr(('expert', 'mymail@site.com'))
msg['Content-Type'] = "text/html; charset=utf-8"
conn = SMTP(SMTPserver)
conn.set_debuglevel(False)
conn.login('info', 'password')
try:
conn.sendmail(msg.get('From'), to, msg.as_string())
finally:
conn.close()
except Exception, exc:
sys.exit( "mail failed; %s" % str(exc) )
当用户收到电子邮件时,他看不到 TO 字段,因为该字段将为空。我希望收件人在电子邮件中看到一次收件人字段。我该怎么做?