功能:发邮件、多人发送、发附件
import smtplib,os,sys
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email import encoders
from email.mime.base import MIMEBase
from email.utils import parseaddr, formataddr
def formatAddr(s):name, addr = parseaddr(s)return formataddr((Header(name, 'utf-8').encode(), addr))
def sendMail(body, attachment):smtp_server = 'smtp.exmail.qq.com'from_mail = 'xxx@xxx'mail_pass = '********'to_mail = ['邮箱1','邮箱2','邮箱3']msg = MIMEMultipart()msg['From'] = formatAddr('来自哪里 <%s>' % from_mail).encode()msg['To'] = ','.join(to_mail)msg['Subject'] = Header('抬头', 'utf-8').encode()msg.attach(MIMEText(body, 'plain', 'utf-8'))with open(attachment, 'rb') as f:mime = MIMEBase('text', 'txt', filename=attachment)mime.add_header('Content-Disposition', 'attachment', filename=attachment)mime.set_payload(f.read())encoders.encode_base64(mime)msg.attach(mime)server = smtplib.SMTP(smtp_server, 25)server.set_debuglevel(1)server.login(from_mail, mail_pass)print(2)print(type(msg))server.sendmail(from_mail, to_mail, msg.as_string()) server.quit()if __name__ == "__main__":filer=sys.argv[1]sendMail('附件是测试数据, 请查收!', filer)