Python 发送email的方法
来源:互联网 发布:whatsapp for mac 编辑:程序博客网 时间:2024/05/19 17:56
import smtplibfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEText import MIMETextfrom email.MIMEBase import MIMEBasefrom email import Encoders##------------------------------------------------------------------------------def sendEmail(subject, body, host, sender, recipients):## recipients = getRecipientList(recipients) header = "Subject: %s\r\nFrom: %s\r\nTo: %s\r\n\r\n" % ( subject, sender, ", ".join(recipients)) smtp = smtplib.SMTP(host) smtp.sendmail(sender, recipients, header + body) smtp.quit()##------------------------------------------------------------------------------ def sendEmailWithAttachments(subject, body, host, sender, recipients, attachments): msg = MIMEMultipart() msg['Subject'] = subject msg['From'] = sender msg['To'] = ", ".join(recipients) msg.preamble = subject msg.epilogue = '' # guarantees the message ends in a newline if body: msg.attach(MIMEText(body)) # Handle the attachments: for filename in attachments: msg.attach(getMIMEMessage(filename)) smtp = smtplib.SMTP(host) smtp.sendmail(sender, recipients, msg.as_string()) smtp.quit()##------------------------------------------------------------------------------ def sendEmailWithHtmlContent(subject, body, host, sender, recipients): msg = MIMEMultipart('alternative') msg['Subject'] = subject msg['From'] = sender msg['To'] = ", ".join(recipients) msg.preamble = subject msg.epilogue = '' # guarantees the message ends in a newline if body: msg.attach(MIMEText(body,'html')) smtp = smtplib.SMTP(host) smtp.sendmail(sender, recipients, msg.as_string()) smtp.quit() ##------------------------------------------------------------------------------def getMIMEMessage(filename): fp = open(filename, 'rb') msg = MIMEBase('application', 'octet-stream') msg.set_payload(fp.read()) fp.close() Encoders.encode_base64(msg) # Set the filename parameter: msg.add_header('Content-Disposition', 'attachment', filename=filename) return msg##------------------------------------------------------------------------------def getRecipientList(filename): lines = file(filename).readlines() return [l.strip() for l in lines if l.strip() and not l.startswith('#')] ##------------------------------------------------------------------------------if __name__ == '__main__':# sendEmail(subject='Testing',# body='This is just a test',# host='internalmail.qq.com',# sender='EmailTester@qq.com',# recipients=['mike.cheng@qq.com'])# sendEmailWithAttachments(# subject='Testing',# body='This is just a test',# host='internalmail.qq.com',# sender='EmailTester@qq.com',# recipients=['feihong.hsu@qq.com'],# attachments=['mail.py'])# # sendEmailWithHtmlContent # mailcontent= open('reboot_success.html','r').read()# mailcontent = mailcontent.replace('{processName}', '')# mailcontent = mailcontent.replace('{computerName}', '')# mailcontent = mailcontent.replace('{serviceName}', '')# sendEmailWithHtmlContent(subject='[Warn] has been restarted successfully on ',# body=mailcontent,# host='internalmail.qq.com',# sender='DataOperation.Monitor@qq.com',# recipients=['SZDATAPRODUCTION@qq.com']) print '\nDone!\n'
阅读全文
0 0
- Python 发送email的方法
- python 简单的发送email方法
- C#发送Email的方法
- IOS发送Email的方法
- IOS发送Email的方法
- Matlab发送Email的方法
- IOS发送Email的方法
- [Python] 发送email的几种方式
- Python发送Email
- Python 发送Email
- python 发送Email程序
- python发送email
- python实现发送email
- Python 发送Email
- python 简单email发送
- Delphi下发送Email的方法
- Delphi下发送Email的方法
- 发送Email的方法(jmail方式实现)
- List.Set,Map
- Mysql 热备份-读写分离
- eslint代码规范,规则配置
- CentOS7上安装RabbitMQ与win互通测试
- pymssql 安装和使用
- Python 发送email的方法
- 【bzoj4429】[Nwerc2015] Elementary Math小学数学
- Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) A 题目忘了
- CART树回归
- STM32平台移植uCOSIII
- Selenium IDE 使用方法小结
- POJ 1751 Highways (kruskal)
- Selenium-如何截取图像
- threejs学习随记(一)