Skip to content

Commit

Permalink
Merge pull request #290 from ropable/master
Browse files Browse the repository at this point in the history
Bugfix email_utils.py: use raw strings (invalid escape sequences).
  • Loading branch information
ropable authored Sep 5, 2024
2 parents 37155d7 + 83d1df8 commit c7eb7aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
2 changes: 0 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ updates:
interval: "weekly"
target-branch: "master"
- package-ecosystem: "github-actions"
# Workflow files stored in the
# default location of `.github/workflows`
directory: "/"
schedule:
interval: "weekly"
Expand Down
36 changes: 15 additions & 21 deletions tracking/email_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
from imaplib import IMAP4_SSL


def get_imap(mailbox='INBOX'):
"""Instantiate a new IMAP object, login, and connect to a mailbox.
"""
def get_imap(mailbox="INBOX"):
"""Instantiate a new IMAP object, login, and connect to a mailbox."""
imap = IMAP4_SSL(settings.EMAIL_HOST)
imap.login(settings.EMAIL_USER, settings.EMAIL_PASSWORD)
imap.select(mailbox)
return imap


def email_get_unread(imap, from_email_address):
"""Returns (status, list of UIDs) of unread emails from a sending email address.
"""
"""Returns (status, list of UIDs) of unread emails from a sending email address."""
search = '(UNSEEN UNFLAGGED FROM "{}")'.format(from_email_address)
status, response = imap.search(None, search)
if status != 'OK':
if status != "OK":
return status, response
# Return status and list of unread email UIDs.
return status, response[0].split()
Expand All @@ -28,44 +26,40 @@ def email_fetch(imap, uid):
Email is returned as an email.Message class object.
"""
message = None
status, response = imap.fetch(str(uid), '(BODY.PEEK[])')
status, response = imap.fetch(str(uid), "(BODY.PEEK[])")

if status != 'OK':
if status != "OK":
return status, response

for i in response:
if isinstance(i, tuple):
s = i[1]
if isinstance(s, bytes):
s = s.decode('utf-8')
s = s.decode("utf-8")
message = email.message_from_string(s)

return status, message


def email_mark_read(imap, uid):
"""Flag an email as 'Seen' based on passed-in UID.
"""
status, response = imap.store(str(uid), '+FLAGS', '\Seen')
"""Flag an email as 'Seen' based on passed-in UID."""
status, response = imap.store(str(uid), "+FLAGS", r"\Seen")
return status, response


def email_mark_unread(imap, uid):
"""Remove the 'Seen' flag from an email based on passed-in UID.
"""
status, response = imap.store(str(uid), '-FLAGS', '\Seen')
"""Remove the 'Seen' flag from an email based on passed-in UID."""
status, response = imap.store(str(uid), "-FLAGS", r"\Seen")
return status, response


def email_delete(imap, uid):
"""Flag an email for deletion.
"""
status, response = imap.store(str(uid), '+FLAGS', '\Deleted')
"""Flag an email for deletion."""
status, response = imap.store(str(uid), "+FLAGS", r"\Deleted")
return status, response


def email_flag(imap, uid):
"""Flag an email as unprocessable.
"""
status, response = imap.store(str(uid), '+FLAGS', '\Flagged')
"""Flag an email as unprocessable."""
status, response = imap.store(str(uid), "+FLAGS", r"\Flagged")
return status, response

0 comments on commit c7eb7aa

Please sign in to comment.