Commit aa4ff70c authored by Benjamin Krause's avatar Benjamin Krause
Browse files

Adjust mail provider class

parent de0bb236
...@@ -2,6 +2,7 @@ import email ...@@ -2,6 +2,7 @@ import email
import imaplib import imaplib
import smtplib import smtplib
from email.message import EmailMessage from email.message import EmailMessage
from enum import Enum
from pydantic import BaseModel from pydantic import BaseModel
...@@ -10,17 +11,26 @@ from app.user import User ...@@ -10,17 +11,26 @@ from app.user import User
from app.utils import subject_identifier from app.utils import subject_identifier
class Encryption(Enum):
SSL_TLS = "ssl_tls"
STARTTLS = "starttls"
class MailProvider(BaseModel): class MailProvider(BaseModel):
name: str
base_url: str base_url: str
imap_url: str imap_url: str
imap_port: int = 993
imap_encryption: Encryption = Encryption.STARTTLS
smtp_url: str smtp_url: str
smtp_port: int smtp_port: int = 587
smtp_encryption: Encryption = Encryption.STARTTLS
def send_mail(self, from_user: User, to_user: User, message: EmailMessage): def send_mail(self, from_user: User, to_user: User, message: EmailMessage):
try: try:
with smtplib.SMTP(self.smtp_url, self.smtp_port) as server: with smtplib.SMTP(self.smtp_url, self.smtp_port) as server:
server.ehlo()
server.starttls() server.starttls()
server.ehlo()
server.login(from_user.mail, from_user.password) server.login(from_user.mail, from_user.password)
server.sendmail(from_user.mail, to_user.mail, message.as_string()) server.sendmail(from_user.mail, to_user.mail, message.as_string())
return True return True
...@@ -54,7 +64,9 @@ class MailProvider(BaseModel): ...@@ -54,7 +64,9 @@ class MailProvider(BaseModel):
print(e) print(e)
def imap_login(self, user: User) -> imaplib.IMAP4_SSL: def imap_login(self, user: User) -> imaplib.IMAP4_SSL:
imap = imaplib.IMAP4_SSL(self.imap_url) imap = imaplib.IMAP4_SSL(self.imap_url, self.smtp_port)
imap.starttls()
imap.ehlo()
imap.login(user.mail, user.password) imap.login(user.mail, user.password)
return imap return imap
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment