我尝试从 python 发送邮件到多个电子邮件地址,从 .txt 文件导入,我尝试了不同的语法,但没有任何效果......
代码:
s.sendmail('sender@mail.com', ['recipient@mail.com', 'recipient2@mail.com', 'recipient3@mail.com'], msg.as_string())
所以我尝试从 .txt 文件中导入收件人地址:
urlFile = open("mailList.txt", "r+")
mailList = urlFile.read()
s.sendmail('sender@mail.com', mailList, msg.as_string())
mainList.txt 包含:
['recipient@mail.com', 'recipient2@mail.com', 'recipient3@mail.com']
但它不起作用......
我也试过:
... [mailList] ... in the code, and '...','...','...' in the .txt file, but also no effect
和
... [mailList] ... in the code, and ...','...','... in the .txt file, but also no effect...
有谁知道该怎么做吗?
非常感谢!
最佳答案
这个问题已经得到了某种程度的回答,但还没有完全回答。对我来说,问题是“收件人:” header 需要电子邮件作为字符串,而 sendmail 函数需要它在列表结构中。
# list of emails
emails = ["banjer@example.com", "slingblade@example.com", "dude@example.com"]
# Use a string for the To: header
msg['To'] = ', '.join( emails )
# Use a list for sendmail function
s.sendmail(from_email, emails, msg.as_string() )
关于python - 使用 Python smtplib 从 .txt 文件向多个收件人发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6941811/