2019年7月19日 / 1,006次阅读 / Last Modified 2019年7月27日
Email
用代码发送Email,在很多场景下都有使用需求。基本思路是,代码准备好要发送的内容,然后连接发送邮箱的SMTP服务器,通过SMTP服务器将Email发送出去。比如,网站服务器定时发送解析log后的统计数据给维护人员,定期备份的数据库并通过邮件发送给管理人员,企业每个月发工资条邮件等等,这些需求很常见,甚至是基本需要。本文介绍如何通过Python代码实现发送常用的文本邮件和HTML邮件(发送其它类型的邮件,以后再说)。
SMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议。SMTP协议属于TCP/IP 协议簇,它帮助每台计算机在发送或中转Email的时候,找到下一个目的地。SMTP服务器就是遵循SMTP协议的发送邮件的服务器。 你的QQ信箱,Gmail或Hotmail信箱,这些公网上免费的Email服务提供商,基本上都会提供邮箱的SMTP服务。本文会介绍如何通过Python代码,连接这些SMTP服务器,然后发送Email。
连接SMTP服务器,要使用Python标准库中的smtplib模块。
>>> import smtplib
本文用连接QQ信箱的SMTP服务器来举例。下面的示例代码,连接QQ信箱的SMTP服务器,然后什么都不干,发一个noop(noop是一个命令,它什么都不做)后退出。
>>> smtp_server = smtplib.SMTP('smtp.qq.com', 25, timeout=3)
>>> smtp_server.set_debuglevel(2)
>>> smtp_server.noop()
14:04:50.133833 send: 'noop\r\n'
14:04:50.179080 reply: b'250 Ok\r\n'
14:04:50.179135 reply: retcode (250); Msg: b'Ok'
(250, b'Ok')
>>> smtp_server.quit()
14:04:57.365814 send: 'quit\r\n'
14:04:57.411823 reply: b'221 Bye\r\n'
14:04:57.411894 reply: retcode (221); Msg: b'Bye'
(221, b'Bye')
smtplib.SMTP
用来创建一个smtp服务器对象(smtp_server),创建时指明服务器,端口,超时时间(可选,如果超时,抛出socket.timeout异常)。smtp服务器对象的 set_debuglevel
函数,用来设置debug信息的显示级别,设置后,会将与SMTP服务器交互时的信息往来打印出来,以上代码使用了参数2,还可以使用参数1,即 smtp_server.set_debuglevel(1)
,区别是2有每条交互信息的时间显示,而1没有时间显示。然后,代码发了一个noop操作,服务器回应OK,什么都不干,最后使用quit函数断开连接。
SMTP服务器的默认端口号是25,以上代码,也可以将端口号25去掉。
下面是另外一种连接方式,使用 connect()
函数:
>>> smtp = smtplib.SMTP()
>>> smtp.set_debuglevel(2)
>>> smtp.connect('smtp.qq.com')
11:00:12.984431 connect: ('smtp.qq.com', 25)
11:00:12.984518 connect: to ('smtp.qq.com', 25) None
11:00:13.105895 reply: b'220 smtp.qq.com Esmtp QQ Mail Server\r\n'
11:00:13.106052 reply: retcode (220); Msg: b'smtp.qq.com Esmtp QQ Mail Server'
11:00:13.106091 connect: b'smtp.qq.com Esmtp QQ Mail Server'
(220, b'smtp.qq.com Esmtp QQ Mail Server')
>>> smtp.noop()
11:00:17.040109 send: 'noop\r\n'
11:00:17.090931 reply: b'250 Ok\r\n'
11:00:17.091019 reply: retcode (250); Msg: b'Ok'
(250, b'Ok')
>>> smtp.quit()
11:00:22.487833 send: 'quit\r\n'
11:00:22.540707 reply: b'221 Bye\r\n'
11:00:22.540788 reply: retcode (221); Msg: b'Bye'
(221, b'Bye')
默认连接的,是SMTP服务求的25号端口。使用25号端口有一个问题,就是保密性不够好,数据都是明文传输,没有加密。现在一般都推荐使用SSL,Secure Socket Layer。Python的smtplib模块支持SSL,请看下面的示例代码:
>>> smtp = smtplib.SMTP_SSL('smtp.qq.com', 465, timeout=3)
>>> smtp.set_debuglevel(2)
>>> smtp.noop()
11:32:05.576145 send: 'noop\r\n'
11:32:05.622854 reply: b'250 Ok\r\n'
11:32:05.622921 reply: retcode (250); Msg: b'Ok'
(250, b'Ok')
>>> smtp.quit()
11:32:14.480283 send: 'quit\r\n'
11:32:14.526599 reply: b'221 Bye\r\n'
11:32:14.526725 reply: retcode (221); Msg: b'Bye'
(221, b'Bye')
这次使用的是 SMTP_SSL
函数,直接进行连接。端口465是默认的SMTP over SSL的端口号,同样,端口号可以省略。
连接上了SMTP服务器,接下来的动作是登录,用你自己的Email账户登录,使用 login
函数。注意:没有logout函数,只有quit。只有成功登录,才可以发送邮件。
>>> smtp = smtplib.SMTP('smtp.qq.com')
>>> smtp.set_debuglevel(2)
>>> smtp.login('qqnumber@qq.com', 'password')
12:25:32.652292 send: 'ehlo [127.0.1.1]\r\n'
12:25:32.696438 reply: b'250-smtp.qq.com\r\n'
12:25:32.696493 reply: b'250-PIPELINING\r\n'
12:25:32.696503 reply: b'250-SIZE 73400320\r\n'
12:25:32.696512 reply: b'250-STARTTLS\r\n'
12:25:32.696520 reply: b'250-AUTH LOGIN PLAIN\r\n'
12:25:32.696527 reply: b'250-AUTH=LOGIN\r\n'
12:25:32.696534 reply: b'250-MAILCOMPRESS\r\n'
12:25:32.696541 reply: b'250 8BITMIME\r\n'
12:25:32.696560 reply: retcode (250); Msg: b'smtp.qq.com\nPIPELINING\nSIZE 73400320\nSTARTTLS\nAUTH LOGIN PLAIN\nAUTH=LOGIN\nMAILCOMPRESS\n8BITMIME'
12:25:32.698200 send: 'AUTH PLAIN ADEwOTMwMjMxMDJAcXEuY29tAG9pY3ExODcyMzY1NEAkJQ==\r\n'
12:25:33.079789 reply: b'235 Authentication successful\r\n'
12:25:33.079897 reply: retcode (235); Msg: b'Authentication successful'
(235, b'Authentication successful')
>>> smtp.quit()
12:25:52.860021 send: 'quit\r\n'
12:25:52.902692 reply: b'221 Bye\r\n'
12:25:52.902753 reply: retcode (221); Msg: b'Bye'
(221, b'Bye')
通过SSL登录SMPT服务器:
>>> smtp = smtplib.SMTP_SSL('smtp.qq.com')
>>> smtp.set_debuglevel(2)
>>> smtp.login('qqnumber@qq.com', 'password')
13:12:20.337882 send: 'ehlo [127.0.1.1]\r\n'
13:12:20.382797 reply: b'250-smtp.qq.com\r\n'
13:12:20.382849 reply: b'250-PIPELINING\r\n'
13:12:20.382860 reply: b'250-SIZE 73400320\r\n'
13:12:20.382867 reply: b'250-AUTH LOGIN PLAIN\r\n'
13:12:20.382874 reply: b'250-AUTH=LOGIN\r\n'
13:12:20.382880 reply: b'250-MAILCOMPRESS\r\n'
13:12:20.382886 reply: b'250 8BITMIME\r\n'
13:12:20.382898 reply: retcode (250); Msg: b'smtp.qq.com\nPIPELINING\nSIZE 73400320\nAUTH LOGIN PLAIN\nAUTH=LOGIN\nMAILCOMPRESS\n8BITMIME'
13:12:20.383182 send: 'AUTH PLAIN ADEwOTMwMjMxMDJAcXEuY29tAG9pY3ExODcyMzY1NEAkJQ==\r\n'
13:12:20.689279 reply: b'235 Authentication successful\r\n'
13:12:20.689540 reply: retcode (235); Msg: b'Authentication successful'
(235, b'Authentication successful')
>>> smtp.quit()
13:12:27.841389 send: 'quit\r\n'
13:12:27.888764 reply: b'221 Bye\r\n'
13:12:27.889036 reply: retcode (221); Msg: b'Bye'
(221, b'Bye')
一封Email邮件,不仅仅是有一些字符串组成的内容,它是一个结构,有收件人,发件人,抄送名单,邮件主题等等。要组织好这样一个结构,我们才能发送邮件。而组织Email邮件内容结构的任务,不属于smtplib模块范围,我们需要用到email模块(标准库中的模块)提供的一些工具:
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
发送一封文本邮件
>>> msg_str = 'this is a test email sending by python'
>>> msg = MIMEText(msg_str, 'plain', 'utf-8')
>>> msg['From'] = 'from@qq.com'
>>> msg['To'] = 'to@qq.com'
>>> msg['Subject'] = Header('python email test', 'utf-8').encode()
>>> smtp = smtplib.SMTP_SSL('smtp.qq.com')
>>> smtp.login('from@qq.com', 'passwd')
(235, b'Authentication successful')
>>> smtp.sendmail('from@qq.com', 'to@qq.com', msg.as_string())
{}
>>> smtp.quit()
(221, b'Bye')
直接,简单!文本邮件,MIMEText对象生成的时候,注意要使用plain参数。
Header
函数,用来对Email标题进行编码。在 sendmail
函数中,msg调用了自己的 as_string()
函数,将整个Email内容结构转换成字符串再发送。
我很好奇Heaer函数和as_string函数到底在干什么,正好现在可以看看:
>>> msg['Subject']
'=?utf-8?q?python_email_test?='
>>> print(msg.as_string())
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
From: from@qq.com
To: to@qq.com
Subject: =?utf-8?q?python_email_test?=
dGhpcyBpcyBhIHRlc3QgZW1haWwgc2VuZGluZyBieSBweXRob24=
Header函数将邮件标题转换成了标准Email格式。as_string函数运行后,得到的就是一封Base64编码的Email邮件。
注意一个细节:msg是MIMEText对象,不是一个dict对象,虽然很想,因此在某些循环发送Email的代码中,msg对象不能重复只用,如果只是修改msg['To']的值,想将相同的Email信息发送给不同的人,这样操作是不行的。正确的方式是:重新创建一个新的MIMEText对象。
将邮件发给多个人
在组织多个Email地址的时候,有两个不同的细节要处理:
>>> msg_str = 'this is a test email sending by python'
>>> msg = MIMEText(msg_str, 'plain', 'utf-8')
>>> msg['From'] = 'from@qq.com'
>>> msg['To'] = 'to1@qq.com,to2@qq.com'
>>> msg['Subject'] = Header('python email test', 'utf-8').encode()
>>> smtp = smtplib.SMTP_SSL('smtp.qq.com')
>>> smtp.login('from@qq.com', 'passwd')
(235, b'Authentication successful')
>>> smtp.sendmail('from@qq.com', ['to1@qq.com','to2@qq.com'], msg.as_string())
{}
>>> smtp.quit()
(221, b'Bye')
msg['To']的值包含多个Email地址,用逗号隔开;sendmail函数的第2个参数,是一个list。这样就实现了将邮件发给多个人,这多个收件人,都在收件人列表中。
抄送邮件
抄送邮件,要用到MIMEText对象的Cc值:
>>> msg_str = 'this is a test email with Cc addr'
>>> msg = MIMEText(msg_str, 'plain', 'utf-8')
>>> msg['From'] = 'from@qq.com'
>>> msg['To'] = 'toaddr@qq.com'
>>> msg['Cc'] = 'ccaddr@163.com'
>>> msg['Subject'] = Header('test Cc addr', 'utf-8').encode()
>>>
>>> smtp = smtplib.SMTP_SSL('smtp.qq.com')
>>> smtp.login('from@qq.com', 'password')
(235, b'Authentication successful')
>>> smtp.sendmail('from@qq.com', ['toaddr@qq.com','ccaddr@163.com'], msg.as_string())
{}
>>> smtp.quit()
(221, b'Bye')
用Msg['Cc']来装抄送列表。注意sendmail的第2个参数,这个参数是一个所有世间人的list,收件人和抄送人都在一个list里面。如果同学们想添加多个Cc地址,在Msg['Cc']中用逗号将地址隔开,并且所有的地址都要在sendmail的第2个参数中。
密送邮件
密送,就是收件人在邮件的头部(To和Cc地址)看不到自己的地址,但是还是收到了邮件。所有To和Cc地址中的收件人,都看不到密送地址。要实现密送,只需要在sendmail函数中,将密送地址加入第2个参数的list即可。
>>> msg_str = 'this is a test email with Cc addr'
>>> msg = MIMEText(msg_str, 'plain', 'utf-8')
>>> msg['From'] = 'from@qq.com'
>>> msg['To'] = 'toaddr@qq.com'
>>> msg['Cc'] = 'ccaddr@163.com'
>>> msg['Subject'] = Header('test Cc addr', 'utf-8').encode()
>>>
>>> smtp = smtplib.SMTP_SSL('smtp.qq.com')
>>> smtp.login('from@qq.com', 'password')
(235, b'Authentication successful')
>>> smtp.sendmail('from@qq.com', ['toaddr@qq.com','ccaddr@163.com','bccaddr@gmail.com'], msg.as_string())
{}
>>> smtp.quit()
(221, b'Bye')
看上面的代码,注意添加密送地址bccaddr@gmail.com的位置。toaddr和ccaddr的收件人,都不会知道bccaddr的存在。
关于 parseaddr 和 formataddr 这两个函数的应用,在别的文章中介绍。现在不用,也不影响发送邮件。
在上一节中,基本上已经介绍完了Python发送邮件的各项基本技能。发送HTML邮件,只有一个细节细节与上一节不同,就是在创建MIMEText对象的时候,使用html参数:
>>> msg_str = '<h1>Hi,there</hi><div><p>this is a html test email</p></div>'
>>> msg = MIMEText(msg_str, 'html', 'utf-8')
你需要做的,就是用HTML代码来作为邮件的内容。如果想把有外链的图片加入Email,这个方案简单有效。
不管是发送文本邮件,还是HTML邮件,MIMEText对象都是用 UTF-8 这个参数!(UTF-8感觉已经统一江湖了...)
以上内容,介绍了使用Python连接SMTP服务器,并发送邮件的基本技巧。
-- EOF --
本文链接:https://www.pynote.net/archives/356
《如何用Python发送Email邮件?》有1条留言
©Copyright 麦新杰 Since 2019 Python笔记
好文~~! [ ]