python实现邮件群发
2015-08-28 13:57
846 查看
#! /usr/bin/env python #coding=gbk import sys, smtplib, os from datetime import date from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase import email.iterators import email.generator from email import Encoders ############# #读取邮件列表 file_object = open('list.txt') try: all_the_text = file_object.readlines( ) finally: file_object.close( ) #读取配置文件 mailto_list = all_the_text file_object = open('set.txt') try: all_the_text = file_object.readlines( ) finally: file_object.close( ) set_list = all_the_text #########格式化配置文件############# def get_mail_fomat(index): return_str = set_list[index] return_str = return_str.replace("\n","") return_str = return_str.split('=')[1] return return_str ##########发送邮件############ def send_mail(to_list,sub,content): ''' to_list:发给谁 sub:主题 content:内容 send_mail("aaa@126.com","sub","content") ''' me=mail_user+"<"+mail_user+"@"+mail_postfix+">" msg = MIMEText(content) msg['Subject'] = sub msg['From'] = me #msg['To'] = ";".join(to_list) msg['To'] = to_list try: s = smtplib.SMTP() s.connect(mail_host) s.login(mail_user,mail_pass) s.sendmail(me, to_list, msg.as_string()) s.close() return True except Exception, e: print str(e) return False if __name__ == '__main__': send_succeed_num = 0 send_fail_num = 0 ##################### #设置服务器,用户名、口令,邮箱的后缀以及标题和内容 mail_host = get_mail_fomat(0) mail_user = get_mail_fomat(1) mail_pass = get_mail_fomat(2) mail_postfix = get_mail_fomat(3) mail_subject = get_mail_fomat(4) mail_content = get_mail_fomat(5) for i in mailto_list: if send_mail(i,mail_subject,mail_content): send_succeed_num +=1 print "%s-->发送成功"%i else: send_fail_num +=1 print "%s-->发送失败"%i print "\n-------------发送成功%d条" %send_succeed_num print "\n-------------发送失败%d条" %send_fail_num
配置文件
list.txt 用于配置要发送的邮件地址列表,每个邮件地址一行
test@xxx.com
test1@xxx.com
test2@xcxx.com
set.txt 用于配置邮件发送设置 请在正式应用时去掉#以及之后的内容。
mail_host=smtp.163.com smtp#服务器
mail_user=axi#用户名
mail_pass=xxx#密码
mail_postfix=163.com 邮件后缀,和mail_user结合起来就是axi@163.com
mail_subject=testsubject#邮件标题
mail_content=testcontent#邮件内容
python就是这样简单好用,一共84行代码而已.
相关文章推荐
- 每日python(4)
- python NumPy函数库基础
- 面向对象编程其实很简单——Python 面向对象(初级篇)
- python中set(可变集合)、frozenset(不可变集合)的基本操作
- python爬网页乱码
- pythonbrew, pythonz, virtualenv
- 深入解析Python中的lambda表达式的用法
- python实现Excel工作簿和表单合并
- Python开发入门与实战18-Windows Azure 虚拟机部署
- python 函数定义和内置函数isinstance以及数据类型检查type的使用
- Python装饰器与面向切面编程
- sh 获取 执行python的结果:$?
- Python算法:动态规划
- 学习日志---python继续
- 快速排序python实现
- Python中的join()函数和split()函数的用法
- 利用python数组解析ifconfig命令输出
- Python的字符串
- python Built-in Functions(python内置函数)
- 零基础学python-8.4 在原处修改列表方法汇总