标签:reset mail send main coding inter email tps 文件中
import smtplib
import os
from email.header import Header
from email.mime.text import MIMEText
# shenyuximr@163.com
def sendemail(host,usernames,verify):
# 发件人和收件人
sender = ‘luckyletop@163.com‘
# receiver = ‘xxxxxx@qq.com‘
receiver = ‘luckyletop@163.com‘
# 所使用的用来发送邮件的SMTP服务器
smtpserver = ‘smtp.163.com‘
# 发送邮箱的用户名和授权码(不是登录邮箱的密码)
username = ‘luckyletop@163.com‘
password = ‘UPLUATHPNLKSUCZM‘
# 邮件主题
mail_title = ‘接口测试报告‘
send_user = ‘奥里给‘
verify_url = "127.0.0.1"
# 读取html文件内容
# f = open(‘/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html‘, "r+", encoding="utf-8") # HTML文件默认和当前文件在同一路径下,若不在同一路径下,需要指定要发送的HTML文件的路径
# file_data = ""
# data = f.read()
# for line in data:
# if "userName" in data:
# line = line.replace("userName", send_user)
# elif "reset_link" in line:
# line = line.replace("reset_link", verify_url)
# elif "host" in line:
# line = line.replace("host","ddddddddddd")
# f.write(data)
# f.close()
with open(r"C:\Users\lenovo\Desktop\zi\inter.html", "r", encoding="utf-8") as f1,open("%s.bak" % r"C:\Users\lenovo\Desktop\zi\inter.html", "w+", encoding="utf-8") as f2:
# for line in f1.readlines():
# if ‘{host}‘ in line:
# line = line.replace(‘{host}‘, host)
# elif ‘{userName}‘ in line:
# line = line.replace(‘{userName}‘, usernames)
# elif ‘reset_link‘ in line:
# line = line.replace(‘reset_link‘, verify)
# f2.write(line)
# data = f2.read()
data = f1.read()
data = data.replace("{host}",host).replace("{userName}", usernames).replace("reset_link", verify)
f2.write(data)
# os.remove("/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html")
# os.rename("%s.bak" % "/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html", "/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html")
# data = f1.read()
# mail_body = f.read()
# print(mail_body.replace(‘{userName}‘, "ssssssssss"))
# print(mail_body.replace(‘{host}‘, "127.0.0.1"))
# print(mail_body.replace(‘reset_link‘, "verify_url"))
# 邮件内容, 格式, 编码
message = MIMEText(data, ‘html‘, ‘utf-8‘)
message[‘From‘] = sender
#多人发送
msg_to = [‘luckyletop@163.com‘,‘shenyuximr@163.com‘]
message[‘To‘] = ‘,‘.join(msg_to)
#单人发送
# message[‘To‘] = receiver
message[‘Subject‘] = Header(mail_title, ‘utf-8‘)
try:
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com‘)
smtp.login(username, password)
smtp.sendmail(sender, message[‘To‘].split(‘,‘), message.as_string())
print("发送邮件成功!!!")
smtp.quit()
except smtplib.SMTPException:
print("发送邮件失败!!!")
# import os
# def alter(file,old_str,new_str):
# """
# 将替换的字符串写到一个新的文件中,然后将原文件删除,新文件改为原来文件的名字
# :param file: 文件路径
# :param old_str: 需要替换的字符串
# :param new_str: 替换的字符串
# :return: None
# """
# with open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:
# for line in f1:
# if old_str in line:
# line = line.replace(old_str, new_str)
# f2.write(line)
# os.remove(file)
# os.rename("%s.bak" % file, file)
#
# alter("/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html", "host", "127.0.0.1")
# alter("/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html", "userName", "ssssss")
# alter("/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html", "reset_link", "verify")
if __name__ == ‘__main__‘:
sendemail(‘luckyletop@163.com‘, ‘luckyletop@163.com‘,‘smtp.163.com‘ )
标签:reset mail send main coding inter email tps 文件中
原文地址:https://www.cnblogs.com/djl-0628/p/14751038.html