diff --git a/config/common.dev.ini b/config/common.dev.ini index 22fc141..1f71632 100644 --- a/config/common.dev.ini +++ b/config/common.dev.ini @@ -21,15 +21,15 @@ port = 6379 ;[email] ;api = http://116.63.130.34:30001 ;sender = fecribd@fecr.com.cn +[email_tool] +sender_email = dogbillions@163.com +smtp_server = smtp.163.com +smtp_port = 25 +username = dogbillions@163.com +password = RMRMGULXYJHRMATM + -[fastapi_mail] -MAIL_USERNAME = dogbillions@163.com -MAIL_PASSWORD = RMRMGULXYJHRMATM -MAIL_FROM = dogbillions@163.com -MAIL_PORT = 25 -MAIL_SERVER = smtp.163.com -MAIL_FROM_NAME = 维德团队 [auth] jwt_key = MADASDZXC255f diff --git a/config/common.prod.ini b/config/common.prod.ini index 08ed8f1..4afa3dc 100644 --- a/config/common.prod.ini +++ b/config/common.prod.ini @@ -21,13 +21,12 @@ port = 6379 ;api = http://email_api:30001 ;sender = fecribd@fecr.com.cn -[fastapi_mail] -MAIL_USERNAME = dogbillions@163.com -MAIL_PASSWORD = RMRMGULXYJHRMATM -MAIL_FROM = dogbillions@163.com -MAIL_PORT = 25 -MAIL_SERVER = smtp.163.com -MAIL_FROM_NAME = 维德团队 +[email_tool] +sender_email = dogbillions@163.com +smtp_server = smtp.163.com +smtp_port = 25 +username = dogbillions@163.com +password = RMRMGULXYJHRMATM [auth] jwt_key = MADASDZXC255f diff --git a/context/common.py b/context/common.py index d128ea2..054ed7e 100644 --- a/context/common.py +++ b/context/common.py @@ -98,7 +98,7 @@ tianyancha_api = TianyanchaApi(conf['tianyancha']['token'], mongo_connect=mg_db) # 全局配置 # 邮件发送工具 -email_tool = EmailTool(dict(conf['fastapi_mail'])) +email_tool = EmailTool(**dict(conf['email_tool'])) # 邮箱验证码工具类 email_verify_code = EmailVerifyCode(redis_pool, email_tool) diff --git a/mods/user/router.py b/mods/user/router.py index ffed360..986bab6 100644 --- a/mods/user/router.py +++ b/mods/user/router.py @@ -24,19 +24,24 @@ router.include_router(auth_rule_router) def get_login_verify_code(body: schemas.GetLoginVerifyCodeReq, db: Session = Depends(get_db)): try: account = body.account + account_type = 'email' user = db.query(User).filter(User.email == account).first() if user: - account_type = 'email' - else: - user = db.query(User).filter(User.phone == account).first() - if user: - account_type = "phone" - else: - raise HTTPException(detail="账号未录入系统", status_code=303) - if account_type == 'email': email_verify_code.send_code(account, EmailVerifyType.login) - if account_type == 'phone': - phone_verify_code.send_code(account, PhoneVerifyType.login) + else: + raise HTTPException(detail="账号未录入系统", status_code=303) + # if user: + # account_type = 'email' + # else: + # user = db.query(User).filter(User.phone == account).first() + # if user: + # account_type = "phone" + # else: + # raise HTTPException(detail="账号未录入系统", status_code=303) + # if account_type == 'email': + # email_verify_code.send_code(account, EmailVerifyType.login) + # if account_type == 'phone': + # phone_verify_code.send_code(account, PhoneVerifyType.login) except HTTPException as e: raise e except Exception as e: @@ -49,18 +54,22 @@ def get_login_verify_code(body: schemas.GetLoginVerifyCodeReq, db: Session = Dep def login_by_verify_code(req: schemas.LoginByVerifyCode, db: Session = Depends(get_db)): account = req.account - if phone_verify_code.check_code(account, req.code, PhoneVerifyType.login): - account_type = 'phone' - else: - if email_verify_code.check_code(account, req.code, EmailVerifyType.login): - account_type = 'email' - else: - raise HTTPException(detail="验证码错误", status_code=303) - user = None - if account_type == 'phone': - user = db.query(User).filter(User.phone == account).first() - if account_type == 'email': - user = db.query(User).filter(User.email == account).first() + # if phone_verify_code.check_code(account, req.code, PhoneVerifyType.login): + # account_type = 'phone' + # else: + # if email_verify_code.check_code(account, req.code, EmailVerifyType.login): + # account_type = 'email' + # else: + # raise HTTPException(detail="验证码错误", status_code=303) + # user = None + # if account_type == 'phone': + # user = db.query(User).filter(User.phone == account).first() + # if account_type == 'email': + # user = db.query(User).filter(User.email == account).first() + + if not email_verify_code.check_code(account, req.code, EmailVerifyType.login): + raise HTTPException(detail="验证码错误", status_code=303) + user = db.query(User).filter(User.email == account).first() if not user: raise HTTPException(detail="账号未录入系统", status_code=303) auth_data = user_crud.get_user_auth(user) diff --git a/utils/email_utils.py b/utils/email_utils.py index d9238fb..8fe0816 100644 --- a/utils/email_utils.py +++ b/utils/email_utils.py @@ -2,7 +2,13 @@ import asyncio import json import requests import re -from fastapi_mail import FastMail, MessageSchema, ConnectionConfig, MessageType + +from fastapi import HTTPException +# from fastapi_mail import FastMail, MessageSchema, ConnectionConfig, MessageType +import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +from email.header import Header def email_check(email: str): @@ -13,38 +19,41 @@ def email_check(email: str): return re.match(reg, email) -class EmailTool: - def __init__(self, conf_dic): - """ - MAIL_USERNAME="dogbillions@163.com", - MAIL_PASSWORD="RMRMGULXYJHRMATM", - MAIL_FROM="dogbillions@163.com", - MAIL_PORT=conf_dic.get('MAIL_PORT', 25), - MAIL_SERVER="smtp.163.com", - MAIL_FROM_NAME="维德团队", - """ - conf = ConnectionConfig( - **conf_dic, - MAIL_STARTTLS=True, - MAIL_SSL_TLS=False, - USE_CREDENTIALS=True, - VALIDATE_CERTS=True, - # MAIL_PORT=int(conf_dic.get('MAIL_PORT', 25)) - ) - self.conf = conf - self.fm = FastMail(conf) - - def send_mail(self, title: str, email: str, msg_body: str): - message = MessageSchema( - subject=title, - body=f"

{msg_body}

", - subtype=MessageType.html, recipients=[email]) - try: - asyncio.run(fm.send_message(message)) - except Exception as e: - print(e.with_traceback(None)) - return False - +# class EmailTool: +# def __init__(self, conf_dic): +# """ +# MAIL_USERNAME="dogbillions@163.com", +# MAIL_PASSWORD="RMRMGULXYJHRMATM", +# MAIL_FROM="dogbillions@163.com", +# MAIL_PORT=conf_dic.get('MAIL_PORT', 25), +# MAIL_SERVER="smtp.163.com", +# MAIL_FROM_NAME="维德团队", +# """ +# conf_dic['MAIL_PORT'] = int(conf_dic.get('MAIL_PORT', 25)) +# conf = ConnectionConfig( +# **conf_dic, +# MAIL_STARTTLS=False, +# MAIL_SSL_TLS=False, +# USE_CREDENTIALS=True, +# VALIDATE_CERTS=True, +# # MAIL_PORT=int(conf_dic.get('MAIL_PORT', 25)) +# ) +# self.conf = conf +# self.fm = FastMail(conf) +# +# def send_email(self, title: str, email: str, msg_body: str): +# print(self.conf.MAIL_FROM) +# message = MessageSchema( +# subject=title, +# body=f"{msg_body}", +# charset='utf-8', +# subtype=MessageType.plain, recipients=[email]) +# try: +# asyncio.run(self.fm.send_message(message)) +# except Exception as e: +# print(e.with_traceback(None)) +# raise HTTPException(status_code=303, detail='邮件发送失败') +# def send_email(title: str, email: str, msg_body: str, email_api="http://email_api:30001", sender="dogbillions@163.com"): @@ -81,3 +90,29 @@ def send_email(title: str, email: str, msg_body: str, email_api="http://email_ap # body=f"

{msg_body}

", # subtype=MessageType.html, recipients=[email]) # asyncio.run(fm.send_message(message)) + + +class EmailTool: + def __init__(self, sender_email, smtp_server, smtp_port, username, password): + self.sender_email = sender_email + self.smtp_server = smtp_server + self.smtp_port = int(smtp_port) + self.username = username + self.password = password + + def send_email(self, title, email, body): + message = MIMEMultipart() + message['From'] = Header(self.sender_email) # 设置发件人信息,确保符合格式要求 + message['To'] = email + message['Subject'] = Header(title, 'utf-8') # 设置邮件主题 + message.attach(MIMEText(body, 'plain')) + # 使用SMTP对象连接到SMTP服务器 + try: + server = smtplib.SMTP(self.smtp_server, self.smtp_port) + server.starttls() + server.login(self.username, self.password) + server.sendmail(self.sender_email, email, message.as_string()) + server.quit() + except Exception as e: + print(f'邮件发送失败: {str(e)}') + raise HTTPException(status_code=303,detail='发送失败')