所以我正在研究邮件脚本,以便从文本文件向用户列表发送邮件。它工作正常,遍历列表,每个用户都会收到一封电子邮件 - 除了一个细节,主题将从除脚本发送的最后一封电子邮件之外的所有电子邮件中删除。这里发生了什么?
#!/usr/bin/env python
import sys
from smtplib import SMTP
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
targets = open('targets.txt', "rb")
def mailme():
for line in targets:
msg = MIMEMultipart('alternative')
m_source = '<sender@address.com>'
m_target = '%s' % line
smtp_server = 'mail.server.com'
smtp_port = '25'
msg['From'] = m_source
msg['To'] = m_target
msg['Subject'] = "Subject"
smtp = SMTP()
smtp.set_debuglevel(0)
smtp.connect(smtp_server, smtp_port)
message_text = ('Some Text for %s' % (m_target)
)
message_html = ("""<body>
<h1>Some HTML for %s</h1>
</body>"""
) % (m_target)
txt = MIMEText(message_text, 'plain')
web = MIMEText(message_html, 'html')
msg.attach(txt)
msg.attach(web)
smtp.sendmail(m_source, m_target, msg.as_string())
smtp.quit()
if __name__ == "__main__":
mailme()
sys.exit(0)
答案 0 :(得分:1)
我建议OP将msg['Subject']
放在msg['From']
之前,它解决了问题。我建议,因为python MIME email examples/tutorials所有msg['Subject']
都来自msg['From']
和msg['To']
。
我认为这是一个可以遵循的模板。如果有人知道为什么这种方法解决了这个问题,请对此有所了解。