I tried with company Email system. It works.
You can refer to http://www.2cto.com/kf/201205/130791.html.
在日常
工作当中,我们经常要发送一些邮件,比如系统的监控、自动化运维、网站注册后的确认信等各种方面。我们可以通过
Python的smtplib模块轻松的实现发送电子邮件。
51Testing软件测试网#n,VM!tw
A smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])
51Testing软件测试网F
O
bXA1bH.T m
Ks:Q SMTP类构造函数,表示与SMTP服务器之间的连接,通过这个连接我们可以向SMTP服务器发送指令,执行相关操作(如:登陆、发送邮件)。该类提供了许多方法,将在下面介绍。它的所有参数都是可选的,其中host参数表示SMTP服务器主机名;port表示SMTP服务的端口,默认是25;如果在创建SMTP对象的时候提供了这两个参数,在初始化的时候会自动调用connect方法去连接服务器。
#at2P-kY(P4I,{D
`n.k0 smtplib模块还提供了SMTP_SSL类和LMTP类,对它们的操作与SMTP基本一致。
51Testing软件测试网rEW+YByZC3`b SMTP.set_debuglevel(level)
51Testing软件测试网MES*na `5f9mz&S 设置是否为调试模式。默认为False,即非调试模式,表示不输出任何调试信息。
51Testing软件测试网Fe oA)wm;X-M
b SMTP.connect([host[, port]])
51Testing软件测试网J9Em!SI[ 连接到指定的SMTP服务器。参数分别表示SMTP主机和端口。注意: 也可以在host参数中指定端口号(如:smtp.163.com:25),这样就没必要给出port参数。
51Testing软件测试网1y
ds(\:Tr;q'p$k'?%ZS SMTP.login(user, password)
51Testing软件测试网3KNP"@4_^ 登陆到SMTP服务器。现在几乎所有的SMTP服务器,都必须在验证用户信息合法之后才允许发送邮件。
ZVOl4aHbPs0 SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])
SGYEM;K0 发送邮件。这里要注意一下第三个参数,msg是字符串,表示邮件。我们知道邮件一般由标题,发信人,收件人,邮件内容,附件等构成,发送邮件的时候,要注意msg的格式。这个格式就是SMTP协议中定义的格式。
;B(_W2W
O{@P0 SMTP.quit()
\)eSH*`0 断开与SMTP服务器的连接,相当于发送"quit"指令。
51Testing软件测试网?xA*O@,J ~cn Python中可以使用emial模块用来处理邮件消息,包括MIME和其他基于RFC 2822的消息文档。使用这些模块来定义邮件的内容,是非常简单的。
'i1G J$SQ L6m0 class email.mime.multipart.MIMEMultipart: 多个MIME对象的集合
51Testing软件测试网*ExA1_+T class email.mime.audio.MIMEAudio: MIME音频对象
51Testing软件测试网E Xl0x%a g/[rK class email.mime.image.MIMEImage: MIME二进制文件对象
51Testing软件测试网*Z#`/G&s@7@-G class email.mime.text.MIMEText: MIME文本对象
51Testing软件测试网:RHFDi8@ 以上介绍只是对Python中smtplib模块和email模块的基本介绍,详细信息请参考官方手册。OK,下面给出如何使用Python发送可以带附件邮件的示例代码,如下:
tN"dO2nX01. #!/usr/bin/env python
51Testing软件测试网#H-Q3e
dt(q8IO7c2. #-*-coding:utf8-*-
/^N;^twd
M2C03.
1S x'b'tSD;C"kJ04. import os, smtplib, mimetypes
51Testing软件测试网2b&GV IQ@_3n5. from email.mime.text import MIMEText
L8VM
nj;S,]6d;a6b` S06. from email.mime.image import MIMEImage
&Ji.m(Bp~07. from email.mime.multipart import MIMEMultipart
/Lj'U\xJ08.
mJNvw;`09. MAIL_LIST = ["
username@51cto.com"]
51Testing软件测试网2g-v3w H#Z2\G10. MAIL_HOST = "smtp.51cto.com"
T#BjT0l"A"H9y
r011. MAIL_USER = "username"
51Testing软件测试网,Q*R$[~6h$f#R|F12. MAIL_PASS = "password"
gLj8E6W D013. MAIL_POSTFIX = "51cto.com"
Qf6P!r4W014. MAIL_FROM = MAIL_USER + "<"+MAIL_USER + "@" + MAIL_POSTFIX + ">"
i Py\|w"eZH015.
6E` hf!|'L016. def send_mail(subject, content, filename = None):
s)iQ b5vBB7Yp017. try:
51Testing软件测试网,`oU%j%u&Nq$Ur1p)X5gm~18. message = MIMEMultipart()
51Testing软件测试网3l{D _7rD19. message.attach(MIMEText(content))
@z"M.Pe020. message["Subject"] = subject
q2qU6l^ |;P0V021. message["From"] = MAIL_FROM
3{p.v%u TL@022. message["To"] = ";".join(MAIL_LIST)
~ kVGd~4{(T_023. if filename != None and os.path.exists(filename):
51Testing软件测试网G$nLY,MU24. ctype, encoding = mimetypes.guess_type(filename)
51Testing软件测试网e]'~2S2g*~'@:d.s(q
[
Z25. if ctype is None or encoding is not None:
51Testing软件测试网qH5VE/p:s/K\26. ctype = "application/octet-stream"
51Testing软件测试网2N"S;o)~4KK9Kn^,K27. maintype, subtype = ctype.split("/", 1)
51Testing软件测试网\g:L7q*uH IT"P9]28. attachment = MIMEImage((lambda f: (f.read(), f.close()))(open(filename, "rb"))[0], _subtype = subtype)
3|jG0VF+}2FB1H;A029. attachment.add_header("Content-Disposition", "attachment", filename = filename)
51Testing软件测试网U!a6u)oF
^y&M-{30. message.attach(attachment)
51Testing软件测试网m3X
z#X+OFQwJ31.
51Testing软件测试网U$A+Ne6N
o-K32. smtp = smtplib.SMTP()
51Testing软件测试网^$D:RiWF33. smtp.connect(MAIL_HOST)
51Testing软件测试网/oN5cUCTG0B34. smtp.login(MAIL_USER, MAIL_PASS)
51Testing软件测试网Thl6Y
n-c35. smtp.sendmail(MAIL_FROM, MAIL_LIST, message.as_string())
0W^uy6s;N036. smtp.quit()
51Testing软件测试网5GI l.SO)Ji5k37.
hxQ%O1\.VP`Vxs4Y!]038. return True
.CZPL
yt.[ o
{p039. except Exception, errmsg:
51Testing软件测试网oR)|8eG-Sy40. print "Send mail failed to: %s" % errmsg
51Testing软件测试网{7l
j8s)u8i,?41. return False
51Testing软件测试网Zgr Rux*Je.Q42.
51Testing软件测试网D*]#Q3ib+N
j!a43. if __name__ == "__main__":
8s#[$g$[
H|wx044. if send_mail("
测试信", "我的博客欢迎您/", r"G:\attachment.rar"):
51Testing软件测试网Ua/w"{G3P _o45. print "发送成功!"
51Testing软件测试网\!hwc$w@46. else:
N,R*W;n pF0^$l047. print "发送失败!"