# -*- coding: UTF-8 -*- # 开发人员:萌狼蓝天 # 博客:Https://mllt.cc # 笔记:Https://cnblogs.com/mllt # 哔哩哔哩/微信公众号:萌狼蓝天 # 开发时间:2022/5/6 # Coding:UTF-8 import hashlib import flask, json from flask import request 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 import random server = flask.Flask(__name__) # 将该py文件转为一个服务 class EmailSentObject(): """ 发送邮件对象 """ mail_host = "smtp.ym.163.com" # 网易企业邮箱SMTP服务器 mail_admin = '' # 企业邮箱账号 mail_pwd = '' # 企业邮箱密码 mail_sender = "" # 企业邮箱账号 admin_name = "萌狼工作室" # 发信人姓名 请查阅 https://www.cnblogs.com/mllt/p/18286902 admin_email = mail_sender # 发信人邮箱 与企业邮箱账号保持一致 # nc -w 2 smtp.ym.163.com 465 < /dev/null && echo "port is ok" def __init__(self, receiver_email, receivers_name, subject, content): """ 邮件发送对象参数 :param receiver_email: 接收者邮件,类型为List :param receivers_name: 接收者姓名,类型为string :param subject: 邮件主题,类型为string :param content: 邮件正文,支持解析HTML标签,类型为string """ self.receivers_eamil = receiver_email # 接收者邮件,类型为List self.receivers_name = receivers_name # 接收者姓名,类型为string self.subject = subject # 邮件主题,类型为string self.content = content # 邮件正文,支持解析HTML标签,类型为string def sent(self): """ 发送邮件 :return: 成功返回 True | 失败返回 False """ message = MIMEMultipart('related') message['From'] = Header('{}<{}>'.format(self.admin_name, self.admin_email), 'utf-8') # 发信人 message['To'] = Header(self.receivers_name, 'utf-8') # 收信人 message['Subject'] = Header(self.subject, 'utf-8') # 邮件主题 msgAlternative = MIMEMultipart('alternative') message.attach(msgAlternative) msgAlternative.attach(MIMEText(self.content, 'html', 'utf-8')) try: smtpObj = smtplib.SMTP_SSL(self.mail_host) # 连接发信服务器 smtpObj.connect(self.mail_host, 465) smtpObj.login(self.mail_admin, self.mail_pwd) # 发信账号连接 smtpObj.sendmail(self.mail_sender, self.receivers_eamil, message.as_string()) # 发送邮件 return True except smtplib.SMTPException: return False def sendEmailCode(receiver, name): code = str(random.randint(0, 10)) + str(random.randint(0, 9)) + str(random.randint(0, 9)) + str( random.randint(0, 9)) + str(random.randint(0, 9)) + str(random.randint(0, 9)) subject = "【验证码】萌狼工作室旗下产品用户操作验证码" content = """

[萌狼工作室]操作验证,您的验证码为

{}

验证码10分钟内有效。

如果这不是你自己操作获得的邮件,忽略即可。

(本邮件自动发送,无需回复) """.format(code) sent = EmailSentObject(receiver, name, subject, content).sent() if sent: return code else: return -1 def send(receiver,name,key): if receiver == "": return {"Error": 0, "提示": "未填写收信邮箱"} # 未填写邮箱 if name == "": name = receiver md5hash = hashlib.md5() md5hash.update(key.encode(encoding='utf-8')) md5 = md5hash.hexdigest() if (md5 == "ffb4f6bee9830f0abbc215356a91ed2e"): code = sendEmailCode(receiver, name) if (code == -1): # 邮件发送失败 return {"Error": -2, "提示": "发送失败"} # 邮件发送失败 return {"user": name, "oemail": receiver, "code": code} return {"Error": -1, "提示": "没有权限"} # 无权限操作