python发送邮件及附件

今天给大伙说说python发送邮件,官方的多余的话自己去百度好了,还有一大堆文档说实话不到万不得已的时候一般人都不会去看,回归主题:

本人是mac如果没有按照依赖模块的请按照下面的截图安装

导入模块如果没有错误,表示已经安装成功。

Python发送一个未知MIME类型的文件附件其基本思路如下:
1. 构造MIMEMultipart对象做为根容器
2. 构造MIMEText对象做为邮件显示内容并附加到根容器
3. 构造MIMEBase对象做为文件附件内容并附加到根容器
  a. 读入文件内容并格式化
  b. 设置附件头
4. 设置根容器属性
5. 得到格式化后的完整文本
6. 用smtp发送邮件

实例代码:

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 import smtplib
 5 from email.mime.multipart import MIMEMultipart
 6 from email.mime.text import MIMEText
 7 from email.mime.application import MIMEApplication
 8 
 9 class Mailer(object):
10     def __init__(self,maillist,mailtitle,mailcontent):
11         self.mail_list = maillist
12         self.mail_title = mailtitle
13         self.mail_content = mailcontent
14 
15         self.mail_host = "smtp.163.com"
16         self.mail_user = "your email name"
17         self.mail_pass = "your email password"
18         self.mail_postfix = "163.com"
19 
20     def sendMail(self):
21 
22         me = self.mail_user + "<" + self.mail_user + "@" + self.mail_postfix + ">"
23         msg = MIMEMultipart()
24         msg['Subject'] = 'Python mail Test'
25         msg['From'] = me
26         msg['To'] = ";".join(self.mail_list)
27 
28         #puretext = MIMEText('<h1>你好,<br/>'+self.mail_content+'</h1>','html','utf-8')
29         puretext = MIMEText('纯文本内容'+self.mail_content)
30         msg.attach(puretext)
31 
32         # jpg类型的附件
33         jpgpart = MIMEApplication(open('/home/mypan/1949777163775279642.jpg', 'rb').read())
34         jpgpart.add_header('Content-Disposition', 'attachment', filename='beauty.jpg')
35         msg.attach(jpgpart)
36 
37         # 首先是xlsx类型的附件
38         #xlsxpart = MIMEApplication(open('test.xlsx', 'rb').read())
39         #xlsxpart.add_header('Content-Disposition', 'attachment', filename='test.xlsx')
40         #msg.attach(xlsxpart)
41 
42         # mp3类型的附件
43         #mp3part = MIMEApplication(open('kenny.mp3', 'rb').read())
44         #mp3part.add_header('Content-Disposition', 'attachment', filename='benny.mp3')
45         #msg.attach(mp3part)
46 
47         # pdf类型附件
48         #part = MIMEApplication(open('foo.pdf', 'rb').read())
49         #part.add_header('Content-Disposition', 'attachment', filename="foo.pdf")
50         #msg.attach(part)
51 
52         try:
53             s = smtplib.SMTP() #创建邮件服务器对象
54             s.connect(self.mail_host) #连接到指定的smtp服务器。参数分别表示smpt主机和端口
55             s.login(self.mail_user, self.mail_pass) #登录到你邮箱
56             s.sendmail(me, self.mail_list, msg.as_string()) #发送内容
57             s.close()
58             return True
59         except Exception, e:
60             print str(e)
61             return False
62 
63 
64 if __name__ == '__main__':
65     #send list
66     mailto_list = ["aaa@lsh123.com","bbb@163.com"]
67     mail_title = 'Hey subject'
68     mail_content = 'Hey this is content'
69     mm = Mailer(mailto_list,mail_title,mail_content)
70     res = mm.sendMail()
71     print res

 

posted @ 2016-12-30 18:28  北京流浪儿  阅读(1606)  评论(0编辑  收藏  举报