Python 批量发送邮件脚本
2014-02-21 16:07
656 查看
查看源码
1 #!/usr/bin/env python 2 #-*- coding: utf-8 -*- 3 4 import email 5 import smtplib 6 import mimetypes 7 from email.MIMEMultipart import MIMEMultipart 8 from email.MIMEText import MIMEText 9 10 # 邮件列表文件(每行一个邮件地址) 11 MAIL_FILE_PATH = './emails.txt' 12 13 # 邮件内容文件 14 MAIL_CONTENT_PATH = './page_kfc.html' 15 16 # 发件人名称 17 SENDER_NAME = 'Company Inc.' 18 19 # 发件人邮箱 20 SENDER_MAIL = 'noreply@yourmailhost.com' 21 22 # 发件人邮箱密码 23 SENDER_PSWD = 'yourpassword' 24 25 # SMTP 服务器 26 SMTP_SERVER = 'smtp.yourmailhost.com' 27 28 # SMTP 端口 29 SMTP_PORT = '25' 30 31 # 每次发送给几人 32 RECEIVER_LIMIT_PER_TIME = 10 33 34 # ################################################################## 35 # # 36 # 以下部分请勿修改 # 37 # # 38 # ################################################################## 39 40 # 获取收件人列表 41 def GetReceivers(limit = 10): 42 f = open(MAIL_FILE_PATH, 'r+') 43 44 try: 45 lines = f.readlines() 46 finally: 47 f.close() 48 49 receivers = lines[:RECEIVER_LIMIT_PER_TIME] 50 lines = lines[RECEIVER_LIMIT_PER_TIME:] 51 52 f = open(MAIL_FILE_PATH, 'w+') 53 f.writelines(lines) 54 f.close() 55 56 return receivers 57 58 # 批量发送邮件 59 def SendEmail(sender, senderName, receivers, subject, body): 60 smtp = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) 61 smtp.login(SENDER_MAIL, SENDER_PSWD) 62 63 if(senderName != ''): 64 sender = senderName + '<' + sender + '>' 65 66 for receiver in receivers: 67 receiver = receiver.strip() 68 69 msg = MIMEMultipart('alternative') 70 msg['Subject'] = subject 71 msg['From'] = sender 72 msg['To'] = receiver 73 msg.attach(MIMEText(body, 'html', 'utf-8')) 74 75 smtp.sendmail(sender, receiver, msg.as_string()) 76 77 smtp.quit() 78 79 if __name__ == '__main__': 80 ''' 81 发送邮件开始 82 ''' 83 84 # 获取本次要发送的邮件地址 85 receivers = GetReceivers(RECEIVER_LIMIT_PER_TIME) 86 87 # 获取邮件标题和内容 88 f = open(MAIL_CONTENT_PATH, 'r'); 89 lines = f.readlines() 90 f.close() 91 92 subject = lines[0].strip() 93 body = ''.join(lines[1:]) 94 95 # 发送 96 SendEmail(SENDER_MAIL, SENDER_NAME, receivers, subject, body)
相关文章推荐
- PHP 命令行模式实战之cli+mysql 模拟队列批量发送邮件(在Linux环境下PHP 异步执行脚本发送事件通知消息实际案例)
- 批量添加JS脚本,像126发送邮件时的上传附件,可以自由增,减
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送,附有说明文件
- postfix python发送邮件脚本配置(不乱码)
- python脚本实现发送邮件功能
- python脚本获取zabbix监控数据,并且通过邮件发送
- python脚本发送普通邮件
- Python 发送邮件脚本
- python邮件发送脚本
- python脚本发送报警邮件
- Python实现邮件的批量发送
- Python 发送带 附件 邮件 脚本
- Python实现邮件的批量发送
- centos 7 keepalived故障邮件通知实战(附Python邮件发送脚本)
- 通过ANT实现jmeter批量执行脚本、生成报告、发送邮件全套build.xml文件
- python发送邮件的脚本
- python写的简单发送邮件的脚本
- Zabbix 自定义邮件发送脚本(Python) 推荐
- 监控redis和zookpeer服务脚本 并且python发送邮件
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送