2021-12-08 20:14:32 +01:00
|
|
|
from mailjet_rest import Client
|
|
|
|
|
|
|
|
from django.template.loader import render_to_string, get_template
|
|
|
|
from config import settings
|
2021-12-11 22:01:14 +01:00
|
|
|
from tools.tools import EmailException
|
2021-12-08 20:14:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
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']}",
|
2021-12-11 22:01:14 +01:00
|
|
|
"Name": f"{self.data['user_first_name']}"
|
2021-12-08 20:14:32 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"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())
|
2021-12-11 22:01:14 +01:00
|
|
|
# import pdb;pdb.set_trace()
|
2021-12-08 20:14:32 +01:00
|
|
|
if response.status_code != 200:
|
|
|
|
raise EmailException()
|
|
|
|
else:
|
|
|
|
return response
|