import smtplib from email.mime.text import MIMEText from email.header import Header from email.utils import formataddr import aiosmtplib import asyncio # 发送邮件函数 class EmailService(): def __init__(self): self.mail_host = "smtp.ym
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr
import aiosmtplib
import asyncio
# 发送邮件函数
class EmailService():
def __init__(self):
self.mail_host = "smtp.ym.163.com" # 设置服务器
self.mail_port = 465
# self.mail_user = "xxx" # 用户名
self.mail_user = "xxx" # 用户名
self.mail_pass = "xxx"
self.sender = 'xxx'
async def SendEmail(self, mail_to, subject, msg):
receivers = mail_to.split(',')
if len(receivers) == 0:
return False
if len(msg) == 0:
return False
message = MIMEText(msg, 'html', 'utf-8')
# message['From'] = formataddr(['监控组', self.mail_user])
message['From'] = self.mail_user
# Header("警告处理人员", 'utf-8')
# message['To'] = formataddr(['警告处理人员', receivers[0]])
message['To'] = receivers[0]
if subject == "":
subject = '验证码'
message['Subject'] = Header(subject, 'utf-8')
try:
async with aiosmtplib.SMTP(self.mail_host, self.mail_port,use_tls=True) as smtpObj:
await smtpObj.login(self.mail_user, self.mail_pass)
await smtpObj.sendmail(self.sender, receivers, message.as_string())
await smtpObj.quit()
except aiosmtplib.SMTPException as e:
return False
return True
emailService = EmailService()
await emailService.SendEmail(email, subject, msg)
#