Skip to content
Snippets Groups Projects
Commit 931ec8a8 authored by Recolic's avatar Recolic :house_with_garden:
Browse files

.

parent aae03f0b
No related branches found
No related tags found
No related merge requests found
# usage: python archived/py/listemail.py (genpasswd btest@recolic.net) # python archived/py/listemail2.py (genpasswd btest@recolic.net)
import imaplib import imaplib
import email import email
import re
from email.header import decode_header from email.header import decode_header
import sys import sys
IMAP_SERVER = "imap.recolic.net" IMAP_SERVER = "imap.recolic.net"
USERNAME = "btest@recolic.net" USERNAME = "btest@recolic.net"
PASSWORD = sys.argv[1] PASSWORD = sys.argv[1]
Search_Regex = r"<b>\$[0-9\.]*</b>"
# Connect and upgrade to TLS # Connect and upgrade to TLS
mail = imaplib.IMAP4(IMAP_SERVER, 143) mail = imaplib.IMAP4(IMAP_SERVER, 143)
...@@ -22,20 +24,40 @@ if status != "OK": ...@@ -22,20 +24,40 @@ if status != "OK":
print("Failed to fetch emails") print("Failed to fetch emails")
exit() exit()
# Get list of email IDs
email_ids = messages[0].split() email_ids = messages[0].split()
# Fetch and print subject lines
for eid in email_ids: for eid in email_ids:
status, msg_data = mail.fetch(eid, "(RFC822.HEADER)") status, msg_data = mail.fetch(eid, "(RFC822)")
if status != "OK": if status != "OK":
continue continue
msg = email.message_from_bytes(msg_data[0][1]) msg = email.message_from_bytes(msg_data[0][1])
# Decode subject
subject, encoding = decode_header(msg["Subject"])[0] subject, encoding = decode_header(msg["Subject"])[0]
if isinstance(subject, bytes): if isinstance(subject, bytes):
subject = subject.decode(encoding or "utf-8", errors="ignore") subject = subject.decode(encoding or "utf-8", errors="ignore")
print(subject) print(f"Subject: {subject}")
# Extract body
body = ""
if msg.is_multipart():
for part in msg.walk():
content_type = part.get_content_type()
if content_type == "text/html":
try:
body = part.get_payload(decode=True).decode(part.get_content_charset() or "utf-8", errors="ignore")
break
except:
continue
else:
if msg.get_content_type() == "text/html":
body = msg.get_payload(decode=True).decode(msg.get_content_charset() or "utf-8", errors="ignore")
# Regex matches
matches = re.findall(Search_Regex, body)
for m in matches:
print(f" Match: {m}")
mail.logout() mail.logout()
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
# const variables # const variables
MORNING_ALARM="08:17" MORNING_ALARM="10:17"
[[ $1 = daemon ]] && cloudalarm_token=$(rsec WEAK10) [[ $1 = daemon ]] && cloudalarm_token=$(rsec WEAK10)
TMP_CTL_FILE=/tmp/.rwatchdog.cmd TMP_CTL_FILE=/tmp/.rwatchdog.cmd
TMP_INFO_FILE=/tmp/.rwatchdog.alarm-info TMP_INFO_FILE=/tmp/.rwatchdog.alarm-info
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment