python 发送邮件 -- 解析配置文件
2014-02-15 15:24
615 查看
昨天做了一个用python发送邮件的模块。因为是用在项目中,所以写得比较模块化一点,增加了账户的配置文件,所以其中也用到了Python 配置文件解析模块。把他们集中在一起,以供新手参考,对有同样需求的新手,望有所帮助。实现过程中参考了 这两篇文章,特此感谢
python发送邮件参考1 和这个
python 配置文件解析。
发送邮件模块
import ConfigParser
import smtplib
from email.mime.text import MIMEText
conf = ConfigParser.ConfigParser()
conf.read('test.cfg')
#sections = conf.sections()
#options = conf.options('section_a')
mail_to = conf.get('section_a','mail_to')
mail_host = conf.get('section_a','mail_host')
mail_user = conf.get('section_a','mail_user')
mail_passwd = conf.get('section_a','mail_passwd')
mail_postfix = conf.get('section_a','mail_postfix')
def send_mail(mail_to, subject, content):
me = mail_user + '@' + mail_postfix
msg = MIMEText(content,_subtype='plain',_charset='utf-8')
msg['Subject'] = subject
msg['From'] = me
msg['To'] = mail_to
try:
sv = smtplib.SMTP()
sv.connect(mail_host)
sv.login(mail_user, mail_passwd)
sv.sendmail(me,mail_to, msg.as_string())
sv.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == '__main__':
subject = 'Python test'
content = 'Nothing in this world is free!'
if send_mail(mail_to,subject,content):
print 'sent success'
else:
print 'sent failed'
配置文件 test.cfg 。当然,上面的例子中 这个文件是和 .py 文件是在同一个目录下的,如果不在,配置路径就可以。
整个模块就是这样了,包含了 python 发送邮件 和 python 配置文件解析的 主要用法,如果深入学习,可以就里面出现的具体的部分到 python 的官方文档中阅读。
python发送邮件参考1 和这个
python 配置文件解析。
发送邮件模块
import ConfigParser
import smtplib
from email.mime.text import MIMEText
conf = ConfigParser.ConfigParser()
conf.read('test.cfg')
#sections = conf.sections()
#options = conf.options('section_a')
mail_to = conf.get('section_a','mail_to')
mail_host = conf.get('section_a','mail_host')
mail_user = conf.get('section_a','mail_user')
mail_passwd = conf.get('section_a','mail_passwd')
mail_postfix = conf.get('section_a','mail_postfix')
def send_mail(mail_to, subject, content):
me = mail_user + '@' + mail_postfix
msg = MIMEText(content,_subtype='plain',_charset='utf-8')
msg['Subject'] = subject
msg['From'] = me
msg['To'] = mail_to
try:
sv = smtplib.SMTP()
sv.connect(mail_host)
sv.login(mail_user, mail_passwd)
sv.sendmail(me,mail_to, msg.as_string())
sv.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == '__main__':
subject = 'Python test'
content = 'Nothing in this world is free!'
if send_mail(mail_to,subject,content):
print 'sent success'
else:
print 'sent failed'
配置文件 test.cfg 。当然,上面的例子中 这个文件是和 .py 文件是在同一个目录下的,如果不在,配置路径就可以。
[section_a] mail_host = smtp.163.com mail_user = yourusername mail_postfix = 163.com mail_passwd = yourpasswd mail_to = mail_to_account@126.com
整个模块就是这样了,包含了 python 发送邮件 和 python 配置文件解析的 主要用法,如果深入学习,可以就里面出现的具体的部分到 python 的官方文档中阅读。
相关文章推荐
- Python解析配置文件模块:ConfigPhaser
- python使用chilkat解析出邮件文本中的发送to和抄送cc
- python3 发邮件,从文件读邮件内容和发送html内容
- [Python]ConfigParser解析配置文件
- asp.net 自动发送激活邮件,不需修改web.config配置文件
- python解析xml配置文件
- PasswordRecovery控件发送邮件配置文件web.config设置
- python小实例--监控Nginx配置文件的不同并且邮件报警
- PostFix的安装配置,python发送邮件示例
- Python 解析配置文件 (ConfigParser)模块
- 【Python】配置文件解析模块ConfigParser使用实例
- 【JavaMail开发总结】配置文件形式--发送邮件程序
- linux shell 发送email 邮件【文章内容仅供参考,有些地方可能不正确,默认只能在局域网,公网需要修改配置文件】
- Python 模块之 ConfigParser: 用 Python 解析配置文件
- LOG4J的配置..实现了输出到控制台、文件、回滚文件、发送日志邮件
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送,附有说明文件
- python ConfigParser – 配置文件解析(转载)
- Python 02 解析配置文件和日志库的使用
- python学习笔记3-解析配置文件ConfigParser模块
- python 解析配置文件