经过多次搜索后,我无法找到如何使用smtplib.sendmail发送给多个收件人.问题是每次发送邮件时邮件标题似乎包含多个地址,但事实上只有第一个收件人才会收到电子邮件.
问题似乎是email.Message模块期望与smtplib.sendmail()函数不同的东西.
简而言之,要发送给多个收件人,您应将标头设置为逗号分隔的电子邮件地址字符串.但该sendmail()参数to_addrs应该是电子邮件地址列表.
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib
msg = MIMEMultipart()
msg["Subject"] = "Example"
msg["From"] = "me@example.com"
msg["To"] = "malcom@example.com,reynolds@example.com,firefly@example.com"
msg["Cc"] = "serenity@example.com,inara@example.com"
body = MIMEText("example email body")
msg.attach(body)
smtp = smtplib.SMTP("mailhost.example.com", 25)
smtp.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())
smtp.quit()
Run Code Online (Sandbox Code Playgroud) 我尝试从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)
有谁知道该怎么办?
非常感谢!
我在想.有没有办法在Python的默认SMTPlib上添加多个接收器?
喜欢(主题和内容已经设置,smtp服务器gmail.):
python sendmail.py receiver1@gmail.com receiver2@gmail.com receiver3@gmail.com ...
Run Code Online (Sandbox Code Playgroud)
谢谢