通过电子邮件向多个收件人发送Python [英] Email multiple recipients Python

查看:57
本文介绍了通过电子邮件向多个收件人发送Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下面的pyton脚本通过电子邮件向多个收件人发送电子邮件.我已经在论坛上搜索了答案,但是无法正确实现其中的任何一个.如果有人有时间查看我的脚本并发现/解决问题,将不胜感激.

I'm trying to email multiple recipients using the pyton script below. I've searched the forum for answers, but have not been able to implement any of them correctly. If anyone has a moment to review my script and spot/resolve the problem it would be greatly appreciated.

这是我的脚本,我收集的问题在"sendmail"部分中,但无法解决该问题:

Here's my script, I gather my issue is in the 'sendmail' portion, but can't figure out how to fix it:

gmail_user = "sender@email.com"
gmail_pwd = "sender_password"
recipients = ['recipient1@email.com','recipient2@email.com']

def mail(to, subject, text, attach):
   msg = MIMEMultipart()
   msg['From'] = gmail_user
   msg['To'] = ", ".join(recipients)
   msg['Subject'] = subject

   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, to, msg.as_string())
   mailServer.close()

mail("recipient1@email.com, recipient2@email.com",
   "Subject",
   "Message",
   "attchachment")

任何见识将不胜感激.

最好

马特

推荐答案

应该更像

mail(["recipient1@email.com", "recipient2@email.com"],
   "Subject",
   "Message", 
   "attchachment")

您已经声明了一系列收件人,这些收件人过于笼统,您可以使用它而不必将其作为 mail 的参数传递.

You already have a array of recipients declared,that too globally,You can use that without passing it as an argument to mail.

这篇关于通过电子邮件向多个收件人发送Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆