From 69a892c948b37eacb7f14f340897c3217468c7fa Mon Sep 17 00:00:00 2001 From: Hubert Jankowski Date: Wed, 8 Dec 2021 20:14:32 +0100 Subject: [PATCH] WIP --- tools/__init__.py | 0 tools/emails/data.py | 50 +++++++++ tools/emails/templates/reset_password.html | 117 +++++++++++++++++++++ tools/emails/templates/reset_password.txt | 1 + 4 files changed, 168 insertions(+) create mode 100644 tools/__init__.py create mode 100644 tools/emails/data.py create mode 100644 tools/emails/templates/reset_password.html create mode 100644 tools/emails/templates/reset_password.txt 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 @@ + + + + + + + + + template_reset_password + + + + + + +
+ + + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + + + + + + + + + + + + + + + + +

FORGOT YOUR

 PASSWORD?

HI, {{ owner_first_name }} {{ owner_last_name }}

There was a request to change your password!

If did not make this request, just ignore this email. Otherwise, please click the button below to change your password:

RESET PASSWORD
+
+ + diff --git a/tools/emails/templates/reset_password.txt b/tools/emails/templates/reset_password.txt new file mode 100644 index 0000000..ced40d6 --- /dev/null +++ b/tools/emails/templates/reset_password.txt @@ -0,0 +1 @@ +{{ variable }}