在日常的工作中,經常需要對收款到賬進行監控,以確保收入的及時性和準確性。而Python作為一種優秀的編程語言,可以幫助我們快速地實現該功能。
import smtplib from email.mime.text import MIMEText import requests import json #設置收款信息 pay_url = 'https://xxxx.com/pay_check' payload = {'order_id': '123456', 'amount': '1000'} headers = {'Content-Type': 'application/json'} #設置發送郵箱信息 mail_host = "smtp.163.com" mail_user = "xxxxxx@163.com" mail_pass = "xxxxxx" sender = 'xxxxxx@163.com' receivers = ['xxxxxx@qq.com'] #查詢收款信息 res = requests.post(url = pay_url, data = json.dumps(payload), headers = headers) res_json = res.json() #判斷收款是否成功 if res_json['status'] == 'success': #發送郵件通知收款到賬 message = MIMEText('收款已到賬', 'plain', 'utf-8') message['From'] = sender message['To'] = ",".join(receivers) message['Subject'] = '收款到賬通知' try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) print("郵件已發送成功") except smtplib.SMTPException as e: print("Error: 無法發送郵件", e) else: print('收款尚未到賬')
上述代碼通過發送POST請求查詢收款信息,將返回的數據轉為JSON格式后,判斷收款是否成功。如果收款成功,則發送郵件通知;否則,輸出“收款尚未到賬”。
上一篇python監事鍵盤事件
下一篇java讀取流和寫入流