我正在尝试使用smtp和MIME在python中发送邮件。我可以发送一封附件很好的电子邮件,但是当我尝试发送许多邮件时,它会向列表中的第一个人发送相同的电子邮件,列表中的人数。
# -*- coding: iso-8859-1 -*-
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
msg = MIMEMultipart()
msg['Subject'] = 'Email From Python with attachment'
msg['From'] = 'email1.com'
# That is what u see if dont have an email reader:
msg.preamble = 'Multipart massage.\n'
# This is the textual part:
part = MIMEText("Hello im sending an email from a python program with an attachment")
msg.attach(part)
# This is the binary part(The Attachment):
part = MIMEApplication(open("test.pdf","rb").read())
part.add_header('Content-Disposition', 'attachment', filename="test.pdf")
msg.attach(part)
# Create an instance in SMTP server
smtp = SMTP('localhost')
#open data file
fo = open("data.txt", "rw+")
#send email to names in file
for line in fo:
msg['To'] = line
# Send the email
smtp.sendmail(msg['From'], msg['To'], msg.as_string())
从环顾四周看似问题可能在于msg ['To']期望字符串和sendmail期待列表。如果可能的话,我想在一个循环中这样做。如果使用循环不是标准范例,我会尝试使用什么。