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 26, 2024
2 parents 5999ada + 5125d60 commit 0edd9a3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions EMailService.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from imapclient import IMAPClient
from smtplib import SMTP_SSL,SMTP_SSL_PORT
import email

from email.message import EmailMessage


class ProtocolTemplate(ABC):
Expand Down Expand Up @@ -77,18 +77,25 @@ def sendEmail(self, email:Email) -> bool:

#craft email
from_email = SMTP_USER
to_emails = to
body = email.body
headers = f"From: {from_email}\r\n"
headers += f"To: {', '.join(to_emails)}\r\n"
headers += f"Subject: {email.subject}\r\n"
email_message = headers + "\r\n" + body
to_emails = to
msg = EmailMessage()
msg['Subject'] = email.subject
msg['From'] = from_email
msg['To'] = to_emails
msg['Cc'] = cc
msg['Bcc'] = bcc
msg.set_content(email.body)

#attachment
with open(email.attachments[0].filename, "rb") as f:
file_data = f.read()
msg.add_attachment(file_data, maintype = "text", subtype = "plain", filename = "test.txt")

#connect/authenticate
smtp_server = SMTP_SSL(self.SMTP_HOST, port = SMTP_SSL_PORT)
smtp_server.set_debuglevel(1)
smtp_server.login(SMTP_USER, SMTP_PASS)
smtp_server.sendmail(from_email, to_emails, email_message)
smtp_server.send_message(msg)

#disconnect
smtp_server.quit()
Expand Down Expand Up @@ -164,7 +171,7 @@ def getEmails(self)->list[Email]:


from exchangelib import Credentials, Account, Message, FileAttachment
import os
import os

class ExchangeProtocol(ProtocolTemplate):

Expand Down Expand Up @@ -252,10 +259,14 @@ def getEmails(self)->list[Email]:
if not self.logged_in:
return None

attachments = []

result = []
for item in self.acc.inbox.all():
attachments = []
for attachment in item.attachments:
if isinstance(attachment, FileAttachment):
local_path = os.path.join('attachments', attachment.name)
with open(local_path, 'wb') as f:
f.write(attachment.content)
result += [create_email(
uid = item.message_id,
sender= item.sender,
Expand All @@ -274,14 +285,15 @@ def imap_test():
test = Email(

subject="Hello",
body="World",
recipients=[EmailReception(contact=(Contact(email_address ="praxisprojekt-remail@uni-due.de")), kind=RecipientKind.to)])
body="World i wanna go home",
recipients=[EmailReception(contact=(Contact(email_address ="praxisprojekt-remail@uni-due.de")), kind=RecipientKind.to)],
attachments=[Attachment(filename=r"C:\Users\toadb\Documents\ReinventingEmail\test.txt")])

print("IMAP Logged_in: ",imap.logged_in)
imap.login("thatchmilo35@gmail.com","mgtszvrhgkphxghm")
print("IMAP Logged_in: ",imap.logged_in)

#imap.sendEmail(test)
imap.sendEmail(test)
print("sent?")

listofmails = imap.getEmails()
Expand All @@ -295,18 +307,18 @@ def exchange_test():


#exchange
import keyring
#import keyring

test = Email(

subject="Betreff",
body="World",
recipients=[EmailReception(contact=(Contact(email_address ="thatchmilo35@gmail.com")), kind=RecipientKind.to)],
recipients=[EmailReception(contact=(Contact(email_address ="thatchmilo35@gmail.com")))],
attachments=[Attachment(filename="path")])


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.getEmails()
#exchange.sendEmail(test)
Expand Down
Binary file modified __pycache__/email2.cpython-312.pyc
Binary file not shown.

0 comments on commit 0edd9a3

Please sign in to comment.