#!/usr/bin/python3 # -*- coding: utf-8 -*- import smtplib import os from email.mime.text import MIMEText from email.header import Header from email.mime.multipart import MIMEMultipart from datetime import datetime """ 将作业成功率的结果发邮件 """ #公共的 now=os.path.dirname(os.path.abspath(__file__)) file_path=os.path.join(now,"check_report.txt") week = str(datetime.now().isocalendar()[1]) def send_mail(): # 发件人 sender = 'mh@una-mail.com' # 接收邮件,可以发给多人 receivers = ['mh@una-mail.com','yk@una-mail.com','sr@una-mail.com','dn@una-mail.com'] # 邮件主体 msg = MIMEMultipart() # 正文取文本中的内容 with open(file_path, "r") as f: msgdata = f.read() message = "第{0}周,长期任务的作业成功率如下:\n{1}".format( week, msgdata) msg.attach(MIMEText(message, 'plain', _charset="utf-8")) # 发送者 msg['From'] = Header(sender, 'utf-8') # 接收者 msg['To'] = Header(receivers[0], 'utf-8') # 主题 subject = '【长期任务】第W{0}周作业成功率情况'.format(week) msg['Subject'] = Header(subject, 'utf-8') # 附件信息 #att = MIMEText(open(file_week_path, 'rb').read(), 'base64', 'utf-8') #att["Content-Type"] = 'application/octet-stream' #att["Content-Disposition"] = 'attachment; filename="{}"'.format( # file_name_week) #msg.attach(att) try: smtpObj = smtplib.SMTP('10.10.110.102') #身份认证 smtpObj.login(sender,"111111") smtpObj.sendmail(sender, receivers, msg.as_string()) print("邮件发送成功") except smtplib.SMTPException: print("Error: 无法发送邮件") if __name__ == '__main__': send_mail()