邮件,微信发送信息
邮箱发送邮件
import smtlib
from email.mime.text import MIMEText
from email.utils import formataddr
msg = MIMEText('邮件内容','plain','utf-8') # 发送内容
msg['From'] = formataddr(['发件人','发件人邮箱'])# 发件人
msg['To'] = formataddr(['收件人','收件人邮箱']) #收件人
msg['Subject'] = "[邮件主题]"
server = smtplib.SMTP('smtp.126.com',25) # SMTP 服务,端口固定
server.login('邮箱','密码') #登录
server.sendmail('发件人',['收件人',],msg.as_string())
server.quit()
微信信息
# pip3 install requests
import requests
import json
def get_access_token():
"""
获取微信全局接口的凭证(默认有效期俩个小时)
如果不每天请求次数过多, 通过设置缓存即可
"""
result = requests.get(
url="https://api.weixin.qq.com/cgi-bin/token",
params={
"grant_type": "client_credential",
"appid": "wx13f235a73fa3b42e",
"secret": "7f5a5ccd89f65de2b73e9eb3a4de9bf8",
}
).json()
if result.get("access_token"):
access_token = result.get('access_token')
else:
access_token = None
return access_token
def sendmsg(openid,msg):
access_token = get_access_token()
body = {
"touser": openid,
"msgtype": "text",
"text": {
"content": msg
}
}
response = requests.post(
url="https://api.weixin.qq.com/cgi-bin/message/custom/send",
params={
'access_token': access_token
},
data=bytes(json.dumps(body, ensure_ascii=False), encoding='utf-8')
)
# 这里可根据回执code进行判定是否发送成功(也可以根据code根据错误信息)
result = response.json()
print(result)
if __name__ == '__main__':
sendmsg('o2Ifb0va8Xp4zIidu8RYAR57ae-U','你好啊') #别人关注你才能发消息
读书使人心眼明亮