smtplib模块定义SMTP类,该类可用于将邮件发送到任何具有SMTP或ESMTP侦听器守护程序的计算机。
smtplib官方说明文档
官方小例子
import smtplib
def prompt(prompt):
return raw_input(prompt).strip()
fromaddr = prompt("From: ")
toaddrs = prompt("To: ").split()
print "Enter message, end with ^D (Unix) or ^Z (Windows):"
# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
% (fromaddr, ", ".join(toaddrs)))
while 1:
try:
line = raw_input()
except EOFError:
break
if not line:
break
msg = msg + line
print "Message length is " + repr(len(msg))
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
简要介绍
#smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])
使用SMTP或者ESMTP协议初始化并创建连接
如果省略端口,则使用标准SMTP端口(25)
可选的timeout参数指定连接尝试的超时时间(以秒为单位),如果未指定,将使用全局默认超时设置
#smtplib.SMTP_SSL([host[, port[, local_hostname[, keyfile[, certfile[, timeout]]]]]])
与SMTP类功能几乎完全一致,允许使用SSL加密的套接字与服务器通信,默认端口为465
#smtplib.LMTP([host[, port[, local_hostname]]])
LMTP协议与ESMTP非常相似,主要基于标准的SMTP客户端。
#exception
#smtplib.SMTPException
smtplib模块的异常基类
#smtplib.SMTPServerDisconnected
当服务器意外断开连接或尝试在将SMTP实例连接到服务器之前使用它时将引发此异常
#smtplib.SMTPResponseException
SMTP错误代码基类。
在某些情况下,当SMTP服务器返回错误代码时,会引发此异常。
#smtplib.SMTPSenderRefused
发件人地址被拒绝
#smtplib.SMTPRecipientsRefused
收件人地址被拒绝
#smtplib.SMTPDataError
SMTP服务器拒绝接受邮件
#smtplib.SMTPConnectError
与服务器建立连接时出错
#smtplib.SMTPHeloError
服务器拒绝了HELO消息
#smtplib.SMTPAuthenticationError
SMTP身份验证出错
#SMTP.set_debuglevel(level)
设置调试级别,如果为True输出调试信息,否则不输出
#SMTP.docmd(cmd[, argstring])
向SMTP服务器发送cmd命令,返回由响应代码和相应组成的元祖
#SMTP.connect([host[, port]])
用指定端口连接到SMTP主机
#SMTP.helo([hostname])
使用HELO向SMTP服务器表明身份
#SMTP.ehlo([hostname])
使用扩展的HELO向SMTP服务器表明身份
#SMTP.verify(address)
检查用户邮件地址的有效性
#SMTP.login(user, password)
用户登录
#SMTP.starttls([keyfile[, certfile]])
将SMTP连接转入TLS模式,后面的所有SMTP指令都将被加密
#SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])
发送电子邮件
#SMTP.quit()
终止SMTP会话并关闭连接
简单的例子
smtpserver = 'smtp.163.com'
smtpuser = 'xxxxx@163.com'
authorizedcode = 'xxxxxxxxxxxx'
#163需登录邮箱,手动开通pop3/smtp服务
import smtplib,email.utils
_from = smtpuser
_to = ['xxxxxx0@qq.com','xxxxxx1@qq.com','xxxxxx2@qq.com']
_date = email.utils.formatdate()
_theme = 'test email'
_text = ('From:%s\nTo:%s\nDate:%s\nSubject:%s\n\n'%(_from,_to,_date,_theme))
_text += 'This is a test email, no reply.'
try:
server = smtplib.SMTP(smtpserver)
server.set_debuglevel(True)
server.login(smtpuser, authorizedcode)
failed = server.sendmail(_from,_to,_text)
if failed:
print(failed)
else:
print('Done')
finally:
server.quit()
输出结果
send: 'ehlo [xx.xx.xx.xx]\r\n'
reply: b'250-mail\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-AUTH LOGIN PLAIN\r\n'
reply: b'250-AUTH=LOGIN PLAIN\r\n'
reply: b'250-coremail 1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UFKqsuBUCa0xDrUUUUj\r\n'
reply: b'250-STARTTLS\r\n'
reply: b'250 8BITMIME\r\n'
reply: retcode (250); Msg: b'mail\nPIPELINING\nAUTH LOGIN PLAIN\nAUTH=LOGIN PLAIN\ncoremail 1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UFKqsuBUCa0xDrUUUUj\nSTARTTLS\n8BITMIME'
send: 'AUTH PLAIN AGxpeXVhbi41MzFAMTYzLmNvbQBVUFdOVldTRURSS01OQVZR\r\n'
reply: b'235 Authentication successful\r\n'
reply: retcode (235); Msg: b'Authentication successful'
send: 'mail FROM:<liyuan.531@163.com>\r\n'
reply: b'250 Mail OK\r\n'
reply: retcode (250); Msg: b'Mail OK'
send: 'rcpt TO:<413504861@qq.com>\r\n'
reply: b'250 Mail OK\r\n'
reply: retcode (250); Msg: b'Mail OK'
send: 'rcpt TO:<6512151@qq.com>\r\n'
reply: b'250 Mail OK\r\n'
reply: retcode (250); Msg: b'Mail OK'
send: 'rcpt TO:<35669852@qq.com>\r\n'
reply: b'250 Mail OK\r\n'
reply: retcode (250); Msg: b'Mail OK'
send: 'data\r\n'
reply: b'354 End data with <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>'
data: (354, b'End data with <CR><LF>.<CR><LF>')
send: b"From:xxxxx@163.com\r\nTo:['xxxxxx0@qq.com', 'xxxxxx1@qq.com', 'xxxxxx2@qq.com']\r\nDate:Mon, 07 Dec 2020 06:44:00 -0000\r\nSubject:test email\r\n\r\nThis is a test email, no reply.\r\n.\r\n"
reply: b'250 Mail OK queued as smtp8,DMCowAA3x9Ywz81fCa8aFw--.50286S2 1607323441\r\n'
reply: retcode (250); Msg: b'Mail OK queued as smtp8,DMCowAA3x9Ywz81fCa8aFw--.50286S2 1607323441'
data: (250, b'Mail OK queued as smtp8,DMCowAA3x9Ywz81fCa8aFw--.50286S2 1607323441')
send: 'quit\r\n'
Done
reply: b'221 Bye\r\n'
reply: retcode (221); Msg: b'Bye'