Skip to content

Commit

Permalink
Merge branch 'EmailInfra' of https://github.com/koesterlab/remail int…
Browse files Browse the repository at this point in the history
…o EmailInfra
  • Loading branch information
SpartasWar committed Nov 29, 2024
2 parents 03c48c4 + 717298a commit 5981b6d
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions EMailService.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ def send_email(self, email:Email) -> bool:
msg['From'] = from_email
msg['To'] = to
msg['Cc'] = cc
#not working
#msg['Bcc'] = bcc
msg['Bcc'] = ",".join(bcc)
msg.set_content(email.body)

#attachment
Expand Down Expand Up @@ -277,7 +276,6 @@ def get_emails(self, date : datetime = None)->list[Email]:
result += self.get_email_exchange(item)
else:
start_date = EWSDateTime.from_datetime(date).astimezone(UTC)
end_date = EWSDateTime.now().astimezone(UTC)
for item in self.acc.inbox.filter(datetime_received__gte = start_date):
result += self.get_email_exchange(item)
return result
Expand All @@ -289,6 +287,10 @@ def get_email_exchange(self, item):
local_path = os.path.join('attachments', attachment.name)
with open(local_path, 'wb') as f:
f.write(attachment.content)

ews_datetime_str = item.datetime_received.astimezone()
parsed_datetime = datetime.fromisoformat(ews_datetime_str.ewsformat())

return [create_email(
uid = item.message_id,
sender= item.sender,
Expand All @@ -297,17 +299,19 @@ def get_email_exchange(self, item):
attachments=attachments,
to_recipients=item.to_recipients,
cc_recipients=[],
bcc_recipients=[])]
bcc_recipients=[],
date = parsed_datetime
)]

#-------------------------------------------------

def imap_test():
imap = ImapProtocol()
test = Email(

subject="Hellololololo",
subject="TestBCC",
body="Test time!!",
recipients=[EmailReception(contact=(Contact(email_address ="praxisprojekt-remail@uni-due.de")), kind=RecipientKind.to),EmailReception(contact=(Contact(email_address ="toadbella@gmail.com")), kind=RecipientKind.to)],
recipients=[EmailReception(contact=(Contact(email_address ="praxisprojekt-remail@uni-due.de")), kind=RecipientKind.to),EmailReception(contact=(Contact(email_address ="toadbella@gmail.com")))],
#attachments=[Attachment(filename=r"C:\Users\toadb\Documents\ReinventingEmail\test.txt")])
)
print("IMAP Logged_in: ",imap.logged_in)
Expand All @@ -331,7 +335,7 @@ def exchange_test():


#exchange
import keyring
#import keyring

test = Email(

Expand All @@ -342,15 +346,26 @@ def exchange_test():


print("Exchange Logged_in: ",exchange.logged_in)
exchange.login("praxisprojekt-remail@uni-due.de",keyring.get_password("remail/exchange","praxisprojekt-remail@uni-due.de"))
#exchange.login("praxisprojekt-remail@uni-due.de",keyring.get_password("remail/exchange","praxisprojekt-remail@uni-due.de"))
print("Exchange Logged_in: ",exchange.logged_in)
emails = exchange.get_emails(datetime(2024,11,29,9,28,tzinfo=UTC))
emails = exchange.get_emails(datetime(2024,11,29,9,29))
print(emails)
#exchange.send_email(test)
exchange.logout()
print("Exchange Logged_in: ",exchange.logged_in)

def create_email(uid : str,sender : str, subject: str, body: str, attachments: list[str], to_recipients: list[str],cc_recipients: list[str],bcc_recipients: list[str], html_files: list[str] = None ) -> Email:
def create_email(
uid : str,
sender : str,
subject: str,
body: str,
attachments: list[str],
to_recipients: list[str],
cc_recipients: list[str],
bcc_recipients: list[str],
date: datetime,
html_files: list[str] = None
) -> Email:

sender_contact = get_contact(sender)

Expand All @@ -368,7 +383,8 @@ def create_email(uid : str,sender : str, subject: str, body: str, attachments: l
subject=subject,
body=body,
attachments=attachments_class,
recipients=recipients
recipients=recipients,
date=date
)

def get_contact(email : str) -> Contact:
Expand Down

0 comments on commit 5981b6d

Please sign in to comment.