码迷,mamicode.com
首页 > 编程语言 > 详细

python实现发送邮件

时间:2015-09-08 00:01:59      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

SMTP是发送邮件的协议。python对SMTP的支持有两个模块,smtplib模块和email模块。email负责构造邮件,smtplib负责发送邮件。

一个例子简单的实现发送邮件的功能

#!/usr/bin/env python
# coding=utf-8
import smtplib
from email.mime.text import MIMEText

mail_to_list=["openstack@126.com"]
mail_host="smtp.126.com"
mail_user="linux"
mail_pass="password"
mail_postfix="126.com"

def send_mail(to_list,sub,content):
    me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
    msg=MIMEText(content)
    msg[‘Subject‘]=sub
    msg[‘From‘]=me
    msg[‘To‘]=";".join(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__‘:
    if send_mail(mail_to_list,"this is sub","This is content"):
        print "ok"
    else:
        print "fail"

 

python实现发送邮件

标签:

原文地址:http://www.cnblogs.com/yeming/p/4790174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!