#报错
from framework.logger import Logger
ModuleNotFoundError: No module named 'framework'
要发送邮件,报错这个模块,安装又找不到
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from framework.logger import Logger
from framework.settings import SendMail_INFO
logger = Logger(logger="SendEmail").getlog()
from framework.Sendmail import SendEmail
dic= {
'title':'发送邮件',#标题
'contect':'发送邮件',#邮件内容
'file_path':''#file_path不为空就走发送附件api,file_path为空就走文字邮件api
}
SendEmail.SendMailMian(dic)
SendMail_INFO={
"email_host":['smtp.163.com',25],#smtplib.SMTP_SSL("smtp.qq.com", 465)
'email_user':"hu******h@163.com",
'email_pwd':"DY******K" ,
'to':["h*****5@163.com","39*****6@qq.com","17*****@qq.com"], # 发送["h*****5@163.com","3*****6@qq.com","1*****8@qq.com"],
'Cc':["h*****5@163.com","3*****6@qq.com","1*****8@qq.com"] , # 抄送
}
class SendEmail():
def __init__(self):
self.msg_from =SendMail_INFO ['email_user']
self.passward = SendMail_INFO ['email_pwd'] # 授权码
self.email_host=SendMail_INFO['email_host']
self.to = SendMail_INFO ['to'] # 发送["h******5@163.com","3******6@qq.com","1******8@qq.com"]
self.Cc = SendMail_INFO ['Cc'] # 抄送
def send_attach(self,dic,):
# dic = {
# 'contect': '测试',
# 'title': '测试\n测试n\n\n\n\n\n\n\n测试测试测试',
# 'file_path': ''
# }
receiver = self.to + self.Cc
message = MIMEMultipart()
message['From'] = self.msg_from # Header("自动化测试平台系统", 'utf-8')#msg['From'] = msg_from
message['To'] =";".join(self.to)#收件人Header(";".join(to) , 'utf-8')
message["Cc"]=";".join(self.Cc)#Header(";".join(Cc) , 'utf-8') #抄送人";".join(Cc)
subject = (dic['title'])
message['Subject'] = Header(subject, 'utf-8')
message.attach(MIMEText((str(dic['contect'])), 'plain', 'utf-8'))
att = MIMEText(open(dic['file_path'], 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
#att["Content-Disposition"] = ('attachment; filename=' + file_name.split('\\')[-1]) #英文名称
att.add_header('Content-Disposition', 'attachment', filename=Header(dic['file_path'].split('\\')[-1], 'utf-8').encode())#中、英文名称
message.attach(att)
try:
#smtpObj = smtplib.SMTP(self.email_host) #smtplib.SMTP_SSL("smtp.qq.com", 465)
smtpObj = smtplib.SMTP(self.email_host[0], int(self.email_host[1])) # smtplib.SMTP_SSL("smtp.qq.com", 465)
smtpObj.login(self.msg_from, self.passward)
smtpObj.sendmail(self.msg_from, receiver, message.as_string())
logger.info('带附件发邮件,邮件发送成功')
except smtplib.SMTPException as e:
logger.error("带附件发邮件,无法发送邮件," +str( e.strerror))
def send_normal(self, dic):
# dic = {
# 'contect': '测试',
# 'title': '测试\n测试n\n\n\n\n\n\n\n测试测试测试',
# 'file_path': ''
# }
receiver = self.to + self.Cc
subject = dic['title']
content = (str(dic['contect']))
msg = MIMEText(content)
msg['From'] = self.msg_from#Header("自动化测试平台系统", 'utf-8')#msg['From'] = msg_from
msg['Subject'] = subject
msg['To'] = ";".join(self.to) # Header("相关", 'utf-8') #收件人
msg["Cc"] = ";".join(self.Cc) # 抄送人
try:
s = smtplib.SMTP(self.email_host[0], int(self.email_host[1])) #smtplib.SMTP_SSL("smtp.qq.com", 465)
#s = smtplib.SMTP('smtp.163.com', 25) # smtplib.SMTP_SSL("smtp.qq.com", 465)
s.login(self.msg_from, self.passward)
s.sendmail(self.msg_from, receiver, msg.as_string())
logger.info('不带附件发邮件,邮件发送成功')
except smtplib.SMTPException as e:
logger.error("Error: 不带附件发邮件,无法发送邮件" + format(e))
#print('发送失败' + format(e))
def SendMailMian(self,dic):
# dic = {
# 'title': '服务器监控测试[正常]',
# 'contect': strs,
# 'file_path': os.getcwd() + "\\logs\\%s.log" % rq
# }
if dic['file_path']=='':
self.send_normal(dic)#不带附件
else:
self.send_attach(dic)#带附件