-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Email Listener
- Loading branch information
Showing
11 changed files
with
839 additions
and
12 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Module for email Attachments.""" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"""Email Attachments.""" | ||
import email | ||
from email.message import EmailMessage | ||
from io import BytesIO | ||
|
||
|
||
class Attachments: | ||
"""Email Attachment Reader.""" | ||
|
||
manager = email.contentmanager.raw_data_manager | ||
|
||
def __init__(self, email_attachment: EmailMessage): | ||
"""Initializes Email Attachment.""" | ||
self.email_attachment = email_attachment | ||
|
||
@property | ||
def filename(self): | ||
"""Get filename.""" | ||
return self.email_attachment.get_filename() | ||
|
||
def save_file(self, path): | ||
"""Saves file to path provided.""" | ||
with open(path, "wb") as file: | ||
file.write(self.email_attachment.get_content(content_manager=self.manager)) | ||
|
||
def get_data(self) -> BytesIO: | ||
"""Get file data as an inmemory as a BytesIO file-like object.""" | ||
obj = BytesIO() | ||
obj.write(self.email_attachment.get_content(content_manager=self.manager)) | ||
obj.seek(0) | ||
return obj | ||
|
||
def __str__(self): | ||
"""Get string representation.""" | ||
return self.filename | ||
|
||
def __repr__(self): | ||
"""Get repr representation.""" | ||
return f"Attachments({self.__str__()})" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Module for reading received emails.""" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
"""Email Message Reader.""" | ||
import base64 | ||
import email | ||
from email.message import EmailMessage | ||
from email.policy import default | ||
|
||
from promail.core.attachements.attachments import Attachments | ||
|
||
|
||
class Message: | ||
"""Email Message reader.""" | ||
|
||
def __init__(self, msg: dict) -> None: | ||
"""Initalises Message object. | ||
Args: | ||
msg: email message data containing id | ||
""" | ||
self.msg = email.message_from_bytes( | ||
base64.urlsafe_b64decode(msg["raw"]), _class=EmailMessage, policy=default | ||
) | ||
self._attachments = None | ||
|
||
@property | ||
def html_body(self) -> str: | ||
"""Get HTML Body of email.""" | ||
return self.msg.get_body(preferencelist=["html"]) # type: ignore | ||
|
||
@property | ||
def plain_text(self): | ||
"""Get Plain text body of email.""" | ||
return self.msg.get_body(preferencelist=["plain"]) # type: ignore | ||
|
||
@property | ||
def sender(self) -> str: | ||
"""Get sender of email.""" | ||
return self.msg.get("from") | ||
|
||
@property | ||
def cc(self) -> str: | ||
"""Get emails cc'd.""" | ||
return self.msg.get("cc") | ||
|
||
@property | ||
def bcc(self) -> str: | ||
"""Get emails ccc'd.""" | ||
return self.msg.get("bcc") | ||
|
||
@property | ||
def message_id(self) -> str: | ||
"""Get Message ID of email.""" | ||
return self.msg.get("message-id") | ||
|
||
@property | ||
def to(self) -> str: | ||
"""Get to field of email.""" | ||
return self.msg.get("to") | ||
|
||
@property | ||
def subject(self) -> str: | ||
"""Get Subject of email.""" | ||
return self.msg.get("subject") | ||
|
||
@property | ||
def date(self): | ||
"""Get Date of Email.""" | ||
return self.msg.get("date") | ||
|
||
@property | ||
def attachments(self): | ||
"""Get Email Attachments.""" | ||
if self._attachments is None: | ||
self._attachments = {} | ||
for email_message_attachment in self.msg.iter_attachments(): | ||
print(type(email_message_attachment)) | ||
if email_message_attachment.is_attachment(): | ||
self._attachments[ | ||
email_message_attachment.get_filename() | ||
] = Attachments(email_message_attachment) | ||
return self._attachments | ||
|
||
def __str__(self) -> str: | ||
"""String representation.""" | ||
return self.subject |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Email Filters.""" |
Oops, something went wrong.