spam_livemail.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # Python 3.8.0
  2. import smtplib
  3. import time
  4. import imaplib
  5. import email
  6. import traceback
  7. import datetime
  8. import re
  9. import mysql.connector
  10. from mysql.connector import Error
  11. # -------------------------------------------------
  12. #
  13. # Utility to read email from Gmail Using Python
  14. #
  15. # ------------------------------------------------
  16. def XoopsExecuteQuery(isql):
  17. connection = mysql.connector.connect(
  18. host='hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com',
  19. database='xoops',
  20. user='hhh7796hhh',
  21. password='lYmWsu^ujcA1'
  22. )
  23. cursor = connection.cursor(dictionary=True)
  24. cursor.execute(isql)
  25. if cursor.rowcount == -1:
  26. return cursor.fetchall()
  27. else:
  28. connection.commit()
  29. return cursor.rowcount
  30. def EdmExecuteQuery(isql):
  31. connection = mysql.connector.connect(
  32. host='edm.hhh.com.tw',
  33. database='hhh_edm',
  34. user='root',
  35. password='bimbothin'
  36. )
  37. cursor = connection.cursor(dictionary=True)
  38. cursor.execute(isql)
  39. if cursor.rowcount == -1:
  40. return cursor.fetchall()
  41. else:
  42. connection.commit()
  43. return cursor.rowcount
  44. ORG_EMAIL = "noreply@hhh.com.tw"
  45. FROM_EMAIL = "noreply@hhh.com.tw"
  46. FROM_PWD = "Hhhn0123"
  47. SMTP_SERVER = "outlook.office365.com"
  48. SMTP_PORT = 993
  49. mail = imaplib.IMAP4_SSL('outlook.office365.com')
  50. mail.login(FROM_EMAIL, FROM_PWD)
  51. """ for folder in mail.list():
  52. print(folder) """
  53. mail.select('Inbox')
  54. result, data = mail.uid('search', None, "ALL") # (ALL/UNSEEN)
  55. i = len(data[0].split())
  56. print(i)
  57. for x in range(i):
  58. latest_email_uid = data[0].split()[x]
  59. result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)')
  60. # result, email_data = conn.store(num,'-FLAGS','\\Seen')
  61. # this might work to set flag to seen, if it doesn't already
  62. raw_email = email_data[0][1]
  63. raw_email_string = raw_email.decode('ISO-8859-1')
  64. email_message = email.message_from_string(raw_email_string)
  65. # Header Details
  66. date_tuple = email.utils.parsedate_tz(email_message['Date'])
  67. if date_tuple:
  68. local_date = datetime.datetime.fromtimestamp(
  69. email.utils.mktime_tz(date_tuple))
  70. local_message_date = "%s" % (
  71. str(local_date.strftime("%a, %d %b %Y %H:%M:%S")))
  72. email_from = str(email.header.make_header(
  73. email.header.decode_header(email_message['From'])))
  74. email_to = str(email.header.make_header(
  75. email.header.decode_header(email_message['To'])))
  76. try:
  77. subject = str(email.header.make_header(
  78. email.header.decode_header(email_message['Subject'])))
  79. except:
  80. subject = ""
  81. lst = re.findall(r'To: [\w.+-]+@[\w-]+\.[\w.-]+', raw_email.decode('UTF-8'))
  82. if local_date + datetime.timedelta(days=8) > datetime.datetime.now():
  83. if email_from.find('complaint') >= 0:
  84. #print(local_date)
  85. print(lst)
  86. if len(lst) > 0:
  87. XoopsExecuteQuery("update _users set email_status='B' where email='"+ lst[0].replace('To: ','') +"'")
  88. XoopsExecuteQuery("update _hnewspaper set onoff='0' where email='"+ lst[0].replace('To: ','') +"'")
  89. EdmExecuteQuery("delete from subscriber where email='"+ lst[0].replace('To: ','') +"'")
  90. EdmExecuteQuery("delete from subscriber_for_all where email='"+ lst[0].replace('To: ','') +"'")
  91. if email_from.lower() == 'mailer-daemon@amazonses.com':
  92. print(lst)
  93. if len(lst) > 0:
  94. XoopsExecuteQuery("update _users set email_status='B' where email='"+ lst[0].replace('To: ','') +"'")
  95. XoopsExecuteQuery("update _hnewspaper set onoff='0' where email='"+ lst[0].replace('To: ','') +"'")
  96. EdmExecuteQuery("delete from subscriber where email='"+ lst[0].replace('To: ','') +"'")
  97. EdmExecuteQuery("delete from subscriber_for_all where email='"+ lst[0].replace('To: ','') +"'")
  98. """ if x == i-2:
  99. print(raw_email.decode('UTF-8')) """
  100. #print(email_from)
  101. #print(email_from)
  102. # Body details
  103. """ for part in email_message.walk():
  104. if part.get_content_type() == "text/plain":
  105. # if subject == 'complaint':
  106. body = part.get_payload(decode=True)
  107. # print(email_from)
  108. try:
  109. dbody = body.decode('UTF-8')
  110. except:
  111. dbody = body.decode('ISO-8859-1')
  112. #lst = re.search(r'[\w.+-]+@[\w-]+\.[\w.-]+', raw_email.decode('utf-8'))
  113. lst = re.findall(r'[\w.+-]+@[\w-]+\.[\w.-]+', dbody)
  114. print(email_from)
  115. if len(lst) > 0 and email_from == "complaints@email-abuse.amazonses.com":
  116. if XoopsExecuteQuery("update _users set email_status='B' where email='"+ lst[0] +"'") > 0:
  117. print(lst[0])
  118. XoopsExecuteQuery("update _hnewspaper set onoff='0' where email='"+ lst[0] +"'")
  119. EdmExecuteQuery("delete from subscriber where email='"+ lst[0] +"'")
  120. EdmExecuteQuery("delete from subscriber_for_all where email='"+ lst[0] +"'")
  121. print(lst[0])
  122. if x == i-2:
  123. print(subject)
  124. print(email_from)
  125. else:
  126. continue """