python脚本发邮件,一般会用到smtplib和email这两个模块。看看该模块怎么使用,先看smtplib模块。

smtplib模块定义了一个简单的SMTP客户端,可以用来在互联网上发送邮件。

定义的类有如下:

class smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])

class smtplib.SMTP_SSL([host[, port[, local_hostname[, keyfile[, certfile[, timeout]]]]]])

class smtplib.LMTP([host[, port[, local_hostname]]])

还有一些已经定义好的异常

exception smtplib.SMTPException

exception smtplib.SMTPServerDisconnected

exception smtplib.SMTPResponseException

exception smtplib.SMTPSenderRefused

exception smtplib.SMTPRecipientsRefused

exception smtplib.SMTPDataError

exception smtplib.SMTPConnectError

exception smtplib.SMTPHeloError

exception smtplib.SMTPAuthenticationError

这么多已定义的类中,我们最常用的的还是smtplib.SMTP类,就具体看看该类的用法:

smtp实例封装一个smtp连接,它支持所有的SMTP和ESMTP操作指令,如果host和port参数被定义,则smtp会在初始化期间自动调用connect()方法,如果connect()方法失败,则会触发SMTPConnectError异常,timeout参数设置了超时时间。在一般的调用过程中,应该遵connetc()、sendmail()、quit()步骤。

下面我们来看看该类的方法:

SMTP.set_debuglevel(level)

设置输出debug调试信息,默认不输出调试信息。

SMTP.docmd(cmd[, argstring])

发送一个command到smtp服务器,

SMTP.connect([host[, port]])

连接到指定的smtp服务器,默认是本机的25端口。也可以写成hostname:port的形式。

SMTP.helo([hostname])

使用helo指令向smtp服务器确认你的身份。

SMTP.ehlo([hostname])

使用ehlo指令向esmtp服务器确认你的身份。

SMTP.ehlo_or_helo_if_needed()

如果在以前的会话连接中没有提供ehlo或者helo指令,这个方法调用ehlo()或者helo()。

SMTP.has_extn(name)

判断指定的名称是否在smtp服务器上。

SMTP.verify(address)

判断邮件地址是否在smtp服务器上存在。

SMTP.login(user, password)

登陆需要验证的smtp服务器,如果之前没有提供ehlo或者helo指令,则会先尝试ESMTP的ehlo指令。

SMTP.starttls([keyfile[, certfile]])

使smtp连接运行在TLS模式,所有的smtp指令都会被加密。

SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])

发送邮件,该方法需要一些邮件地址和消息。

SMTP.quit()

终止smtp会话并且关闭连接。

经过搜索学习发现网上大多都是用smtp类的sendmail这个方法来发邮件,那就先看看这个例子:

#!/usr/bin/python

import smtplib

from_mail='xxx@126.com'

to_mail='yyy@qq.com'

server=smtplib.SMTP('smtp.126.com')

server.docmd('ehlo','xxx@126.com')

server.login('xxx@126.com','password')

msg='''from:xxx@126.com

to:yyy@qq.com

subject:I am guol

I'm come 126

.

'''

server.sendmail(from_mail,to_mail,msg)

server.quit()

上例是使用sendmail方式,采用邮箱验证的模式来发邮件,因为现在大多数邮件系统都有反垃圾邮件机制及验证机制,我在开始实验非验证模式时就被126当作垃圾邮件而拒绝发送。

下面这个例子采用原始的smtp指令来发邮件。常用的smtp指令如下:

HELO 向服务器标识用户身份

MAIL 初始化邮件传输 mail from:

RCPT 标识单个的邮件接收人;常在MAIL命令后面,可有多个rcpt to:

DATA 在单个或多个RCPT命令后,表示所有的邮件接收人已标识,并初始化数据传输,以.结束

VRFY 用于验证指定的用户/邮箱是否存在;由于安全方面的原因,服务器常禁止此命令

EXPN 验证给定的邮箱列表是否存在,扩充邮箱列表,也常被禁用

HELP 查询服务器支持什么命令

NOOP 无操作,服务器应响应OK

QUIT 结束会话

RSET 重置会话,当前传输被取消

MAIL FROM 指定发送者地址

RCPT TO 指明的接收者地址

#!/usr/bin/python

import base64

import smtplib

from_mail='xxx@126.com'

to_mail='yyy@qq.com'

user=base64.encodestring('xxx@126.com').strip()

passwd=base64.encodestring('password').strip()

server=smtplib.SMTP('smtp.126.com')

server.set_debuglevel(1)

server.docmd('ehlo','xxx@126.com')

server.docmd('auth login')

server.docmd(user)

server.docmd(passwd)

server.docmd('mail FROM:<%s>' % from_mail)

server.docmd('rcpt TO:<%s>' % to_mail)

server.docmd('data')

server.docmd('''from:xxx@126.com

to:yyy@qq.com

subject:I am guol

I'm come here

.

''')

server.getreply()

server.quit()

注意在以上两个例子中在发送消息的subject后面书写正文时,需要空一行,正文以'.'结尾。

果然简单,如果想在邮件中携带附件、使用html书写邮件,附带图片等等,就需要使用email模块及其子模块。下面来看看email包,email包是用来管理email信息的,它包括MIME和其他基于RFC 2822的消息格式。email包的主要特征是在它内部解析和生成email信息是分开的模块来实现的。

MIME消息由消息头和消息体两大部分组成,在邮件里就是邮件头和邮件体。邮件头与邮件体之间以空行进行分隔。

邮件头包含了发件人、收件人、主题、时间、MIME版本、邮件内容的类型等重要信息。每条信息称为一个域,由域名后加“: ”和信息内容构成,可以是一行,较长的也可以占用多行。域的首行必须“顶头”写,即左边不能有空白字符(空格和制表符);续行则必须以空白字符打头,且第一个空白字符不是信息本身固有的。

邮件体包含邮件的内容,它的类型由邮件头的“Content-Type”域指出。最常见的类型有text/plain(纯文本)和text/html(超文本)。邮件体被分为多个段,每个段又包含段头和段体两部分,这两部分之间也以空行分隔。常见的multipart类型有三种:multipart/mixed, multipart/related和multipart/alternative。

在email的包里面包含了很多模块:

email.message

email.parser

email.generator

email.mime 创建email和MIME对象

email.header

email.charset

email.encoders

email.ereors

email.utils

email.iterators

主要来看看email.mime,在邮件中携带附件、图片、音频时,主要使用的是该模块。一般情况下,你通过解析一个文件或者一段text来生成一个消息对象结构,你也可以从头开始建立一个消息结构,实际上,你可以给一个已经存在的消息结构追加一个新的消息对象。你可以通过创建message实例来创建一个对象结构,然后给该结构追加附件和头部信息。email包提供了一些子类使得该操作变得很容易。

email.mime中常用的类如下:

email.mime.base email.mime.base.MIMEBase(_maintype, _subtype, **_params)

email.mime.nonmultipart email.mime.nonmultipart.MIMENonMultipart

email.mime.multipart email.mime.multipart.MIMEMultipart([_subtype[, boundary[, _subparts[, _params]]]])

A subclass of MIMEBase, this is an intermediate base class for MIME messages that are multipart. Optional _subtype defaults to mixed, but can be used to specify the subtype of the message. A Content-Type header of multipart/_subtype will be added to the message object. A MIME-Version header will also be added.

email.mime.application email.mime.application.MIMEApplication(_data[, _subtype[, _encoder[, **_params]]])

email.mime.audio email.mime.audio.MIMEAudio(_audiodata[, _subtype[, _encoder[, **_params]]])

email.mime.image email.mime.image.MIMEImage(_imagedata[, _subtype[, _encoder[, **_params]]])

A subclass of MIMENonMultipart, the MIMEImage class is used to create MIME message objects of major type image. _imagedata is a string containing the raw image data.

email.mime.message email.mime.message.MIMEMessage(_msg[, _subtype])

email.mime.text email.mime.text.MIMEText(_text[, _subtype[, _charset]])

A subclass of MIMENonMultipart, the MIMEText class is used to create MIME objects of major type text. _text is the string for the payload. _subtype is the minor type and defaults to plain. _charset is the character set of the text and is passed as a parameter to the MIMENonMultipart constructor; it defaults to us-ascii. If _text is unicode, it is encoded using the output_charset of _charset, otherwise it is used as-is.

只解释了常用的几个类,详细信息见: http://docs.python.org/2/library/email.mime.html

模拟在邮件内容中携带图片,如下:

import smtplib

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

from email.mime.image import MIMEImage

from_mail='xxx@126.com'

to_mail='yyy@qq.com'

msg=MIMEMultipart()

msg['From']=from_mail

msg['To']=to_mail

msg['Subject']='I am from email package'

body='I am come 126,i content with pic'

con=MIMEText('%s

' % body,'html')

msg.attach(con)

img=MIMEImage(file('/opt/25343674.png','rb').read())

img.add_header('Content-ID','/opt/25343674.png')

msg.attach(img)

server=smtplib.SMTP('smtp.126.com')

server.docmd('ehlo','xxx@126.com')

server.login('yyy@126.com','password')

server.sendmail(from_mail,to_mail,msg.as_string())

server.quit()

模拟在

邮件中携带附件

,如下:

import smtplib

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

from email.mime.image import MIMEImage

from_mail='xxx@126.com'

to_mail='yyy@qq.com'

msg=MIMEMultipart()

msg['From']=from_mail

msg['To']=to_mail

msg['Subject']='I am from email package'

content=MIMEText('I am come 126,i with attach','html')

msg.attach(content)

attac=MIMEImage(file('/opt/25343674.png','rb').read())

attac['Content-Type']='application/octet-stream'

attac.add_header('content-disposition','attachment',filename='/opt/25343674.png')

msg.attach(attac)

server=smtplib.SMTP('smtp.126.com')

server.docmd('ehlo','xxx@126.com')

server.login('yyy@126.com','password')

server.sendmail(from_mail,to_mail,msg.as_string())

server.quit()

以上代码均在python2.7下经过测试。

参考:

http://outofmemory.cn/code-snippet/1105/python-usage-smtplib-fayoujian-carry-fujian-code

http://docs.python.org/2/library/smtplib.html

http://stackoverflow.com/questions/3362600/how-to-send-email-attachments-with-python

smtplib python_Python2.7 smtplibemail相关推荐

  1. 发送电子邮件模块smtplib

    简介 电子邮件是最流行的互联网应用之一.在系统管理领域,我们常常使用邮件来发送告警信息.业务质量报表等,方便运维人员第一时间了解业务的服务状态.本节通过python的smtplib模块来实现邮件的发送 ...

  2. 【转】解决smtplib发送多人邮件没有展示收件人的问题

    [转]解决smtplib发送多人邮件没有展示收件人的问题 参考文章: (1)[转]解决smtplib发送多人邮件没有展示收件人的问题 (2)https://www.cnblogs.com/i-shu/ ...

  3. Python发送邮件smtplib.SMTP各报错问题的解决方法

    Python发送邮件smtplib.SMTP各报错问题的解决方法 参考文章: (1)Python发送邮件smtplib.SMTP各报错问题的解决方法 (2)https://www.cnblogs.co ...

  4. Python + HTMLTestRunner + smtplib 完成测试报告生成及发送测试报告邮件

    一下代码是自己结合教材,并结合以往用到的实例编写的代码,可以做为参考 import smtplib from email.mime.text import MIMEText from email.mi ...

  5. 转:python模块学习 ---- smtplib 邮件发送

    2019独角兽企业重金招聘Python工程师标准>>> 在基于互联网的应用中,程序经常需要自动地发送电子邮件.如:一个网站的注册系统会在用户注册时发送一封邮件来确认注册:当用户忘记登 ...

  6. Python网络协议模块学习之smtplib

    功能:smtplib模块是通过邮件服务器发送电子邮件,是smtp客户端的实现,支持邮件格式有:文本.HTML.Image.EXCEL等. 1.普通文本邮件 1 2 3 4 5 6 7 8 9 10 1 ...

  7. 安装了email模块还是报错_科普:利用Python smtplib和email模块实现自动发送邮件功能...

    概要 我们都知道SMTP(简单邮件传输协议),是一组用于从原地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式.SMTP规定电子邮件应该如何格式化.如何加密,以及如何在邮件服务器之间传递.SMT ...

  8. Python 技术篇-用smtplib和email库实现邮件发送各种类型的附件实例演示

    qq 账号发送邮箱登陆密码需要用授权码. 可以看我的这个文章: Python 技术篇-qq邮箱授权码开通 实现原理 我们用图片和文档两种类型的附件举个例子. MIMEBase("," ...

  9. Python 技术篇-用smtplib和email库实现邮件发送并展示本地图片实例演示

    qq账号发送邮箱登陆密码需要用授权码. 可以看我的这个文章: Python 技术篇-qq邮箱授权码开通 实现原理 将本地图片加入到邮件的附件中. m_img.add_header('Content-I ...

最新文章

  1. 题目1444:More is better
  2. Vivado 随笔(3) 其他综合属性 dont_touch、fsm_encoding?
  3. Struts2漏洞导致的反弹shell——青藤云安全使用的是agent进程采集器进行检测
  4. 高性能计算机存储部件有磁盘阵列,信息存储技术——磁盘阵列解读.pptx
  5. php链接中二维数组传参数,JS用POST怎么传送二维数组给PHP
  6. [js] setTimeout的第三个参数有什么用?
  7. 工行高级经理林承军:工行基于 MySQL 构建分布式架构的转型之路
  8. MTK MODEM(1)--- MTK平台NV基本功能与操作
  9. Hadoop-MR实现日志清洗(三)
  10. web渗透测试思路浅谈-----漏洞发现及利用
  11. 颠覆三观,内存真能当SSD用了!!!
  12. 【Java考试】易错知识点,期末考试踩坑题
  13. 58节沈大海H5edu.cn2016javaScript视频教程打包下载
  14. 【指纹识别】基于模板匹配算法实现教室指纹打卡系统含Matlab源码
  15. kodi电视smb android,小米/天猫魔盒KODI(XBMC)SMB协议播放测试
  16. 无线院2018下半年技术教练认证-专业能力测评初试
  17. 北京个体户税务申报如何办理?
  18. [AI开发]深度学习如何选择GPU?
  19. PW2051降压型DC/DC调整器芯片
  20. oracle更新右数第一位,Oracle数据库基本查询语句

热门文章

  1. vue.js 三种方式安装
  2. php加入语音播报功能_如何使用PHP实现智能语音播报
  3. Android 与手机通讯相关的状态和信息的类 TelephonyManager
  4. 10年前计算机水平,十年前的老电脑配置是啥样?系统要怎么装?今天算是见识了!...
  5. 动态二维码生成器PHP Dynamic QRcode
  6. 张晨光-JAVA零基础保姆式技术教程之-事务
  7. 基于javaweb的物流快递管理系统(java+ssm+html+js+jsp+mysql)
  8. 一碗麻辣烫利润多少?广州学做麻辣烫哪家靠谱
  9. python画气球_micro:bit + LoRa 实现气球追踪
  10. cdr 表格自动填充文字_PPT填充都可以怎么玩呢?