| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- # -*- coding: UTF-8 -*-
- import smtplib
- from email.mime.text import MIMEText
- from email.header import Header
- import os
- import sys
- sys.path.append(os.path.join(os.path.abspath(__file__).split('auto')[0] + 'auto'))
- from base.base_load_config import load_config, get_base_path
- config_json = load_config()
- base_project = 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_SSL(self.mail_host)
- smtpObj.login(self.mail_user, self.mail_pass)
- smtpObj.sendmail(self.sender, self.receivers, message.as_string())
- print("邮件发送成功")
- except smtplib.SMTPException as e:
- print("Error: 无法发送邮件", e)
- # if __name__ == '__main__':
- # email = SendEmail(subject="测试邮件", title="测试邮件", text="这是一封测试邮件。")
- # email.send()
|