一、介绍 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装。
Python创建 SMTP 对象语法如下:
1 2 import smtplibsmtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
参数说明:
host: SMTP 服务器主机。 你可以指定主机的ip地址或者域名如:runoob.com,这个是可选参数。
port: 如果你提供了 host 参数, 你需要指定 SMTP 服务使用的端口号,一般情况下SMTP端口号为25。
local_hostname: 如果SMTP在你的本机上,你只需要指定服务器地址为 localhost 即可。
Python SMTP对象使用sendmail方法发送邮件,语法如下:
1 SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options]
参数说明:
from_addr: 邮件发送者地址。
to_addrs: 字符串列表,邮件发送地址。
msg: 发送消息
这里要注意一下第三个参数,msg是字符串,表示邮件。我们知道邮件一般由标题,发信人,收件人,邮件内容,附件等构成,发送邮件的时候,要注意msg的格式。这个格式就是smtp协议中定义的格式。
二、实例 1. Python发送邮件简单的实例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import smtplibfrom email.mime.text import MIMETextmail_host = 'smtp.163.com' mail_user = '***@163.com' mail_pass = '***' senderOne = 'yaokun_130@163.com' receiversOne = ['yaokun_130@163.com' ,'2237553939@qq.com' ] subject = 'Python SMTP 邮件发送测试' message['Subject' ] = subject message = MIMEText('第一封邮件发送' , 'plain' , 'utf-8' ) message['From' ] = "{}" .format(senderOne) message['To' ] = "," .join(receiversOne) try : smtpObj = smtplib.SMTP_SSL(mail_host, 465 ) smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(senderOne, receiversOne, message.as_string()) print("邮件发送成功" ) except smtplib.SMTPException: print("Error: 无法发送邮件" )
2. Python发送HTML格式的邮件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #!/usr/bin/python3 import smtplib from email.mime.text import MIMEText from email.header import Header # 第三方邮件服务器设置 mail_host = 'smtp.163.com' # smtp服务器 mail_user = '***@163.com' # 需要登录的邮箱账号 mail_pass = '***' # 邮箱密码或者授权码,需要开启smtp #内容项,下面有引用 sender = 'yaokun_130@163.com' # 发件人邮箱(mail_user = 'xx@qq.com') receivers = ['yaokun_130@163.com','2237553939@qq.com'] # 接收邮箱,可设置QQ邮箱或者其他邮箱 subject = 'Python SMTP 邮件发送测试' #主题 mail_msg = """ <p>Python 邮件发送测试...</p> <p><a href="https://blog.bwcxtech.com">这是我的博客</a></p> """ message['Subject'] = Header(subject, 'utf-8') # 主题 message = MIMEText(mail_msg, 'html', 'utf-8') #内容 message['From'] = "{}".format(sender) # 发送者 message['To'] = ",".join(receivers) # 接收者 try: smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465 smtpObj.login(mail_user, mail_pass) # 登录验证 smtpObj.sendmail(sender, receivers, message.as_string()) # 发送 print("邮件发送成功") except smtplib.SMTPException: print("Error: 无法发送邮件")
3. Python发送HTML格式加图片的邮件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #!/usr/bin/python3 import smtplib from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header # 第三方邮件服务器设置 mail_host = 'smtp.163.com' # smtp服务器 mail_user = '***@163.com' # 需要登录的邮箱账号 mail_pass = '***' # 邮箱密码或者授权码,需要开启smtp #内容项,下面有引用 sender = 'yaokun_130@163.com' # 发件人邮箱(mail_user = 'xx@qq.com') receivers = ['yaokun_130@163.com','2237553939@qq.com'] # 接收邮箱,可设置QQ邮箱或者其他邮箱 subject = 'Python SMTP 邮件发送测试' #主题 mail_msg = """ <p>Python 邮件发送测试...</p> <p><a href="https://blog.bwcxtech.com">我的博客链接</a></p> <p>图片演示:</p> <p><img src="cid:image1"></p> """ message = MIMEMultipart('related') msgAlternative = MIMEMultipart('alternative') msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8')) message['Subject'] = Header(subject, 'utf-8') message['From'] = "{}".format(sender) message['To'] = ",".join(receivers) message.attach(msgAlternative) # 指定图片 fp = open('test.jpg', 'rb') msgImage = MIMEImage(fp.read()) fp.close() # 定义图片 ID,在 HTML 文本中引用 msgImage.add_header('Content-ID', '<image1>') message.attach(msgImage) try: smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465 smtpObj.login(mail_user, mail_pass) # 登录验证 smtpObj.sendmail(sender, receivers, message.as_string()) # 发送 print("邮件发送成功") except smtplib.SMTPException: print("Error: 无法发送邮件")
4. Python发送带附件的邮件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #!/usr/bin/python3 import smtplib from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header # 第三方邮件服务器设置 mail_host = 'smtp.163.com' # smtp服务器 mail_user = '***@163.com' # 需要登录的邮箱账号 mail_pass = '***' # 邮箱密码或者授权码,需要开启smtp #内容项 sender = 'yaokun_130@163.com' # 发件人邮箱(mail_user = 'xx@qq.com') receivers = ['yaokun_130@163.com','2237553939@qq.com'] # 接收邮箱,可设置QQ邮箱或者其他邮箱 subject = 'Python SMTP 邮件发送测试' #主题 message = MIMEMultipart() message.attach(MIMEText('这是Python 邮件发送测试……', 'plain', 'utf-8')) message['From'] = "{}".format(sender) message['To'] = ",".join(receivers) message['Subject'] = Header(subject, 'utf-8') # 构造附件1,传送当前目录下的 test.txt 文件 att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 att1["Content-Disposition"] = 'attachment; filename="test.txt"' message.attach(att1) # 构造附件2,传送当前目录下的 runoob.txt 文件 att2 = MIMEText(open('runoob.txt', 'rb').read(), 'base64', 'utf-8') att2["Content-Type"] = 'application/octet-stream' att2["Content-Disposition"] = 'attachment; filename="runoob.txt"' message.attach(att2) try: smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465 smtpObj.login(mail_user, mail_pass) # 登录验证 smtpObj.sendmail(sender, receivers, message.as_string()) # 发送 print("邮件发送成功") except smtplib.SMTPException: print("Error: 无法发送邮件")