# -*- coding: utf-8 -*- from utils.config import config import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header #自适应邮件标题 def get_subject(text): default_subject='交易通知' if "买单成交" in text: return "买单成交" elif "卖单成交" in text: return "卖单成交" elif "启动" in text: return "机器人启动" elif "配置不当" in text: return "配置不当" elif "单创建失败" in text: return "订单创建失败" elif "撤销失败" in text: return "订单撤销失败" else: return default_subject #发出邮件 def send_email(text): #连接邮箱服务器 #con = smtplib.SMTP_SSL('mail.yangqiao.org', 465) con = smtplib.SMTP(config.SMTP_HOST, config.SMTP_PORT) #登录邮箱 con.login(config.SMTP_USER, config.SMTP_PASSWORD) # 准备数据 创建邮件对象 msg = MIMEMultipart() # 设置邮件主题 设置邮件发送者 设置邮件接受者 subject_and_sender=get_subject(text) subject = Header(subject_and_sender, 'utf-8').encode() msg['Subject'] = subject msg['From'] = (f"{subject_and_sender} <{config.SMTP_USER}>") msg['To'] = config.user_email # 添加文字内容 text = MIMEText(text, 'plain', 'utf-8') msg.attach(text) # 发送邮件 con.sendmail(config.SMTP_USER,config.user_email, msg.as_string()) con.quit() if __name__ == '__main__': config.loads('../conf/config.json') send_email("卖单成交")