Django:发消息示例
一、http://mail.126.com/ 发送邮件
import smtplib from email.mime.text import MIMEText from email.utils import formataddr msg = MIMEText('老大,我今天需要请假。', 'plain', 'utf-8') # 发送内容 msg['From'] = formataddr(["海燕", 'xxxx@126.com']) # 发件人 msg['To'] = formataddr(["雅玲", 'ooooo@qq.com']) # 收件人 msg['Subject'] = "【请回复】请假事宜" # 主题 server = smtplib.SMTP("smtp.163.com", 25) # SMTP服务 server.login("xxxx@126.com", "密码") # 邮箱用户名和密码 server.sendmail('xxxx@126.com', ['ooooo@qq.com', ], msg.as_string()) # 发送者和接收者 server.quit()
二、QQ发送邮箱
import smtplib from email.mime.text import MIMEText _user = "你的qq邮箱" _pwd = "你的授权码" _to = "501257367@163.com" msg = MIMEText("Test") #要发送的内容 msg["Subject"] = "don't panic" #主题 msg["From"] = _user msg["To"] = _to try: s = smtplib.SMTP_SSL("smtp.qq.com ", 465) s.login(_user, _pwd) s.sendmail(_user, _to, msg.as_string()) s.quit() print("Success!") except smtplib.SMTPException as e: print() print("Falied,%s"%e )
import smtplib from email.mime.text import MIMEText from email.utils import formataddr class Qq(object): '''发送邮件''' def __init__(self): self.email = "2533916647@qq.com" #自己的邮箱 self.user = "不冷不热的温柔" #用户名 self.pwd = "uwaendbwhypweagi" def send(self,subject,body,to,name): print(222) msg = MIMEText(body, 'plain', 'utf-8') # 发送内容 msg['From'] = formataddr([self.user, self.email]) # 发件人 msg['To'] = formataddr([name,to]) # 收件人 msg['Subject'] =subject # 主题 server = smtplib.SMTP_SSL("smtp.qq.com", 465) # SMTP服务 print(333333) server.login(self.email,self.pwd) # 邮箱用户名和密码 server.sendmail(self.email, [to, ], msg.as_string()) # 发送者和接收者 server.quit()
三、微信发送消息
https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
# 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','你好啊') #别人关注你才能发消息
四、短息发送消息
每天逼着自己写点东西,终有一天会为自己的变化感动的。这是一个潜移默化的过程,每天坚持编编故事,自己不知不觉就会拥有故事人物的特质的。 Explicit is better than implicit.(清楚优于含糊)