Last active
March 23, 2020 17:59
-
-
Save Aero-Blue/0379ec80128aafc54ec094fc065ce892 to your computer and use it in GitHub Desktop.
Class to preform a simple login to gmail via pop3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
] | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def subject_search(messages, key): | |
found = [] | |
for message in messages: | |
if key in message["Subject"] | |
found.append(message) | |
return found |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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