python脚本发邮件,通常会用到smtplib和email这两个模块。看看该模块怎么使用,先看smtplib模块。 html
smtplib模块定义了一个简单的SMTP客户端,能够用来在互联网上发送邮件。
定义的类有以下:
python
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会话而且关闭链接。 app
通过搜索学习发现网上大多都是用smtp类的sendmail这个方法来发邮件,那就先看看这个例子: python2.7
#!/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后面书写正文时,须要空一行,正文以'.'结尾。 ui
果真简单,若是想在邮件中携带附件、使用html书写邮件,附带图片等等,就须要使用email模块及其子模块。下面来看看email包,email包是用来管理email信息的,它包括MIME和其余基于RFC 2822的消息格式。email包的主要特征是在它内部解析和生成email信息是分开的模块来实现的。 this
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]]])
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])
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('<b>%s</b><br><img src="cid:/opt/25343674.png"><br>' % 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('<b>I am come 126,i with attach<b>','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