2020-12-20 14:49:43 +01:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
class EMail:
|
|
|
|
|
|
|
|
def __init__(self, header: list[str] = None, subject: str = None, body: str = None, transceiver: str = None, receiver: list[str] = None):
|
2021-03-14 16:08:52 +01:00
|
|
|
"""
|
|
|
|
Represents an email
|
|
|
|
:param header:
|
|
|
|
:param subject:
|
|
|
|
:param body:
|
|
|
|
:param transceiver:
|
|
|
|
:param receiver:
|
|
|
|
"""
|
2020-12-20 14:49:43 +01:00
|
|
|
self._header: list[str] = header
|
|
|
|
|
|
|
|
self._subject: str = subject
|
|
|
|
self._body: str = body
|
|
|
|
|
|
|
|
self._transceiver: str = transceiver
|
|
|
|
self._receiver: list[str] = receiver
|
|
|
|
|
|
|
|
@property
|
|
|
|
def header(self) -> str:
|
|
|
|
return '\r\n'.join(self._header)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def header_list(self) -> list[str]:
|
|
|
|
return self._header
|
|
|
|
|
|
|
|
@header.setter
|
|
|
|
def header(self, header: list[str]):
|
|
|
|
self._header = header
|
|
|
|
|
|
|
|
@property
|
|
|
|
def subject(self) -> str:
|
|
|
|
return self._subject
|
|
|
|
|
|
|
|
@subject.setter
|
|
|
|
def subject(self, subject: str):
|
|
|
|
self._subject = subject
|
|
|
|
|
|
|
|
@property
|
|
|
|
def body(self) -> str:
|
|
|
|
return self._body
|
|
|
|
|
|
|
|
@body.setter
|
|
|
|
def body(self, body: str):
|
|
|
|
self._body = body
|
|
|
|
|
|
|
|
@property
|
|
|
|
def transceiver(self) -> str:
|
|
|
|
return self._transceiver
|
|
|
|
|
|
|
|
@transceiver.setter
|
|
|
|
def transceiver(self, transceiver: str):
|
|
|
|
if self.check_mail(transceiver):
|
|
|
|
self._transceiver = transceiver
|
|
|
|
else:
|
|
|
|
raise Exception(f'Invalid email: {transceiver}')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def receiver(self) -> str:
|
|
|
|
return ','.join(self._receiver)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def receiver_list(self) -> list[str]:
|
|
|
|
return self._receiver
|
|
|
|
|
|
|
|
@receiver.setter
|
|
|
|
def receiver(self, receiver: list[str]):
|
|
|
|
self._receiver = receiver
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def check_mail(address: str) -> bool:
|
2021-03-14 16:08:52 +01:00
|
|
|
"""
|
|
|
|
Checks if an email is valid
|
|
|
|
:param address:
|
|
|
|
:return:
|
|
|
|
"""
|
2020-12-20 14:49:43 +01:00
|
|
|
return bool(re.search('^\\w+([.-]?\\w+)*@\\w+([.-]?\\w+)*(.\\w{2,3})+$', address))
|
|
|
|
|
|
|
|
def add_header(self, header: str):
|
2021-03-14 16:08:52 +01:00
|
|
|
"""
|
|
|
|
Adds header
|
|
|
|
:param header:
|
|
|
|
:return:
|
|
|
|
"""
|
2020-12-20 14:49:43 +01:00
|
|
|
if self._header is None:
|
|
|
|
self._header = []
|
|
|
|
|
|
|
|
self._header.append(header)
|
|
|
|
|
|
|
|
def add_receiver(self, receiver: str):
|
2021-03-14 16:08:52 +01:00
|
|
|
"""
|
|
|
|
Adds receiver
|
|
|
|
:param receiver:
|
|
|
|
:return:
|
|
|
|
"""
|
2020-12-20 14:49:43 +01:00
|
|
|
if self._receiver is None:
|
|
|
|
self._receiver = []
|
|
|
|
|
|
|
|
if self.check_mail(receiver):
|
|
|
|
self._receiver.append(receiver)
|
|
|
|
else:
|
|
|
|
raise Exception(f'Invalid email: {receiver}')
|
|
|
|
|
|
|
|
def get_content(self, transceiver: str):
|
2021-03-14 16:08:52 +01:00
|
|
|
"""
|
|
|
|
Returns the mail as string
|
|
|
|
:param transceiver:
|
|
|
|
:return:
|
|
|
|
"""
|
2020-12-20 14:49:43 +01:00
|
|
|
return str(f'From: {transceiver}\r\nTo: {self.receiver}\r\n{self.header}\r\nSubject: {self.subject}\r\n{self.body}').encode('utf-8')
|