Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
ops_sdk
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PublicSource
ops_sdk
Commits
23cb9f19
Commit
23cb9f19
authored
May 16, 2019
by
shenshuo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
发邮件异步支持
parent
410d7db9
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
0 deletions
+62
-0
utils.py
websdk/utils.py
+62
-0
No files found.
websdk/utils.py
View file @
23cb9f19
...
@@ -80,6 +80,68 @@ class SendMail(object):
...
@@ -80,6 +80,68 @@ class SendMail(object):
print
(
str
(
e
))
print
(
str
(
e
))
return
False
return
False
class
SendMailAsync
(
object
):
def
__init__
(
self
,
mail_host
,
mail_port
,
mail_user
,
mail_password
,
mail_ssl
):
"""
:param mail_host: SMTP主机
:param mail_port: SMTP端口
:param mail_user: SMTP账号
:param mail_password: SMTP密码
:param mail_ssl: SSL=True, 如果SMTP端口是465,通常需要启用SSL, 如果SMTP端口是587,通常需要启用TLS
"""
self
.
mail_host
=
mail_host
self
.
mail_port
=
mail_port
self
.
__mail_user
=
mail_user
self
.
__mail_password
=
mail_password
self
.
mail_ssl
=
mail_ssl
async
def
send_mail
(
self
,
to_list
,
subject
,
content
,
subtype
=
'plain'
,
att
=
None
):
"""
:param to_list: 收件人,多收件人半角逗号分割, 必填
:param subject: 标题, 必填
:param content: 内容, 必填
:param subtype: 格式,默认:plain, 可选html
:param att: 附件,支持单附件,选填
"""
msg
=
MIMEMultipart
()
msg
[
'Subject'
]
=
subject
## 标题
msg
[
'From'
]
=
self
.
__mail_user
## 发件人
msg
[
'To'
]
=
to_list
# 收件人,必须是一个字符串
# 邮件正文内容
msg
.
attach
(
MIMEText
(
content
,
subtype
,
'utf-8'
))
if
att
:
if
not
os
.
path
.
isfile
(
att
):
raise
FileNotFoundError
(
'{0} file does not exist'
.
format
(
att
))
dirname
,
filename
=
os
.
path
.
split
(
att
)
# 构造附件1,传送当前目录下的 test.txt 文件
att1
=
MIMEText
(
open
(
att
,
'rb'
)
.
read
(),
'base64'
,
'utf-8'
)
att1
[
"Content-Type"
]
=
'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1
[
"Content-Disposition"
]
=
'attachment; filename="{0}"'
.
format
(
filename
)
msg
.
attach
(
att1
)
try
:
if
self
.
mail_ssl
:
'''SSL加密方式,通信过程加密,邮件数据安全, 使用端口465'''
# print('Use SSL SendMail')
server
=
smtplib
.
SMTP_SSL
()
await
server
.
connect
(
self
.
mail_host
,
self
.
mail_port
)
# 连接服务器
await
server
.
login
(
self
.
__mail_user
,
self
.
__mail_password
)
# 登录操作
await
server
.
sendmail
(
self
.
__mail_user
,
to_list
.
split
(
','
),
msg
.
as_string
())
server
.
close
()
else
:
# print('Use TLS SendMail')
'''使用普通模式'''
server
=
smtplib
.
SMTP
()
await
server
.
connect
(
self
.
mail_host
)
# 连接服务器
await
server
.
login
(
self
.
__mail_user
,
self
.
__mail_password
)
# 登录操作
await
server
.
sendmail
(
self
.
__mail_user
,
to_list
.
split
(
','
),
msg
.
as_string
())
server
.
close
()
return
True
except
Exception
as
e
:
print
(
str
(
e
))
return
False
class
SendSms
(
object
):
class
SendSms
(
object
):
def
__init__
(
self
,
REGION
,
DOMAIN
,
PRODUCT_NAME
,
sms_access_key_id
,
sms_access_key_secret
,
):
def
__init__
(
self
,
REGION
,
DOMAIN
,
PRODUCT_NAME
,
sms_access_key_id
,
sms_access_key_secret
,
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment