SMTP发送邮件
发送邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 配置邮箱服务器
smtpserver = "smtp.163.com"
# 用户/密码
user = "admin@163.com"
password = "123456"
# 发送者邮箱
sender = "admin@163.com"
# 接收者邮箱
receiver = "123456@163.com"
# 邮件主题
subject = "Python email test"
msg = MIMEText('<html><h1>你好!</h1></html>', "html", "utf-8")
msg["Subject"] = Header(subject, "utf-8")
if __name__ == '__main__':
smtp = smtplib.SMTP()
smtp.connect(smtpserver,25)
smtp.login(user,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
运行代码报错:
Traceback (most recent call last):
File "C:/Users/LGY/PycharmProjects/t1/python-邮件.py", line 25, in <module>
smtp = smtplib.SMTP()
File "D:\Python_tools\Python36\lib\smtplib.py", line 261, in __init__
fqdn = socket.getfqdn()
File "D:\Python_tools\Python36\lib\socket.py", line 674, in getfqdn
hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd3 in position 2: invalid continuation byte
解决方法:修改SMTP导入socket包中的代码
# 原代码
hostname, aliases, ipaddrs = gethostbyaddr(name)
# 新代码
hostname, aliases, ipaddrs = gethostbyaddr(name.encode("ascii","ignore"))
发送多人邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 配置邮箱服务器
smtpserver = "smtp.163.com"
# 用户/密码
user = "admin@163.com"
password = "123456"
# 发送者邮箱
sender = "admin@163.com"
# 接收者邮箱
receiver = ["123456@163.com","234567@qq.com"]
# 邮件主题
subject = "Python-email2"
msg = MIMEText('<html><h1>你好!</h1></html>', "html", "utf-8")
msg["Subject"] = Header(subject, "utf-8")
# 多人接收邮件,直接显示下面账号的名字
msg['From'] = "admin@163.com"
msg['To'] = "123456@163.com;234567@qq.com"
if __name__ == '__main__':
smtp = smtplib.SMTP()
smtp.connect(smtpserver, 25)
smtp.login(user, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
需要注意添加上msg[“From”]和msg[“To”],而且要跟接收方邮箱相同(receiver),否则可能会报以下错误:
smtplib.SMTPDataError: (554, b'DT:SPM 163 smtp3,G9xpCgAnchCrbkFdS_YZAA--.189S2 1564569259,please see http://mail.163.com/help/help_spam_16.htm?ip=157.122.54.188&hostid=smtp3&time=1564569259')
抄送人
主要添加msg["cc"]="234567@qq.com"
即可
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 配置邮箱服务器
smtpserver = "smtp.163.com"
# 用户/密码
user = "admin@163.com"
password = "123456"
# 发送者邮箱
sender = "admin@163.com"
# 接收者邮箱
receiver = ["123456@163.com","234567@qq.com"]
# 邮件主题
subject = "Python-email2"
msg = MIMEText('<html><h1>你好!</h1></html>', "html", "utf-8")
msg["Subject"] = Header(subject, "utf-8")
#多人接收邮件,一个接收方,一个是抄送
msg['From'] = "admin@163.com"
msg['To'] = "123456@163.com"
msg["cc"]="234567@qq.com"
if __name__ == '__main__':
smtp = smtplib.SMTP()
smtp.connect(smtpserver, 25)
smtp.login(user, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
添加附件,主要用于结合BeautifulReport生成的测试报告
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
# 配置邮箱服务器
smtpserver = "smtp.163.com"
# 用户/密码
user = "admin@163.com"
password = "123456"
# 发送者邮箱
sender = "admin@163.com"
# 接收者邮箱
receiver = ["123456@163.com","234567@qq.com"]
# 邮件主题
subject = "Python-email3"
msg = MIMEMultipart()
# 多人接收邮件,直接显示下面账号的名字
msg['From'] = "admin@163.com"
msg['To'] = "123456@163.com"
msg["cc"]="234567@qq.com"
msg["Subject"] = Header(subject, "utf-8")
# 添加正文
msg.attach(MIMEText('<html><h1>你好!</h1></html>', "html", "utf-8"))
# 添加附近
sendFile = open("./测试报告.html", 'rb').read()
att = MIMEText(sendFile, "base64", "utf-8")
att.add_header("Content-Type", "application/octet-stream")
att.add_header("Content-Disposition", "attachment", filename="测试报告.html")
msg.attach(att)
if __name__ == '__main__':
smtp = smtplib.SMTP()
smtp.connect(smtpserver, 25)
smtp.login(user, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
扩展-发送最新的测试报告
# 利用python中的os.path.getmtime可以查看创建文件的时间,通过这个时间进行判断,发送最新的报告
import os
print(os.path.getmtime("./report/测试报告.html"))
- 实现
import os
# 取出文件夹中的文件名列表
list_filename = os.listdir("./report")
print(list_filename)
list_filename.sort(key=lambda x:os.path.getmtime("./report/"+x),reverse=True)
print("最新的文件名为:",list_filename[0])