Python smtplib:只有其中的第一封电子邮件到达目的地 [英] Python smtplib: only the first email out of several arrives to destination
问题描述
我整理了一个函数 send_email
[1],用于发送支持纯文本和html消息的电子邮件。效果很好,但是我有一个问题,我不太了解如何调试。我的系统将 sendmail
作为其MTA。
I have put together a function send_email
[1] to send emails with support for plain-text and html messages. It works well, but I have an issue I don't quite know how to debug. My system has sendmail
as its MTA.
该函数在for循环中调用,如下所示:
The function is called in a for loop, as follows:
for data in data_set:
subject, message = process(data) # Irrelevant stuff
send_email(subject, message, "fixed@email.address")
在 smtplib
[1]我看到对 send_email
的所有调用均已成功完成。奇怪的行为是:
In the debug output of smtplib
[1] I see that all calls to send_email
completed successfully. The weird behavior is:
- 如果
消息
是短的(我测试过单行),所有已发送的邮件实际上到达fixed@email.address
- 如果
消息
是不短的(也就是说,我使用真正的process_data
函数生成的多行),只有第一 smtplib 的调试输出报告了每封电子邮件的成功,电子邮件确实到达了,而其他电子邮件则没有。 - 如果
消息
同样不短 但,目标地址为不同每条消息,然后所有消息到达其预期的目的地。
- If the
message
is "short" (I tested with a single line), all sent messages actually arrive tofixed@email.address
- If the
message
is "not short" (that is, the multiple lines I generate with the realprocess_data
function), only the first email does arrive, while the others don't, even though the debug output ofsmtplib
in [1] reports success for each and all of the emails. - If the
message
is equally "not short" but the destination address is different for each message, then all messages arrive to their intended destinations.
对于后一种情况, for
循环如下所示: p>
For the latter case, the for
loop would look like:
addresses = ["fixed@email.address", "fixed.2@email.address", ...]
for data, addr in zip(data_set, addresses):
subject, message = process(data) # Irrelevant stuff
send_email(subject, message, addr)
当然,预期的行为是针对不同数据的不同地址,但我担心如果不了解为什么会发生这种情况
The intended behavior is of course different addresses for different data, but I'm concerned that not understanding why this happens might bite me in an unexpected way later on.
[1]我的发送邮件功能:
[1] My send mail function:
import smtplib
import socket
import getpass
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email (subject, message, to, reply_to='', cc='', html_message=''):
COMMASPACE = ", "
user, host = get_user_and_host_names()
sender = '%s@%s' % (user, host)
receivers = make_address_list(to)
copies = make_address_list(cc)
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = COMMASPACE.join(receivers)
if reply_to:
msg.add_header('Reply-to', reply_to)
if len(copies):
msg.add_header('CC', COMMASPACE.join(copies))
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
if message:
msg.attach( MIMEText(message, 'plain'))
if html_message:
msg.attach( MIMEText(html_message, 'html'))
smtpObj = smtplib.SMTP('localhost')
smtpObj.set_debuglevel(1)
smtpObj.sendmail(sender, receivers, msg.as_string())
smtpObj.quit()
print "\nSuccessfully sent email to:", COMMASPACE.join(receivers)
def get_user_and_host_names():
user = getpass.getuser()
host = socket.gethostname()
return user, host
def make_address_list (addresses):
if isinstance(addresses, str):
receivers = addresses.replace(' ','').split(',')
elif isinstance(addresses, list):
receivers = addresses
return receivers
推荐答案
我的工作解决方案以这种方式格式化列表(经过多个小时的实验):
My Working solution to format your list in this manner (after many hours of experiment):
[ firstemail@mail.com, secon dmail@mail.com, thirdmail@email.com]
我使用过:
to_address = list(str(self.to_address_list).split(","))
将我的QString转换为字符串,然后用,分割成列表
to convert my QString into string then into list with splitting with a ","
在我的情况下,COMMASPACE无法正常工作,因为在拆分时默认已添加空格。
In my case COMMASPACE was not working, because on splitting a space was already added by default.
这篇关于Python smtplib:只有其中的第一封电子邮件到达目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!