Skip to content

Commit 854a4d8

Browse files
committed
更新邮件发送工具
1 parent 39b51eb commit 854a4d8

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

backend/app/core/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class Settings(BaseSettings):
5555
EMAIL_PORT: int = 465
5656
EMAIL_USER: str = '[email protected]'
5757
EMAIL_PASSWORD: str = 'cvszjyenrlvfkeaef' # 授权密码,非邮箱密码
58+
EMAIL_SSL: bool = True
5859

5960
# 邮箱登录验证码过期时间
6061
EMAIL_LOGIN_CODE_MAX_AGE: int = 60 * 2 # 时效 60 * 2 = 2 分钟

backend/app/utils/send_email.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,31 @@
1717
def send_email_verification_code(send_to, code, text=SEND_RESET_PASSWORD_TEXT):
1818
"""
1919
发送电子邮件验证码
20+
2021
:param send_to: 收件人
2122
:param code: 验证码
2223
:param text: 邮件文本内容
2324
:return:
2425
"""
25-
_text = text.replace(str(_only_one), code)
26+
_text = text.replace(_only_one, code)
2627
message = MIMEMultipart()
27-
subject = settings.EMAIL_DESCRIPTION
2828
content = MIMEText(_text, _charset="utf-8")
2929
message['from'] = settings.EMAIL_USER
30-
message['subject'] = subject
30+
message['subject'] = settings.EMAIL_DESCRIPTION
3131
message.attach(content)
3232

3333
# 登录smtp服务器并发送邮件
34-
smtp = smtplib.SMTP()
3534
try:
35+
if settings.EMAIL_SSL:
36+
smtp = smtplib.SMTP_SSL(host=settings.EMAIL_HOST, port=settings.EMAIL_PORT)
37+
else:
38+
smtp = smtplib.SMTP(host=settings.EMAIL_HOST, port=settings.EMAIL_PORT)
3639
smtp.connect(settings.EMAIL_SERVER)
3740
smtp.login(settings.EMAIL_USER, settings.EMAIL_PASSWORD)
3841
smtp.sendmail(message['from'], send_to, message.as_string())
42+
smtp.quit()
3943
except Exception as e:
4044
log.error('邮件发送失败 {}', e)
41-
finally:
42-
smtp.quit()
4345

4446

4547
if __name__ == '__main__':

0 commit comments

Comments
 (0)