2021.4.1 #11
@ -4,6 +4,14 @@ import re
|
||||
class EMail:
|
||||
|
||||
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._subject: str = subject
|
||||
@ -65,15 +73,30 @@ class EMail:
|
||||
|
||||
@staticmethod
|
||||
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))
|
||||
|
||||
def add_header(self, header: str):
|
||||
"""
|
||||
Adds header
|
||||
:param header:
|
||||
:return:
|
||||
"""
|
||||
if self._header is None:
|
||||
self._header = []
|
||||
|
||||
self._header.append(header)
|
||||
|
||||
def add_receiver(self, receiver: str):
|
||||
"""
|
||||
Adds receiver
|
||||
:param receiver:
|
||||
:return:
|
||||
"""
|
||||
if self._receiver is None:
|
||||
self._receiver = []
|
||||
|
||||
@ -83,4 +106,9 @@ class EMail:
|
||||
raise Exception(f'Invalid email: {receiver}')
|
||||
|
||||
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')
|
||||
|
@ -13,6 +13,12 @@ from cpl.utils.credential_manager import CredentialManager
|
||||
class EMailClient(EMailClientABC):
|
||||
|
||||
def __init__(self, environment: EnvironmentABC, logger: LoggerABC, mail_settings: EMailClientSettings):
|
||||
"""
|
||||
Service to send emails
|
||||
:param environment:
|
||||
:param logger:
|
||||
:param mail_settings:
|
||||
"""
|
||||
EMailClientABC.__init__(self)
|
||||
|
||||
self._environment = environment
|
||||
@ -24,11 +30,19 @@ class EMailClient(EMailClientABC):
|
||||
self.create()
|
||||
|
||||
def create(self):
|
||||
"""
|
||||
Creates connection
|
||||
:return:
|
||||
"""
|
||||
self._logger.trace(__name__, f'Started {__name__}.create')
|
||||
self.connect()
|
||||
self._logger.trace(__name__, f'Stopped {__name__}.create')
|
||||
|
||||
def connect(self):
|
||||
"""
|
||||
Connects to server
|
||||
:return:
|
||||
"""
|
||||
self._logger.trace(__name__, f'Started {__name__}.connect')
|
||||
try:
|
||||
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')
|
||||
|
||||
def login(self):
|
||||
"""
|
||||
Login to server
|
||||
:return:
|
||||
"""
|
||||
self._logger.trace(__name__, f'Started {__name__}.login')
|
||||
try:
|
||||
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')
|
||||
|
||||
def send_mail(self, email: EMail):
|
||||
"""
|
||||
Sends email
|
||||
:param email:
|
||||
:return:
|
||||
"""
|
||||
self._logger.trace(__name__, f'Started {__name__}.send_mail')
|
||||
try:
|
||||
self.login()
|
||||
|
Loading…
Reference in New Issue
Block a user