62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
"""Webapp utils."""
|
|
|
|
from django.utils import timezone
|
|
# from django.core.mail import send_mail
|
|
import os
|
|
from mailjet_rest import Client
|
|
|
|
from datetime import timedelta
|
|
import logging
|
|
from django.conf import settings
|
|
from django.template.loader import render_to_string
|
|
from sikweb.settings import URL, EMAIL_API_KEY, EMAIL_API_SECRET, DEFAULT_EMAIL_FROM, DEFAULT_EMAIL_FROM_ADDR
|
|
|
|
|
|
def month_from_now():
|
|
"""Return date one month from now."""
|
|
return timezone.now() + timedelta(days=30)
|
|
|
|
|
|
def send_email(to, subject, body, fail_silently=False):
|
|
try:
|
|
mailjet = Client(auth=(EMAIL_API_KEY, EMAIL_API_SECRET), version='v3.1')
|
|
|
|
data = {
|
|
'Messages': [
|
|
{
|
|
"From": {
|
|
"Email": DEFAULT_EMAIL_FROM_ADDR,
|
|
"Name": DEFAULT_EMAIL_FROM
|
|
},
|
|
"To": [
|
|
{
|
|
"Email": to,
|
|
"Name": "You"
|
|
}
|
|
],
|
|
"Subject": subject,
|
|
# "TextPart": "Greetings from Mailjet!",
|
|
"HTMLPart": body
|
|
}
|
|
]
|
|
}
|
|
success = mailjet.send.create(data=data)
|
|
|
|
if success.status_code != 201:
|
|
raise Exception(f'Failed to send email: {success.json()}')
|
|
|
|
except Exception as ex:
|
|
logging.exception('Failed to send email.')
|
|
logging.debug(EMAIL_API_KEY)
|
|
logging.debug(EMAIL_API_SECRET)
|
|
|
|
|
|
def send_signup_email(to, subject, id, uuid):
|
|
message = render_to_string(
|
|
'webapp:signup_email.html', {
|
|
'url': f"https://{URL}/api/signup/{id}/edit/?uuid={uuid}",
|
|
}
|
|
)
|
|
|
|
return send_email(to, subject, message, fail_silently=True)
|