File tree Expand file tree Collapse file tree 2 files changed +9
-6
lines changed Expand file tree Collapse file tree 2 files changed +9
-6
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ class Settings(BaseSettings):
55
55
EMAIL_PORT : int = 465
56
56
EMAIL_USER :
str = '[email protected] '
57
57
EMAIL_PASSWORD : str = 'cvszjyenrlvfkeaef' # 授权密码,非邮箱密码
58
+ EMAIL_SSL : bool = True
58
59
59
60
# 邮箱登录验证码过期时间
60
61
EMAIL_LOGIN_CODE_MAX_AGE : int = 60 * 2 # 时效 60 * 2 = 2 分钟
Original file line number Diff line number Diff line change 17
17
def send_email_verification_code (send_to , code , text = SEND_RESET_PASSWORD_TEXT ):
18
18
"""
19
19
发送电子邮件验证码
20
+
20
21
:param send_to: 收件人
21
22
:param code: 验证码
22
23
:param text: 邮件文本内容
23
24
:return:
24
25
"""
25
- _text = text .replace (str ( _only_one ) , code )
26
+ _text = text .replace (_only_one , code )
26
27
message = MIMEMultipart ()
27
- subject = settings .EMAIL_DESCRIPTION
28
28
content = MIMEText (_text , _charset = "utf-8" )
29
29
message ['from' ] = settings .EMAIL_USER
30
- message ['subject' ] = subject
30
+ message ['subject' ] = settings . EMAIL_DESCRIPTION
31
31
message .attach (content )
32
32
33
33
# 登录smtp服务器并发送邮件
34
- smtp = smtplib .SMTP ()
35
34
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 )
36
39
smtp .connect (settings .EMAIL_SERVER )
37
40
smtp .login (settings .EMAIL_USER , settings .EMAIL_PASSWORD )
38
41
smtp .sendmail (message ['from' ], send_to , message .as_string ())
42
+ smtp .quit ()
39
43
except Exception as e :
40
44
log .error ('邮件发送失败 {}' , e )
41
- finally :
42
- smtp .quit ()
43
45
44
46
45
47
if __name__ == '__main__' :
You can’t perform that action at this time.
0 commit comments