发邮件.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 固定配置的邮件发送脚本
  5. """
  6. import smtplib
  7. import ssl
  8. from email.mime.text import MIMEText
  9. from email.mime.multipart import MIMEMultipart
  10. #!/usr/bin/env python3
  11. # -*- coding: utf-8 -*-
  12. """
  13. 简化版 - 监控MySQL配置值变化
  14. """
  15. import time
  16. import pymysql
  17. import datetime
  18. # 数据库配置
  19. DB_CONFIG = {
  20. 'host': '127.0.0.1',
  21. 'port': 3306,
  22. 'user': 'root',
  23. 'password': 'Blossom2$',
  24. 'database': 'usdt_mall',
  25. 'charset': 'utf8mb4'
  26. }
  27. # 监控配置
  28. CHECK_INTERVAL = 600 # 10分钟
  29. CONFIG_KEY = 'receiving_address'
  30. def get_value():
  31. """获取当前值"""
  32. try:
  33. conn = pymysql.connect(**DB_CONFIG)
  34. with conn.cursor() as cursor:
  35. cursor.execute(
  36. "SELECT config_value FROM system_config WHERE config_key = %s",
  37. (CONFIG_KEY,)
  38. )
  39. result = cursor.fetchone()
  40. return result[0] if result else None
  41. except Exception as e:
  42. print(f"查询出错: {e}")
  43. return None
  44. finally:
  45. if 'conn' in locals():
  46. conn.close()
  47. def send_email(subject, body):
  48. """发送固定内容的邮件"""
  49. # ===== 配置信息 =====
  50. # SMTP服务器配置
  51. smtp_server = "smtppro.zoho.com"
  52. port = 465 # SSL端口
  53. # 发件人信息(请修改为你的密码)
  54. sender_email = "info@as1688.vip"
  55. sender_password = "jexSRAGgpmy0"
  56. # 收件人列表
  57. receivers = [
  58. "Williamjohson9699@gmail.com",
  59. "info@as1688.vip"
  60. ]
  61. # ===================
  62. # 创建邮件
  63. message = MIMEMultipart()
  64. message["From"] = sender_email
  65. message["To"] = ", ".join(receivers) # 多个收件人用逗号分隔
  66. message["Subject"] = subject
  67. # 添加邮件正文
  68. message.attach(MIMEText(body, "plain", "utf-8"))
  69. try:
  70. # 创建SSL连接
  71. context = ssl.create_default_context()
  72. print(f"正在连接到 {smtp_server}...")
  73. print(f"发件人: {sender_email}")
  74. print(f"收件人: {', '.join(receivers)}")
  75. print(f"主题: {subject}")
  76. print(f"内容: {body}")
  77. # 连接到SMTP服务器
  78. with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
  79. # 登录
  80. print("正在登录...")
  81. server.login(sender_email, sender_password)
  82. # 发送邮件
  83. print("正在发送邮件...")
  84. server.send_message(message)
  85. print("✅ 邮件发送成功!")
  86. except Exception as e:
  87. print(f"❌ 发送邮件时出错: {e}")
  88. print("\n可能的原因:")
  89. print("1. 密码错误 - 请检查 sender_password 是否正确")
  90. print("2. 需要应用专用密码 - 请在Zoho邮箱生成应用专用密码")
  91. print("3. 网络连接问题 - 请检查网络连接")
  92. print("4. SMTP设置问题 - 确认Zoho邮箱已开启SMTP访问")
  93. if __name__ == "__main__":
  94. print(f"开始监控 {CONFIG_KEY} 的变化...")
  95. print(f"检查间隔: {CHECK_INTERVAL/60} 分钟")
  96. print("-" * 50)
  97. previous = get_value()
  98. print(f"初始值: {previous}")
  99. count = 0
  100. while True:
  101. time.sleep(CHECK_INTERVAL)
  102. count += 1
  103. current = get_value()
  104. if current is None:
  105. continue
  106. if current != previous:
  107. print(f"[{datetime.datetime.now()}] 值发生变化!")
  108. print(f" 旧值: {previous}")
  109. print(f" 新值: {current}")
  110. previous = current
  111. send_email(f"[{datetime.datetime.now()}] 值发生变化!", f" 旧值: {previous} 新值: {current}")
  112. else:
  113. print(f"[{datetime.datetime.now()}] 第{count}次检查: 值未变化")