Skip to content

Commit

Permalink
U 支持非SSL的邮件服务
Browse files Browse the repository at this point in the history
  • Loading branch information
vapao committed May 18, 2020
1 parent 83098fb commit a33cf03
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
13 changes: 8 additions & 5 deletions spug_api/apps/setting/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@ def ldap_test(request):

def email_test(request):
form, error = JsonParser(
Argument('server'),
Argument('port', type=int),
Argument('username'),
Argument('password'),
Argument('server', help='请输入邮件服务地址'),
Argument('port', type=int, help='请输入邮件服务端口号'),
Argument('username', help='请输入邮箱账号'),
Argument('password', help='请输入密码/授权码'),
).parse(request.body)
if error is None:
try:
server = smtplib.SMTP_SSL(form.server, form.port)
if form.port == 465:
server = smtplib.SMTP_SSL(form.server, form.port)
else:
server = smtplib.SMTP(form.server, form.port)
server.login(form.username, form.password)
return json_response()
except Exception as e:
Expand Down
7 changes: 5 additions & 2 deletions spug_api/libs/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
class Mail:
def __init__(self, server, port, username, password, nickname=None):
self.host = server
self.port = port
self.port = int(port)
self.user = username
self.password = password
self.nickname = nickname

def _get_server(self):
server = smtplib.SMTP_SSL(self.host, self.port)
if self.port == 465:
server = smtplib.SMTP_SSL(self.host, self.port)
else:
server = smtplib.SMTP(self.host, self.port)
server.login(self.user, self.password)
return server

Expand Down

0 comments on commit a33cf03

Please sign in to comment.