# -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header import tools_load_config config_json = tools_load_config.load_config() base_project = tools_load_config.get_base_path() PROJECT_NAME = config_json.get('PROJECT_NAME') DB_USER = config_json.get('DB_USER') DB_PASSWORD = config_json.get('DB_PASSWORD') DB_IP = config_json.get('DB_IP') DB_PORT = config_json.get('DB_PORT') MONGO_LINK = f'mongodb://{DB_USER}:{DB_PASSWORD}@{DB_IP}:{DB_PORT}/' MAIL_HOST = config_json.get('MAIL_HOST') MAIL_USER = config_json.get('MAIL_USER') MAIL_PASS = config_json.get('MAIL_PASS') MAIL_SENDER = config_json.get('MAIL_SENDER') MAIL_RECEIVERS = config_json.get('MAIL_RECEIVERS') class SendEmail(object): def __init__(self, subject='auto subject', title='auto title', text='auto text') -> None: # 第三方 SMTP 服务 self.mail_host = MAIL_HOST # 设置服务器 self.mail_user = MAIL_USER # 用户名 self.mail_pass = MAIL_PASS # 口令 self.sender = MAIL_SENDER self.receivers = [MAIL_RECEIVERS] self.subject = subject self.title = title self.text = text def send(self): message = MIMEText(self.text, 'plain', 'utf-8') message['From'] = Header(self.title, 'utf-8') message['To'] = Header("auto", 'utf-8') message['Subject'] = Header(self.subject, 'utf-8') try: smtpObj = smtplib.SMTP() smtpObj.connect(self.mail_host, 25) smtpObj.login(self.mail_user, self.mail_pass) smtpObj.sendmail(self.sender, self.receivers, message.as_string()) print("邮件发送成功") except smtplib.SMTPException: print("Error: 无法发送邮件")