2021.4.1 #11

Merged
edraft merged 172 commits from 2021.04.01 into 2021.04 2021-03-21 20:04:24 +01:00
2 changed files with 51 additions and 0 deletions
Showing only changes of commit 40fb4fb3f9 - Show all commits

View File

@ -4,6 +4,14 @@ import re
class EMail: class EMail:
def __init__(self, header: list[str] = None, subject: str = None, body: str = None, transceiver: str = None, receiver: list[str] = None): def __init__(self, header: list[str] = None, subject: str = None, body: str = None, transceiver: str = None, receiver: list[str] = None):
"""
Represents an email
:param header:
:param subject:
:param body:
:param transceiver:
:param receiver:
"""
self._header: list[str] = header self._header: list[str] = header
self._subject: str = subject self._subject: str = subject
@ -65,15 +73,30 @@ class EMail:
@staticmethod @staticmethod
def check_mail(address: str) -> bool: def check_mail(address: str) -> bool:
"""
Checks if an email is valid
:param address:
:return:
"""
return bool(re.search('^\\w+([.-]?\\w+)*@\\w+([.-]?\\w+)*(.\\w{2,3})+$', address)) return bool(re.search('^\\w+([.-]?\\w+)*@\\w+([.-]?\\w+)*(.\\w{2,3})+$', address))
def add_header(self, header: str): def add_header(self, header: str):
"""
Adds header
:param header:
:return:
"""
if self._header is None: if self._header is None:
self._header = [] self._header = []
self._header.append(header) self._header.append(header)
def add_receiver(self, receiver: str): def add_receiver(self, receiver: str):
"""
Adds receiver
:param receiver:
:return:
"""
if self._receiver is None: if self._receiver is None:
self._receiver = [] self._receiver = []
@ -83,4 +106,9 @@ class EMail:
raise Exception(f'Invalid email: {receiver}') raise Exception(f'Invalid email: {receiver}')
def get_content(self, transceiver: str): def get_content(self, transceiver: str):
"""
Returns the mail as string
:param transceiver:
:return:
"""
return str(f'From: {transceiver}\r\nTo: {self.receiver}\r\n{self.header}\r\nSubject: {self.subject}\r\n{self.body}').encode('utf-8') return str(f'From: {transceiver}\r\nTo: {self.receiver}\r\n{self.header}\r\nSubject: {self.subject}\r\n{self.body}').encode('utf-8')

View File

@ -13,6 +13,12 @@ from cpl.utils.credential_manager import CredentialManager
class EMailClient(EMailClientABC): class EMailClient(EMailClientABC):
def __init__(self, environment: EnvironmentABC, logger: LoggerABC, mail_settings: EMailClientSettings): def __init__(self, environment: EnvironmentABC, logger: LoggerABC, mail_settings: EMailClientSettings):
"""
Service to send emails
:param environment:
:param logger:
:param mail_settings:
"""
EMailClientABC.__init__(self) EMailClientABC.__init__(self)
self._environment = environment self._environment = environment
@ -24,11 +30,19 @@ class EMailClient(EMailClientABC):
self.create() self.create()
def create(self): def create(self):
"""
Creates connection
:return:
"""
self._logger.trace(__name__, f'Started {__name__}.create') self._logger.trace(__name__, f'Started {__name__}.create')
self.connect() self.connect()
self._logger.trace(__name__, f'Stopped {__name__}.create') self._logger.trace(__name__, f'Stopped {__name__}.create')
def connect(self): def connect(self):
"""
Connects to server
:return:
"""
self._logger.trace(__name__, f'Started {__name__}.connect') self._logger.trace(__name__, f'Started {__name__}.connect')
try: try:
self._logger.debug(__name__, f'Try to connect to {self._mail_settings.host}:{self._mail_settings.port}') self._logger.debug(__name__, f'Try to connect to {self._mail_settings.host}:{self._mail_settings.port}')
@ -44,6 +58,10 @@ class EMailClient(EMailClientABC):
self._logger.trace(__name__, f'Stopped {__name__}.connect') self._logger.trace(__name__, f'Stopped {__name__}.connect')
def login(self): def login(self):
"""
Login to server
:return:
"""
self._logger.trace(__name__, f'Started {__name__}.login') self._logger.trace(__name__, f'Started {__name__}.login')
try: try:
self._logger.debug(__name__, f'Try to login {self._mail_settings.user_name}@{self._mail_settings.host}:{self._mail_settings.port}') self._logger.debug(__name__, f'Try to login {self._mail_settings.user_name}@{self._mail_settings.host}:{self._mail_settings.port}')
@ -55,6 +73,11 @@ class EMailClient(EMailClientABC):
self._logger.trace(__name__, f'Stopped {__name__}.login') self._logger.trace(__name__, f'Stopped {__name__}.login')
def send_mail(self, email: EMail): def send_mail(self, email: EMail):
"""
Sends email
:param email:
:return:
"""
self._logger.trace(__name__, f'Started {__name__}.send_mail') self._logger.trace(__name__, f'Started {__name__}.send_mail')
try: try:
self.login() self.login()