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

Python 发送邮件

时间:2015-06-21 11:46:30      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

几种邮件的具体实施代码

1、普通文本邮件

 普通文本邮件发送的实现,关键是要将MIMEText中_subtype设置为plain。首先导入smtplib和mimetext。创建smtplib.smtp实例,connect邮件smtp服务器,login后发送。

 1 #coding: utf-8
 2 ‘‘‘
 3 发送txt文本邮件
 4 ‘‘‘
 5 import smtplib
 6 from email.mime.text import MIMEText
 7 
 8 mailto_list = [‘xxxxxx@qq.com]
 9 mail_host = "smtp.126.com"
10 mail_user = "xxxx"
11 mail_pass = "xxxx"
12 mail_postfix = "126.com"
13 
14 def send_mail(to_list, sub, content):
15     me = "hello"+"<"+mail_user+"@"+mail_postfix+">"
16     msg = MIMEText(content, _subtype=plain, _charset=gb2312)
17     msg[Subject] = sub
18     msg[From] = me
19     msg[To] = ";".join(to_list)
20     try:
21         server = smtplib.SMTP()
22         server.connect(mail_host)
23         server.login(mail_user,mail_pass)
24         server.sendmail(me, to_list, msg.as_string())
25         server.close()
26         return True
27     except Exception, e:
28         print str(e)
29         return False
30 
31 if __name__==__main__:
32     if send_mail(mailto_list, "hello", "hello world!"):
33         print "发送成功"
34     else:
35         print "发送失败"

2、html邮件的发送

 与text邮件不同之处就是将将MIMEText中_subtype设置为html

 1 #coding: utf-8
 2 ‘‘‘
 3 发送html文本邮件
 4 ‘‘‘
 5 import smtplib
 6 from email.mime.text import MIMEText
 7 
 8 mailto_list = [‘xxxxxx@qq.com]
 9 mail_host = "smtp.126.com"         #设置服务器
10 mail_user = "xxxx"     #设置用户名
11 mail_pass = "xxxx"      #口令
12 mail_postfix = "126.com"           #发件邮箱后缀
13 
14 def send_mail(to_list, sub, content):  #to_list: 收件人; sub: 主题; content: 邮件内容
15     me = "hello"+"<"+mail_user+"@"+mail_postfix+">" #此处的hello可以任意设置,收到邮件后,按照设置显示
16     msg = MIMEText(content, _subtype=html, _charset=gb2312)  #创建一个实例,这里设置为html格式邮件
17     msg[Subject] = sub  #设置主题
18     msg[From] = me
19     msg[To] = ";".join(to_list)
20     try:
21         server = smtplib.SMTP()
22         server.connect(mail_host)   #连接smtp服务器
23         server.login(mail_user,mail_pass)  #登陆服务器
24         server.sendmail(me, to_list, msg.as_string()) #发送邮件
25         server.close()
26         return True
27     except Exception, e:
28         print str(e)
29         return False
30 
31 if __name__==__main__:
32     if send_mail(mailto_list, "hello", "<a href=‘http://www.baidu.com‘> baidu </a>"):
33         print "发送成功"
34     else:
35         print "发送失败"

3、带附件的邮件

发送带附件的邮件,首先要创建MIMEMultipart()实例,然后构造附件,如果有多个附件,可依次构造,最后利用smtplib.smtp发送

# -*- coding: cp936 -*-
‘‘‘
发送带附件邮件
‘‘‘

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib

#创建一个带附件的实例
msg = MIMEMultipart()

#构造附件1
att1 = MIMEText(open(d:\\123.rar, rb).read(), base64, gb2312)
att1["Content-Type"] = application/octet-stream
att1["Content-Disposition"] = attachment; filename="123.doc"#这里的filename可以任意写,写什么名字,邮件中显示什么名字
msg.attach(att1)

#构造附件2
att2 = MIMEText(open(d:\\123.txt, rb).read(), base64, gb2312)
att2["Content-Type"] = application/octet-stream
att2["Content-Disposition"] = attachment; filename="123.txt"
msg.attach(att2)

#加邮件头
msg[to] = YYY@YYY.com
msg[from] = XXX@XXX.com
msg[subject] = hello world
#发送邮件
try:
    server = smtplib.SMTP()
    server.connect(smtp.XXX.com)
    server.login(XXX,XXXXX)#XXX为用户名,XXXXX为密码
    server.sendmail(msg[from], msg[to],msg.as_string())
    server.quit()
    print 发送成功
except Exception, e:  
    print str(e)

4、利用MIMEimage发送图片

 1 # -*- coding: cp936 -*-
 2 import smtplib
 3 import mimetypes
 4 from email.mime.text import MIMEText
 5 from email.mime.multipart import MIMEMultipart
 6 from email.mime.image import MIMEImage
 7 
 8 def AutoSendMail():
 9     msg = MIMEMultipart()
10     msg[From] = "XXX@XXX.com"
11     msg[To] = "YYY@YYY.com"
12     msg[Subject] = "hello world"
13 
14 
15     txt = MIMEText("这是中文的邮件内容哦",plain,gb2312)     
16     msg.attach(txt)
17     
18 
19     file1 = "C:\\hello.jpg"
20     image = MIMEImage(open(file1,rb).read())
21     image.add_header(Content-ID,<image1>)
22     msg.attach(image)
23     
24     
25     server = smtplib.SMTP()
26     server.connect(smtp.XXX.com)
27     server.login(XXX,XXXXXX)
28     server.sendmail(msg[From],msg[To],msg.as_string())
29     server.quit()
30     
31 if __name__ == "__main__":
32     AutoSendMail()

html中添加<Img src=‘cid:hello.jpg‘>,图片不会出现附件中。

Python 发送邮件

标签:

原文地址:http://www.cnblogs.com/nju2014/p/4591711.html

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