首页 >后端开发 >Python教程 >python中如何实现发送邮件及附件功能的具体详解

python中如何实现发送邮件及附件功能的具体详解

黄舟
黄舟原创
2017-05-21 14:10:221992浏览

这篇文章主要为大家详细介绍了python实现发送邮件及附件功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

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

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

实例代码:

 #!/usr/bin/env python
 # -*- coding:utf-8 -*-
 
 import smtplib
 from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
 from email.mime.application import MIMEApplication
 
 class Mailer(object):
   def init(self,maillist,mailtitle,mailcontent):
     self.mail_list = maillist
     self.mail_title = mailtitle
     self.mail_content = mailcontent
 
     self.mail_host = "smtp.163.com"
     self.mail_user = "your email name"
     self.mail_pass = "your email password"
     self.mail_postfix = "163.com"
 
   def sendMail(self):
 
     me = self.mail_user + "<" + self.mail_user + "@" + self.mail_postfix + ">"
     msg = MIMEMultipart()
     msg[&#39;Subject&#39;] = &#39;Python mail Test&#39;
     msg[&#39;From&#39;] = me
     msg[&#39;To&#39;] = ";".join(self.mail_list)
 
     #puretext = MIMEText(&#39;<h1>你好,<br/>&#39;+self.mail_content+&#39;</h1>&#39;,&#39;html&#39;,&#39;utf-8&#39;)
     puretext = MIMEText(&#39;纯文本内容&#39;+self.mail_content)
     msg.attach(puretext)
 
     # jpg类型的附件
     jpgpart = MIMEApplication(open(&#39;/home/mypan/1949777163775279642.jpg&#39;, &#39;rb&#39;).read())
     jpgpart.add_header(&#39;Content-Disposition&#39;, &#39;attachment&#39;, filename=&#39;beauty.jpg&#39;)
     msg.attach(jpgpart)
 
     # 首先是xlsx类型的附件
     #xlsxpart = MIMEApplication(open(&#39;test.xlsx&#39;, &#39;rb&#39;).read())
     #xlsxpart.add_header(&#39;Content-Disposition&#39;, &#39;attachment&#39;, filename=&#39;test.xlsx&#39;)
     #msg.attach(xlsxpart)
 
     # mp3类型的附件
     #mp3part = MIMEApplication(open(&#39;kenny.mp3&#39;, &#39;rb&#39;).read())
     #mp3part.add_header(&#39;Content-Disposition&#39;, &#39;attachment&#39;, filename=&#39;benny.mp3&#39;)
     #msg.attach(mp3part)
 
     # pdf类型附件
     #part = MIMEApplication(open(&#39;foo.pdf&#39;, &#39;rb&#39;).read())
     #part.add_header(&#39;Content-Disposition&#39;, &#39;attachment&#39;, filename="foo.pdf")
     #msg.attach(part)
 
     try:
       s = smtplib.SMTP() #创建邮件服务器对象
       s.connect(self.mail_host) #连接到指定的smtp服务器。参数分别表示smpt主机和端口
       s.login(self.mail_user, self.mail_pass) #登录到你邮箱
       s.sendmail(me, self.mail_list, msg.as_string()) #发送内容
       s.close()
       return True
     except Exception, e:
       print str(e)
       return False
 
 
 if name == &#39;main&#39;:
   #send list
   mailto_list = ["aaa@lsh123.com","bbb@163.com"]
   mail_title = &#39;Hey subject&#39;
   mail_content = &#39;Hey this is content&#39;
   mm = Mailer(mailto_list,mail_title,mail_content)
   res = mm.sendMail()
   print res

以上是python中如何实现发送邮件及附件功能的具体详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn