refactor datatables
This commit is contained in:
@@ -27,61 +27,95 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class EmailService:
|
||||
email = None
|
||||
body = None
|
||||
subject = None
|
||||
to = None
|
||||
from_email = None
|
||||
"""
|
||||
A reusable class for sending emails
|
||||
|
||||
content_subtype = "html"
|
||||
Example:
|
||||
```python
|
||||
email_service = EmailService(
|
||||
subject="Hello",
|
||||
to="recipient@example.com",
|
||||
from_email="sender@example.com",
|
||||
)
|
||||
email_service.set_html_body("<p>Hello, this is a test email!</p>")
|
||||
email_service.send()
|
||||
```
|
||||
|
||||
This class provides methods to set the subject, recipient(s), sender, and body of the email.
|
||||
It also supports loading email content from templates and attaching files.
|
||||
"""
|
||||
def __init__(self, subject=None, to=None, from_email=None):
|
||||
self.subject = subject
|
||||
self.to = (to,)
|
||||
self.to = to if isinstance(to, (list, tuple)) else [to]
|
||||
self.from_email = from_email
|
||||
|
||||
def set_to(self, to):
|
||||
self.to = to
|
||||
self.body = None
|
||||
self.content_subtype = "html"
|
||||
|
||||
def set_subject(self, subject):
|
||||
self.subject = subject
|
||||
|
||||
def set_to(self, to):
|
||||
"""
|
||||
Set the recipient email address or addresses.
|
||||
|
||||
Parameters:
|
||||
- to (str or list): The recipient email address or a list of recipient email addresses.
|
||||
"""
|
||||
self.to = to if isinstance(to, (list, tuple)) else [to]
|
||||
|
||||
def set_from_email(self, from_email):
|
||||
self.from_email = from_email
|
||||
|
||||
def set_text_body(self, body):
|
||||
"""Set the plain text body of the email."""
|
||||
self.body = strip_tags(body)
|
||||
|
||||
def set_html_body(self, html_body):
|
||||
"""Set the HTML body of the email."""
|
||||
self.body = html_body
|
||||
|
||||
def load_template(self, path=None, context={}):
|
||||
"""
|
||||
Load an email template from a file and render it with the provided context.
|
||||
|
||||
Parameters:
|
||||
- path (str): The path to the email template file.
|
||||
- context (dict): The context data to render the template.
|
||||
"""
|
||||
if path is None:
|
||||
raise Exception("Email temaplate path is not provided.")
|
||||
raise Exception("Email template path is not provided.")
|
||||
|
||||
self.content_subtype = "html"
|
||||
html_body = render_to_string(path, context=context)
|
||||
self.body = html_body
|
||||
self.body = render_to_string(path, context=context)
|
||||
|
||||
def attach(self, file_path):
|
||||
self.email.attach_file(file_path)
|
||||
"""
|
||||
Attach a file to the email.
|
||||
|
||||
Parameters:
|
||||
- file_path (str): The path to the file to be attached.
|
||||
"""
|
||||
if not hasattr(self, 'attachments'):
|
||||
self.attachments = []
|
||||
self.attachments.append(file_path)
|
||||
|
||||
def send(self):
|
||||
try:
|
||||
self.email = EmailMessage(
|
||||
email = EmailMessage(
|
||||
subject=self.subject,
|
||||
body=self.body,
|
||||
to=self.to,
|
||||
from_email=self.from_email,
|
||||
)
|
||||
email.content_subtype = self.content_subtype
|
||||
|
||||
self.email.content_subtype = self.content_subtype
|
||||
|
||||
self.email.send()
|
||||
except SMTPException as e:
|
||||
logger.error(str(e))
|
||||
|
||||
if hasattr(self, 'attachments'):
|
||||
for attachment in self.attachments:
|
||||
email.attach_file(attachment)
|
||||
|
||||
email.send()
|
||||
except Exception as e:
|
||||
logger.error(f"Error sending email: {str(e)}")
|
||||
class SMSError(Exception):
|
||||
def __init__(self, message, payload=None):
|
||||
self.message = message
|
||||
|
||||
Reference in New Issue
Block a user