aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md4
-rwxr-xr-xdaily-update.py14
-rw-r--r--email_helper.py71
3 files changed, 86 insertions, 3 deletions
diff --git a/README.md b/README.md
index f826676..7cd62f0 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
Some misc scripts I use regarding email
## email\_helper.py
-General purpose email reading/sending
+General purpose email reading/sending (though tailored to these purposes)
## kitchen-update-email.py
Check if there is a new post on my website and email those signed up for email notifications
@@ -11,5 +11,5 @@ Check if there is a new post on my website and email those signed up for email n
Send an email based on args with the subject staring "REMINDER: ". This makes it easy to set up monthly/weekly/etc. reminders via crontab
## daily-update.py
-Sends an update with news and weather about the day
+Sends an update with news and weather about the day, and any emails left over send from `reminder.py`
diff --git a/daily-update.py b/daily-update.py
index 7109b99..aa30efe 100755
--- a/daily-update.py
+++ b/daily-update.py
@@ -33,9 +33,21 @@ def get_old_news():
news_text += '<a href="%s">%s %s</a>\n' % (full, name, century_ago)
return news_text
+def get_unread_reminders():
+ print("getting unread reminders")
+ subjects = email_helper.filter_unread("subject", "REMINDER:", "subject")
+ subjects = [s[len("REMINDER: "):].strip() for s in subjects]
+ if len(subjects) > 0:
+ reminder_html = "<h1>Reminders:</h1><ul>"
+ for s in subjects:
+ reminder_html += "<li>%s</li>" % s
+ reminder_html += "</ul>\n"
+ return reminder_html
+ return ""
+
def format_email():
print("forming email")
- return '%s%s' % (get_old_news(), get_weather())
+ return '%s%s%s' % (get_unread_reminders(), get_old_news(), get_weather())
def send_update_email():
frm = config["email"]["user"]
diff --git a/email_helper.py b/email_helper.py
index d3edfb8..84440ea 100644
--- a/email_helper.py
+++ b/email_helper.py
@@ -1,6 +1,9 @@
import smtplib
+import imaplib
+import email
from email.mime.text import MIMEText
from email.utils import formatdate
+from email.header import decode_header
from config import config
@@ -26,3 +29,71 @@ def send(frm, name, to, subject, body, subtype="plain"):
print("error")
print(e)
+def get_subject(msg):
+ subject = decode_header(msg["Subject"])[0][0]
+ if isinstance(subject, bytes):
+ subject = subject.decode()
+ return subject
+
+def get_body(msg):
+ if msg.is_multipart():
+ body = ""
+ for part in msg.walk():
+ try:
+ # get the email body
+ body += part.get_payload(decode=True).decode()
+ except:
+ pass
+ else:
+ body = msg.get_payload(decode=True).decode()
+ return body
+
+def get_from(msg):
+ return msg.get("From")
+
+def get_content_type(msg):
+ return msg.get_content_type()
+
+def set_unseen(imap, idx):
+ imap.store(idx, '-FLAGS', '\\SEEN')
+
+def get_what(msg, what):
+ if what == "subject":
+ return get_subject(msg)
+ elif what == "body":
+ return get_body(msg)
+ elif what == "from":
+ return get_from(msg)
+ elif what == "content_type":
+ return get_content_type
+
+def parse(imap, index, check_what, criteria, return_what):
+ res, msg = imap.fetch(index, "(RFC822)")
+ for response in msg:
+ if isinstance(response, tuple):
+ msg = email.message_from_bytes(response[1])
+ if criteria in get_what(msg, check_what):
+ return get_what(msg, return_what)
+
+def filter_unread(check_what, criteria, return_what):
+ """ check if the field 'check_what' contains the given criteria, and if so return a list
+ of the field 'return_what'
+ """
+ imap = imaplib.IMAP4_SSL(config["email"]["server"])
+ imap.login(config["email"]["user"], config["email"]["pass"])
+ status, messages = imap.select("INBOX")
+
+ status, response = imap.search(None, '(UNSEEN)')
+ unread_msg_nums = response[0].split()
+
+ ret = []
+ for i in unread_msg_nums:
+ parse_return = parse(imap, i, check_what, criteria, return_what)
+ if parse_return is not None:
+ ret.append(parse_return)
+ set_unseen(imap, i)
+ imap.close()
+ imap.logout()
+
+ return ret
+