综述
用代码发送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服务器
连接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()
smtp_server.quit()
17:10:33.834993 send: ‘noop\r\n’
17:10:33.866534 reply: b’250 OK from 223.83.217.29 to newxmesmtplogicsvrsza7.qq.com.\r\n’
17:10:33.866590 reply: retcode (250); Msg: b’OK from 223.83.217.29 to newxmesmtplogicsvrsza7.qq.com.’
17:10:33.866609 send: ‘quit\r\n’
17:10:33.895248 reply: b’221 Bye.\r\n’
17:10:33.895300 reply: retcode (221); Msg: 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')
smtp.noop()
smtp.quit()
17:11:56.926867 connect: (‘smtp.qq.com’, 25)
17:11:56.926898 connect: to (‘smtp.qq.com’, 25) None
17:11:57.399699 reply: b’220 newxmesmtplogicsvrsza9.qq.com XMail Esmtp QQ Mail Server.\r\n’
17:11:57.399846 reply: retcode (220); Msg: b’newxmesmtplogicsvrsza9.qq.com XMail Esmtp QQ Mail Server.’
17:11:57.399893 connect: b’newxmesmtplogicsvrsza9.qq.com XMail Esmtp QQ Mail Server.’
17:11:57.399943 send: ‘noop\r\n’
17:11:57.425707 reply: b’250 OK from 223.83.217.29 to newxmesmtplogicsvrsza9.qq.com.\r\n’
17:11:57.425847 reply: retcode (250); Msg: b’OK from 223.83.217.29 to newxmesmtplogicsvrsza9.qq.com.’
17:11:57.425908 send: ‘quit\r\n’
17:11:57.451384 reply: b’221 Bye.\r\n’
17:11:57.451521 reply: retcode (221); Msg: 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()
smtp.quit()
17:12:57.405060 send: ‘noop\r\n’
17:12:57.432799 reply: b’250 OK from 223.83.217.29 to newxmesmtplogicsvrsza8.qq.com.\r\n’
17:12:57.432938 reply: retcode (250); Msg: b’OK from 223.83.217.29 to newxmesmtplogicsvrsza8.qq.com.’
17:12:57.432998 send: ‘quit\r\n’
17:12:57.462318 reply: b’221 Bye.\r\n’
17:12:57.462450 reply: retcode (221); Msg: b’Bye.’
这次使用的是 SMTP_SSL 函数,直接进行连接。端口465是默认的SMTP over SSL的端口号,同样,端口号可以省略。
登录SMTP服务器
连接上了SMTP服务器,接下来的动作是登录,用你自己的Email账户登录,使用 login 函数。
注意:没有logout函数,只有quit。只有成功登录,才可以发送邮件。
smtp = smtplib.SMTP('smtp.qq.com')
smtp.set_debuglevel(2)
# 此处的密码 不是真实的邮箱密码,而是授权码
smtp.login('your_emain_address@qq.com', 'password')
smtp.quit()
当login()函数使用的密码为你的邮箱真实密码时,就会出现smtplib.SMTPAuthenticationError: (535, b’Login Fail. Please enter your authorization code to login错误。
需要注意的是,此处使用的不是真实密码,而是邮箱授权码。
如何获得邮箱授权码
参考:https://blog.csdn.net/weixin_44915703/article/details/104417030
step1: 登陆qq邮箱,点击“设置” — “账户”。
step2:找到“POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务” ,点击“开启” SMTP服务。
step3: 根据提示要求,发送指定短信,即可获取授权码。
当填写正确的授权码之后,代码运行结果如下:
17:19:54.521560 send: ‘ehlo [127.0.1.1]\r\n’
17:19:54.551802 reply: b’250-newxmesmtplogicsvrszc9.qq.com\r\n’
17:19:54.551937 reply: b’250-PIPELINING\r\n’
17:19:54.551996 reply: b’250-SIZE 73400320\r\n’
17:19:54.552045 reply: b’250-STARTTLS\r\n’
17:19:54.552090 reply: b’250-AUTH LOGIN PLAIN\r\n’
17:19:54.552136 reply: b’250-AUTH=LOGIN\r\n’
17:19:54.552197 reply: b’250-MAILCOMPRESS\r\n’
17:19:54.552244 reply: b’250 8BITMIME\r\n’
17:19:54.552300 reply: retcode (250); Msg: b’newxmesmtplogicsvrszc9.qq.com\nPIPELINING\nSIZE 73400320\nSTARTTLS\nAUTH LOGIN PLAIN\nAUTH=LOGIN\nMAILCOMPRESS\n8BITMIME’
17:19:54.553428 send: ‘AUTH PLAIN ADk4MjgzMzQwNUBxcS5jb20Abm5seWp6aWpmYWZvYmJqYQ==\r\n’
17:19:54.688027 reply: b’235 Authentication successful\r\n’
17:19:54.688166 reply: retcode (235); Msg: b’Authentication successful’
17:19:54.688235 send: ‘quit\r\n’
17:19:54.716599 reply: b’221 Bye.\r\n’
17:19:54.716732 reply: retcode (221); Msg: b’Bye.’
通过SSL登录SMPT服务器
smtp = smtplib.SMTP_SSL('smtp.qq.com')
smtp.set_debuglevel(2)
# 此处的密码 不是真实的邮箱密码,而是授权码
smtp.login('your_emain_address@qq.com', 'password')
smtp.quit()
发送\抄送文本邮件
一封Email邮件,不仅仅是有一些字符串组成的内容,它是一个结构,有收件人,发件人,抄送名单,邮件主题等等。要组织好这样一个结构,我们才能发送邮件。而组织Email邮件内容结构的任务,不属于smtplib模块范围,我们需要用到email模块(标准库中的模块)提供的一些工具.
from email.header import Header
from email.mime.text import MIMEText
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@ibdsr.cn' # 发送邮件给多个人 msg['To'] = 'to1@qq.com,to2@qq.com'
msg['Cc'] = 'Cc@ict.ac.cn' # 抄送邮件
msg['Subject'] = Header('python email test', 'utf-8').encode()
smtp = smtplib.SMTP_SSL('smtp.qq.com')
smtp.login('from@qq.com', 'password')
smtp.sendmail('from@qq.com', ['to@ibdsr.cn', 'Cc@ict.ac.cn'], msg.as_string())
smtp.quit()
密送文本邮件
密送,就是收件人在邮件的头部(To和Cc地址)看不到自己的地址,但是还是收到了邮件。所有To和Cc地址中的收件人,都看不到密送地址。要实现密送,只需要在sendmail函数中,将密送地址加入第2个参数的list即可。
from email.header import Header
from email.mime.text import MIMEText
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@ibdsr.cn' # 发送邮件给多个人 msg['To'] = 'to1@qq.com,to2@qq.com'
msg['Cc'] = 'Cc@ict.ac.cn' # 抄送邮件
msg['Subject'] = Header('python email test', 'utf-8').encode()
smtp = smtplib.SMTP_SSL('smtp.qq.com')
smtp.login('from@qq.com', 'password')
smtp.sendmail('from@qq.com', ['to@ibdsr.cn', 'Cc@ict.ac.cn', 'secret_emial_add@163.com'], msg.as_string())
smtp.quit()
看上面的代码,注意添加密送地址secret_emial_add@163.com的位置。toaddr和ccaddr的收件人,都不会知道bccaddr的存在。
发送HTML邮件
在上一节中,基本上已经介绍完了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')
参考
- https://www.pynote.net/archives/356
- https://blog.csdn.net/weixin_44915703/article/details/104417030