Skip to content

Instantly share code, notes, and snippets.

@Aero-Blue
Last active March 23, 2020 17:59
Show Gist options
  • Save Aero-Blue/0379ec80128aafc54ec094fc065ce892 to your computer and use it in GitHub Desktop.
Save Aero-Blue/0379ec80128aafc54ec094fc065ce892 to your computer and use it in GitHub Desktop.
Class to preform a simple login to gmail via pop3
class Inbox:
@classmethod
def login(cls, username, password, smtp):
print(f"Connecting to {smtp}...")
server = poplib.POP3_SSL(smtp)
print(f"Logging in...")
server.user(username)
server.pass_(password)
print(f"Logged in as {username}!")
return server
def __init__(self, username, password, smtp):
self.server = self.login(username, password, smtp)
def get_message_body(raw_message):
return b"".join(
[
quopri.decodestring(part.get_payload(decode=True))
for part in raw_message.walk()
if part.get_payload(decode=True) is not None
]
)
def get_raw_messages(server):
message_ids = [
int(message_id.split(b" ")[0]) for message_id in server.list()[1]
]
raw_messages = [
b"\n".join(server.retr(message_id)[1]) for message_id in message_ids
]
return [
parser.BytesParser().parsebytes(raw_message) for raw_message in raw_messages
]
def html_search(body, element):
return body.select_one(element)
link = html_search(message_body,'a') # message_body -> from our get_message_body function
print(link.get('href')) # First link found in the email
def subject_search(messages, key):
found = []
for message in messages:
if key in message["Subject"]
found.append(message)
return found
def separate(message):
mailFrom = message["From"]
mailTo = message["To"]
subject = message["Subject"]
print(f"Message from {mailFrom} to {mailTo} titled: {subject}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment