Day7 - 发送邮件yamail模块及钉钉消息
生活随笔
收集整理的這篇文章主要介紹了
Day7 - 发送邮件yamail模块及钉钉消息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、使用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.parsedef 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)總結
以上是生活随笔為你收集整理的Day7 - 发送邮件yamail模块及钉钉消息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 8.4.3.1 模板方法模式
- 下一篇: 收银软件哪家强?2023年收银软件排行榜