python实现发送邮件及附件功能

yipeiwu_com6年前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['Subject'] = 'Python mail Test'
     msg['From'] = me
     msg['To'] = ";".join(self.mail_list)
 
     #puretext = MIMEText('<h1>你好,<br/>'+self.mail_content+'</h1>','html','utf-8')
     puretext = MIMEText('纯文本内容'+self.mail_content)
     msg.attach(puretext)
 
     # jpg类型的附件
     jpgpart = MIMEApplication(open('/home/mypan/1949777163775279642.jpg', 'rb').read())
     jpgpart.add_header('Content-Disposition', 'attachment', filename='beauty.jpg')
     msg.attach(jpgpart)
 
     # 首先是xlsx类型的附件
     #xlsxpart = MIMEApplication(open('test.xlsx', 'rb').read())
     #xlsxpart.add_header('Content-Disposition', 'attachment', filename='test.xlsx')
     #msg.attach(xlsxpart)
 
     # mp3类型的附件
     #mp3part = MIMEApplication(open('kenny.mp3', 'rb').read())
     #mp3part.add_header('Content-Disposition', 'attachment', filename='benny.mp3')
     #msg.attach(mp3part)
 
     # pdf类型附件
     #part = MIMEApplication(open('foo.pdf', 'rb').read())
     #part.add_header('Content-Disposition', 'attachment', 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__ == '__main__':
   #send list
   mailto_list = ["aaa@lsh123.com","bbb@163.com"]
   mail_title = 'Hey subject'
   mail_content = 'Hey this is content'
   mm = Mailer(mailto_list,mail_title,mail_content)
   res = mm.sendMail()
   print res

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python中kmeans聚类实现代码

k-means算法思想较简单,说的通俗易懂点就是物以类聚,花了一点时间在python中实现k-means算法,k-means算法有本身的缺点,比如说k初始位置的选择,针对这个有不少人提出...

Python遍历zip文件输出名称时出现乱码问题的解决方法

本文实例讲述了Python遍历zip文件输出名称时出现乱码问题的解决方法。分享给大家供大家参考。具体如下: windows中使用python2.7遍历zip文件之后输出文件名等信息,co...

新手该如何学python怎么学好python?

根据本人的学习经验,我总结了以下十点和大家分享: 1)学好python的第一步,就是马上到www.python.org网站上下载一个python版本。我建议初学者,不要下载具有IDE功能...

Windows下安装Scrapy

Windows下安装Scrapy

这几天正好有需求实现一个爬虫程序,想到爬虫程序立马就想到了python,python相关的爬虫资料好像也特别多。于是就决定用python来实现爬虫程序了,正好发现了python有一个开源...

Python控制Firefox方法总结

Python控制Firefox方法总结

有时候为了自动化测试网页,我们往往希望能够使用一些脚本语言控制浏览器. 通过脚本模拟一些浏览器动作,然后测试得到的结果.这里, 我们讲解一下如何使用Python语言控制Firefox浏览...