使用Python smtplib从.txt文件向多个收件人发送电子邮件

Fra*_*els 5 python email

我尝试从python发送邮件到多个电子邮件地址,从.txt文件导入,我尝试了不同的语法,但没有什么可行的...

代码:

s.sendmail('sender@mail.com', ['recipient@mail.com', 'recipient2@mail.com', 'recipient3@mail.com'], msg.as_string())
Run Code Online (Sandbox Code Playgroud)

所以我尝试从.txt文件导入收件人地址:

urlFile = open("mailList.txt", "r+")
mailList = urlFile.read()
s.sendmail('sender@mail.com', mailList, msg.as_string())
Run Code Online (Sandbox Code Playgroud)

mainList.txt包含:

['recipient@mail.com', 'recipient2@mail.com', 'recipient3@mail.com']
Run Code Online (Sandbox Code Playgroud)

但它不起作用......

我也尝试过这样做:

... [mailList] ... in the code, and '...','...','...' in the .txt file, but also no effect
Run Code Online (Sandbox Code Playgroud)

... [mailList] ... in the code, and ...','...','... in the .txt file, but also no effect...
Run Code Online (Sandbox Code Playgroud)

有谁知道该怎么办?

非常感谢!

Ban*_*jer 34

这个问题已经得到了回答,但还没有完全解决.对我来说,问题是"To:"标题需要将电子邮件作为字符串,而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() )
Run Code Online (Sandbox Code Playgroud)


pyr*_*ope 3

urlFile = open("mailList.txt", "r+")
mailList = [i.strip() for i in urlFile.readlines()]
Run Code Online (Sandbox Code Playgroud)

并将每个收件人放在自己的上(即用换行符分隔)。