注意from_addr自己是作为发信方,发到to_addrs
第一步,找到QQ邮箱的授权码
第二步,代码
以下修改from_addr、to_addrs(可以单个可以多个)、authorization_code,HTML放在py文件同一路径即可
# -*- coding: utf-8 -*-
# @Time : 2020/11/25 11:03
# @Author : Liqiju
# @File : Ptest.py
# @Software : PyCharm
# !/usr/bin/python
# -*- coding: UTF-8 -*-
#coding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from_addr='[email protected]' #邮件发送账号
to_addrs=['[email protected]','[email protected]'] #接收邮件账号,多个账号用列表
#to_addrs = '[email protected]'
authorization_code='vdxrxdvrmfqqbdhb' #授权码(这个要填自己获取到的)
smtp_server='smtp.qq.com' #固定的SMTP服务器
smtp_port=465 #固定端口
#配置服务器
stmp=smtplib.SMTP_SSL(smtp_server,smtp_port)
stmp.login(from_addr,authorization_code)
#组装发送内容
#message = MIMEText('我是胖胖,准备下班吃饭了', 'plain', 'utf-8') #发送的内容
# 创建一个带附件的实例
message = MIMEMultipart()
message['From'] = Header("喜哥", 'utf-8') #发件人
message['To'] = Header(header_name="李自动化测试", charset='utf-8') #收件人
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8') #邮件标题
message.attach(MIMEText('今晚会议室培训,别跑路了', 'plain', 'utf-8'))# 邮件正文内容
#增加HTML附件
atthtml = MIMEText(open('report.html','rb').read(),'base64','utf-8')#文件放在同一路径,不放在同一路径改一下比如'D:/test/report.html
atthtml["Content-Type"] = 'application/octet-stream'
atthtml["Content-Disposition"] = 'attachment;filename = "report.html"'
message.attach(atthtml)
try:
stmp.sendmail(from_addr, to_addrs, message.as_string())
except Exception as e:
print ('邮件发送失败--' + str(e))
print ('邮件发送成功')
看看运行结果:
有关于其他的内容可以去菜鸟教程学习一下,写得真滴不错。Python3 SMTP发送邮件
也可以加入QQ群965230944一起学习进步。
2020年11月25日
-------------------------------------------------------------------------------------------------------------------------------------------------
其实看看上面的图还是有很多的破绽,比如没有收件人,发件人也不是姓名+邮件地址,看起来不专业。下面的代码提供规范的收件人地址。
直接上代码:
# -*- coding: utf-8 -*-
# @Time : 2020/11/25 11:03
# @Author : Liqiju
# @File : Ptest.py
# @Software : PyCharm
# !/usr/bin/python
# -*- coding: UTF-8 -*-
#coding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr
from_addr='[email protected]' #邮件发送账号
to_addrs="[email protected],[email protected]" #接收邮件账号,字符串,多个账号用逗号分隔
authorization_code='vdxrxdvrmfqqbdhb' #授权码(这个要填自己获取到的)
smtp_server='smtp.qq.com' #固定的SMTP服务器
smtp_port=465 #固定端口
#配置服务器
stmp=smtplib.SMTP_SSL(smtp_server,smtp_port)
stmp.login(from_addr,authorization_code)
# 创建一个带附件的实例
subject = 'Python自动化测试报告'
message = MIMEMultipart()
message['From'] = formataddr(["李自动化测试",from_addr]) #发件人
message['To'] = to_addrs #收件人
message['Subject'] = Header(subject, 'utf-8') #邮件标题
message.attach(MIMEText('附件report.html为测试报告,请查阅', 'plain', 'utf-8'))# 邮件正文内容
#增加HTML附件
atthtml = MIMEText(open('report.html','rb').read(),'base64','utf-8')#文件放在同一路径,不放在同一路径改一下比如'D:\\test\\report.html
atthtml["Content-Type"] = 'application/octet-stream'
atthtml["Content-Disposition"] = 'attachment;filename = "report.html"'
message.attach(atthtml)
try:
stmp.sendmail(from_addr, to_addrs.split(','), message.as_string()) # 发送邮件,split分割后产生列表
except Exception as e:
print ('邮件发送失败--' + str(e))
print ('邮件发送成功')
先看看企业邮箱和QQ邮箱效果。
QQ邮箱
如果收件人和发件人是好友关系 ,应该是直接会显示你QQ备注的名称,看上面的QQ邮箱截图。没有的话才取的是message的From的内容
注意我们在发送的时候 to_addrs.split(',') 用逗号分隔,返回列表,下面是源码注释
def sendmail(self, from_addr, to_addrs, msg, mail_options=[],
rcpt_options=[]):
"""This command performs an entire mail transaction.
The arguments are:
- from_addr : The address sending this mail.
- to_addrs : A list of addresses to send this mail to. A bare
string will be treated as a list with 1 address.
- msg : The message to send.
- mail_options : List of ESMTP options (such as 8bitmime) for the
mail command.
- rcpt_options : List of ESMTP options (such as DSN commands) for
all the rcpt commands.
msg may be a string containing characters in the ASCII range, or a byte
string. A string is encoded to bytes using the ascii codec, and lone
\\r and \\n characters are converted to \\r\\n characters.
感谢阅读。
2020年11月26日