mailUtil.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #导入smtplib和MIMEText
  4. import smtplib
  5. from email.mime.text import MIMEText
  6. import json;
  7. import requests;
  8. #要发给谁
  9. mail_to="xiepan1990929@126.com"
  10. class MailUtil(object):
  11. def __init__(self):
  12. i=0;
  13. def send(self,to_list,sub,content):
  14. #设置服务器,用户名、口令以及邮箱的后缀
  15. mail_host="smtp.163.com"
  16. mail_user="vankye"
  17. mail_pass="chy520"
  18. mail_postfix="163.com"
  19. me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
  20. msg = MIMEText(content)
  21. msg['Subject'] = sub
  22. msg['From'] = me
  23. msg['To'] = to_list
  24. try:
  25. s = smtplib.SMTP()
  26. s.connect(mail_host)
  27. s.login(mail_user,mail_pass)
  28. s.sendmail(me, to_list, msg.as_string())
  29. s.close()
  30. #print '1'
  31. return True
  32. except Exception:
  33. #print '2'
  34. return False;
  35. def dingtalk(self,msg, webhook):
  36. headers = {'Content-Type': 'application/json; charset=utf-8'}
  37. data = {'msgtype': 'text', 'text': {'content': msg}, 'at': {'atMobiles': [], 'isAtAll': False}}
  38. post_data = json.dumps(data)
  39. # response = requests.post(webhook, headers=headers, data=post_data)
  40. # return response.text
  41. if __name__ == '__main__':
  42. # 輸出結果
  43. i=0;
  44. ms = MailUtil();
  45. # if ms.send("loemkie@163.com","17816189000","提交成功"):
  46. # print ("发送成功")
  47. # i=i+1;
  48. # if i>10:
  49. # exit
  50. # else:
  51. # print ("发送失败")
  52. # if ms.send("18868188188@163.com","17816189000","提交成功"):
  53. # print ("发送成功")
  54. # i=i+1;
  55. # if i>10:
  56. # exit
  57. # else:
  58. # print ("发送失败")
  59. webhook = 'https://oapi.dingtalk.com/robot/send?access_token=64d8b2c7fed4949e9433b807c7c5559939f1517af8f77c1dacb4de19c6910b56'
  60. ms.dingtalk("100"+"号码",webhook)