一、使用yamail发送邮件
import yamail
# import yagmail #邮件中附件是中文名,显示乱码
username = "xxxxxxx@qq.com"
password = "mugwgipyxkaxjdih"
host = "smtp.qq.com"
# host = "smtp.163.com"
smtp = yamail.SMTP(host=host,user=username,password=password)
#如果是一个人直接写字符串,发送多人则传list
smtp.send(to=["xxxxxxxxxxxx@163.com","xxxxxxxxxxx@qq.com"],subject="haoaho学习",contents="吃吃饭,睡觉觉",attachments=["a.jpg",r"F:\fei_Python\Mycode\day6\all_name_list.txt"])
# smtp.send(
# to=["xxxxxx","aaaaaaa"],
# cc=["xxxxxx1","aaaaaaa1"],
# subject="haoaho学习",
# contents="吃吃饭,睡觉觉",
# attachments="a.jpg"
# )
一、使用request发送dingding群消息
1、关键字发送
#包含关键字发送
url = "https://oapi.dingtalk.com/robot/send?access_token=3xxxxxxxxxxxxxxxxxxxxxxxxxxxxx3a"
data = {
"msgtype": "text",
"text": {
"content": "fmz,快下课了"
},
#添加atall可以@所有人,atMobiles会@摩西尔
"at":{
# "atMobiles":["xxxxxx"],
"isAtAll":True
}
}
r = requests.post(url,json=data)
print(r.json())
2. 加签方式
#计算签名
import time
import hmac
import hashlib
import base64
import urllib.parse
def get_dd_sign():
timestamp = str(round(time.time() * 1000))
secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
return timestamp,sign
# print(timestamp)
# print(sign)
def sent_dd_msg(msg,at=None,at_all=False):
timestamp,sign = get_dd_sign()
params = {
"access_token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"timestamp":timestamp,
"sign":sign
}
# url = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxa"
# url = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
url = "https://oapi.dingtalk.com/robot/send"
data = {
"msgtype": "text",
"text": {
"content": msg
},
#添加atall可以@所有人,atMobiles会@摩西尔
"at":{
"atMobiles":at,
"isAtAll":at_all
}
}
r = requests.post(url,json=data,params=params)
print(r.json())
def main():
msg = "开始了"
sent_dd_msg(msg)