diff --git a/tools/__init__.py b/tools/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tools/emails/data.py b/tools/emails/data.py new file mode 100644 index 0000000..32ff194 --- /dev/null +++ b/tools/emails/data.py @@ -0,0 +1,50 @@ +from mailjet_rest import Client + +from django.template.loader import render_to_string, get_template +from config import settings +from utils.exceptions import EmailException + + +class EmailSender: + def __init__(self, data): + self.client_id = settings.EMAIL_ID + self.client_secret = settings.EMAIL_SECRET + self.email_from = settings.EMAIL_FROM + self.data = data + + def authenticate_user(self): + return Client( + auth=(self.client_id, self.client_secret), + version='v3.1' + ) + + def prepare_email_template(self, template_name): + template = get_template(template_name) + return template.render(self.data) + + def prepare_email(self): + return { + 'Messages': [ + { + "From": { + "Email": "hubertjan98@gmail.com", + "Name": "Hubert" + }, + "To": [ + { + "Email": f"{self.data['email_to']}", + "Name": f"{self.data['owner_first_name']}" + } + ], + "Subject": f"{self.data['subject']}", + "HTMLPart": self.prepare_email_template('reset_password.html'), + } + ] + } + + def send_email(self): + response = self.authenticate_user().send.create(data=self.prepare_email()) + if response.status_code != 200: + raise EmailException() + else: + return response diff --git a/tools/emails/templates/reset_password.html b/tools/emails/templates/reset_password.html new file mode 100644 index 0000000..d5dfee2 --- /dev/null +++ b/tools/emails/templates/reset_password.html @@ -0,0 +1,117 @@ + + +
+ + + + + +
+
|
+