python 发送邮件(3) 多人 带附件
2017-09-07 13:39
316 查看
# -*- coding:utf-8 -*- #发送电子邮件 import smtplib #定义邮件正文 from email.mime.text import MIMEText #定义邮件标题 from email.header import Header #定义附件 from email.mime.multipart import MIMEMultipart class send_email: def __init__(self, user, password, sender, receivers, subject, emailText, emailTextFormat, filename): self.user = user self.password = password self.sender = sender self.receivers = receivers self.subject = subject self.emailText = emailText self.emailTextFormat = emailTextFormat self.filename = filename def send(self): try: # 发送的附件 # 以二进制读模式打开文件 sendfile = open(self.filename, 'rb').read() att = MIMEText(sendfile, 'base64', 'utf-8') att['Content-Type'] ='application/octet-stream' att['Content-Disposition'] = 'attachment; filename =' + self.filename.split("/")[len(self.filename.split("/"))-1] # 编写HTML类型的邮件正文 msg = MIMEText(self.emailText, emailTextFormat, 'utf-8') msgRoot = MIMEMultipart('related') msgRoot.attach(att) msgRoot.attach(msg) # 用于邮箱显示标题、发件人、收件人 msgRoot['Subject'] = Header(self.subject, 'utf-8') msgRoot['From'] = self.sender receivers = ','.join(self.receivers) msgRoot['to'] = receivers # 连接并发送邮件 smtp = smtplib.SMTP() smtp.connect(smtpserver) smtp.login(self.user, self.password) smtp.sendmail(self.sender, self.receivers, msgRoot.as_string()) smtp.quit() except Exception as msgError: print msgError # 发送邮箱服务器 smtpserver = 'smtp.126.com' # 发送邮箱用户/密码 user = '' password = '' # 发送人邮箱 sender = '' # 收件人邮箱 receivers = ['', ''] # 邮件主题 subject = 'python_test_email' # 邮件正文 emailText = '<html><h1>不忘初心,不负本心!</h1></html>' # 邮件正文格式 emailTextFormat='html' # 附件:以二进制读模式打开文件 filename = 'D:/log.txt' send_email(user, password, sender, receivers, subject, emailText, emailTextFormat, filename).send()
设置为QQ邮箱,会报以下错误:
D:\ProgramData\Anaconda2\python.exe D:/ProgramData/guojunqi/Project1/email_all.py(530, 'Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28') Process finished with exit code 0
# -*- coding:utf-8 -*-#发送电子邮件import smtplib#定义邮件正文from email.mime.text import MIMEText#定义邮件标题from email.header import Header#定义附件from email.mime.multipart import MIMEMultipartclass send_email:def __init__(self, user, password, sender, receivers, subject, emailText, emailTextFormat, filename):self.user = userself.password = passwordself.sender = senderself.receivers = receiversself.subject = subjectself.emailText = emailTextself.emailTextFormat = emailTextFormatself.filename = filenamedef send(self):try:# 发送的附件# 以二进制读模式打开文件sendfile = open(self.filename, 'rb').read()att = MIMEText(sendfile, 'base64', 'utf-8')att['Content-Type'] ='application/octet-stream'att['Content-Disposition'] = 'attachment; filename =' + self.filename.split("/")[len(self.filename.split("/"))-1]# 编写HTML类型的邮件正文msg = MIMEText(self.emailText, emailTextFormat, 'utf-8')msgRoot = MIMEMultipart('related')msgRoot.attach(att)msgRoot.attach(msg)# 用于邮箱显示标题、发件人、收件人msgRoot['Subject'] = Header(self.subject, 'utf-8')msgRoot['From'] = self.senderreceivers = ','.join(self.receivers)msgRoot['to'] = receivers# 连接并发送邮件# smtp = smtplib.SMTP()# smtp.connect(smtpserver)smtp = smtplib.SMTP_SSL(smtpserver, 465)smtp.login(self.user, self.password)smtp.sendmail(self.sender, self.receivers, msgRoot.as_string())smtp.quit()print "发送成功!"except Exception as msgError:print msgError# 发送邮箱服务器# 目前支持qq、163、126、youedata等smtpserver = 'smtp.qq.com'# 发送邮箱用户/密码,qq、163密码使用授权码user = '*****@qq.com'password = 'cpruuzzgvtlhbfdg'# 发送人邮箱sender = ''# 收件人邮箱receivers = ['', '']# 邮件主题subject = 'python_test_email'# 邮件正文emailText = '<html><h1>不忘初心,不负本心!</h1></html>'# 邮件正文格式emailTextFormat='html'# 附件:以二进制读模式打开文件filename = 'D:/log.txt'send_email(user, password, sender, receivers, subject, emailText, emailTextFormat, filename).send()
相关文章推荐
- python发送带附件邮件
- python 发送邮件(2) 带附件
- 【python】发送邮件,含附件
- python发送带附件的邮件
- Python模块探秘 Smtplib发送带有各种附件的邮件
- python发送包含html、图片、附件和链接的邮件
- python 发送邮件 带附件
- Python发送带附件的SMTP邮件
- 用Python实现一个简单的能够发送带附件的邮件程序的教程
- Python2.7 smtplib发送带附件邮件报错STARTTLS解决方法 推荐
- Python3:定时发送邮件(带附件)
- python发送包含html、图片、附件和链接的邮件
- python编写发送带附件的邮件脚本
- python 3.5.4 正确 发送含附件邮件 示例
- selenium2+python3自动化2-csdn写博客(邮件发送附件测试报告)
- 用python发送带附件的邮件
- 飘逸的python - 发送带各种类型附件的邮件
- Python 发送带 附件 邮件 脚本
- Windows下python发送邮件_CustomEmail.py[text、html、附件、读取文本到正文]
- python 发送email邮件带附件