我正在尝试使用下面的 pyton 脚本向多个收件人发送电子邮件。我在论坛中搜索了答案,但未能正确实施其中任何一个。如果有人有时间查看我的脚本并发现/解决问题,将不胜感激。
这是我的脚本,我认为我的问题出在“sendmail”部分,但不知道如何解决:
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")
任何见解将不胜感激。
最好的,
马特