python 发邮件 带附件 给多人发邮件

源码如下:

31 import os
 32 import sys
 33 import smtplib
 34 from email.mime.text import MIMEText
 35 from email.mime.multipart import MIMEMultipart
 36 from email.header import Header
 37 from email.mime.application import MIMEApplication
 38
 39 sender = 'lava@xxxx.com.cn'
 40 subject = sys.argv[1]
 41 content = sys.argv[2]
 42 #receivers = sys.argv[3:]
 43 att = sys.argv[3]
 44 receivers = sys.argv[4] #字符串
 45
 46 #print(subject)
 47 #print(content)
 48 #print(receivers)
 49
 50 message = MIMEMultipart() #有附件就有多个part
 51 message['From']=Header('lava','utf-8')
 52 message['To']=receivers
 53 message['Subject']=Header(subject,'utf-8')
 54
 55 # email content
 56 content_obj = MIMEText(content,'plain','utf-8')
 57 message.attach(content_obj)
 58
 59 # email attachment -- detailed build info
 60 #att_obj = MIMEApplication(att,'plain','utf-8')
 61 att_obj = MIMEText(att,'plain','utf-8')
 62 att_obj.add_header('Content-Disposition', 'attachment', filename ='build_info.txt')#附件名字
 63 message.attach(att_obj)
 64
 65 smtpObj = smtplib.SMTP()
 66 smtpObj.connect('smtp.xxxx.com.cn',25)
 67 smtpObj.login('lava@xxxx.com.cn','123456')
 68
 69 #for receiver in receivers:
 70     #print(receiver)
 71     #smtpObj.sendmail(sender,receiver,message.as_string())
 72 #smtpObj.sendmail(sender,'zhangyi@xxxx.com.cn',message.as_string())
 73 smtpObj.sendmail(sender,receivers.split(','),message.as_string()) #receivers.split(',') python字符串转化成列表
74 smtpObj.quit()
原文地址:https://www.cnblogs.com/idyllcheung/p/14075879.html