一、发送邮件
写脚本前需配置邮件获取授权码,如下图,开启POP3/SMTP服务
import yamailimport requestsimport timeimport hmacimport hashlibimport base64import urllib.parsepassword="邮箱授权码"host=""user="925******968@"to = ["wang*******60@"]cc = ["51*****5@","925****68@"]url = "/robot/send?access_token=ea67b3129ba5b6d54b186af690023b0290800075d6dfa46caf8eca68fca5ccb7"
def send_mail(subject,content,files=None): #发邮件函数smtp = yamail.SMTP(host=host,user=user,password=password,)smtp.send(to=to, cc=cc, subject=subject, contents=content, attachments=files)
二、自动发送叮叮消息
1、在叮叮群管理里面点击添加群机器人,设置机器人名字,选择安全设置后,会生成接口地址
2、发送叮叮消息需要选择一种安全认证方式,此处选择加签方式
3、叮叮接口文档地址参考:【链接】自定义机器人接入-钉钉开放平台
/document/app/custom-robot-access
def get_sign(): #发送叮叮群信息加签函数timestamp = str(round(time.time() * 1000))secret = 'SEC12e01f60fb7***0ba9e8d57261c9'#需在设置加签时获取secret_enc = secret.encode()string_to_sign = timestamp + '\n' + secretstring_to_sign_enc = string_to_sign.encode()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
def send_dd(content,at=None,at_all=False): #发送叮叮群消息函数data = {"msgtype": "text","text": {"content": content},"at": {"atMobiles": at,"isAtAll": at_all}}timestamp, sign = get_sign()params = {"timestamp": timestamp, 'sign': sign}r = requests.post(url, json=data, params=params)return r.json()