Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keegan number 12 sulution #13

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 44 additions & 57 deletions email_blaster/cogs/check_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


import email
import socket
import logging
import imaplib

Expand Down Expand Up @@ -63,67 +62,55 @@ async def before_email(self):
self.email_password = self.bot.config['emailpassword']
self.email_server = self.bot.config['emailserver']

# login to mailserver.
self.login()

def login(self):
# Setup a connection to the imap mailserver
self.mail = imaplib.IMAP4_SSL(self.email_server)
self.mail.login(self.email_address, self.email_password)

def get_new_emails(self):
"""
Check for new unread emails and add them to a postlist to sort through.
"""

# Select mailbox
try:
self.mail.select('inbox')
except socket.error as e:
logging.error(f'Doing a socket reconnect, got error: {e}')
self.login()
self.mail.select('inbox')

logging.debug("Checking for new emails.")
# From here https://humberto.io/blog/sending-and-receiving-emails-with-python/
status, data = self.mail.search(None, "(UNSEEN)")

mail_ids = []

for block in data:
mail_ids += block.split()

# Fetch the mail using each ID
for i in mail_ids:
status, data = self.mail.fetch(i, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
message = email.message_from_bytes(response_part[1])
mail_from = message['from']
mail_subject = message['subject']
if message.is_multipart():
mail_content = ''

# on multipart we have the text message and
# another things like annex, and html version
# of the message, in that case we loop through
# the email payload
for part in message.get_payload():
# if the content type is text/plain
# we extract it
if part.get_content_type() == 'text/plain':
mail_content += part.get_payload()
else:
# if the message isn't multipart, just extract it
mail_content = message.get_payload()

# and then let's show its result
logging.info(f'From: {mail_from}')
logging.info(f'Subject: {mail_subject}')
logging.info(f'Content: {mail_content}')

return mail_content
return "" # Return empty string otherwise
with imaplib.IMAP4_SSL(self.email_server) as mail:

mail.select('inbox')

logging.debug("Checking for new emails.")
# From here https://humberto.io/blog/sending-and-receiving-emails-with-python/
status, data = mail.search(None, "(UNSEEN)")

mail_ids = []

for block in data:
mail_ids += block.split()

# Fetch the mail using each ID
for i in mail_ids:
status, data = mail.fetch(i, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
message = email.message_from_bytes(response_part[1])
mail_from = message['from']
mail_subject = message['subject']
if message.is_multipart():
mail_content = ''

# on multipart we have the text message and
# another things like annex, and html version
# of the message, in that case we loop through
# the email payload
for part in message.get_payload():
# if the content type is text/plain
# we extract it
if part.get_content_type() == 'text/plain':
mail_content += part.get_payload()
else:
# if the message isn't multipart, just extract it
mail_content = message.get_payload()

# and then let's show its result
logging.info(f'From: {mail_from}')
logging.info(f'Subject: {mail_subject}')
logging.info(f'Content: {mail_content}')

return mail_content
return "" # Return empty string otherwise

@check_email.error
async def exception_catching_callback(self, e):
Expand Down