replace mailjet with sendgrid
This commit is contained in:
+20
-35
@@ -1,18 +1,16 @@
|
||||
"""Webapp utils."""
|
||||
|
||||
from django.utils import timezone
|
||||
# from django.core.mail import send_mail
|
||||
import os
|
||||
from mailjet_rest import Client
|
||||
import sendgrid
|
||||
from sendgrid.helpers.mail import *
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from django.conf import settings
|
||||
from django.template.loader import render_to_string
|
||||
from django.core.files.base import ContentFile
|
||||
import base64
|
||||
import uuid
|
||||
from sikweb.settings import FRONTEND_URL, URL, EMAIL_API_KEY, EMAIL_API_SECRET, DEFAULT_EMAIL_FROM, DEFAULT_EMAIL_FROM_ADDR, ENABLE_AUTOMATIC_EMAILS
|
||||
from sikweb.settings import FRONTEND_URL, EMAIL_API_KEY, DEFAULT_EMAIL_FROM_ADDR, ENABLE_AUTOMATIC_EMAILS
|
||||
import imghdr
|
||||
import markdown
|
||||
|
||||
@@ -60,37 +58,24 @@ def send_email(to, subject, body, html=False):
|
||||
logging.debug(f"subject: {subject}")
|
||||
logging.debug(f"body: {body}")
|
||||
return
|
||||
|
||||
from_email = Email(DEFAULT_EMAIL_FROM_ADDR)
|
||||
to_email = To(to)
|
||||
sub = Subject(subject)
|
||||
|
||||
if (html):
|
||||
content = HtmlContent(body)
|
||||
else:
|
||||
content = PlainTextContent(body)
|
||||
|
||||
mail = Mail(from_email, to_email, sub, content)
|
||||
|
||||
try:
|
||||
mailjet = Client(auth=(EMAIL_API_KEY, EMAIL_API_SECRET), version='v3.1')
|
||||
sg = sendgrid.SendGridAPIClient(EMAIL_API_KEY)
|
||||
response = sg.client.mail.send.post(request_body=mail.get())
|
||||
|
||||
data = {
|
||||
'Messages': [
|
||||
{
|
||||
"From": {
|
||||
"Email": DEFAULT_EMAIL_FROM_ADDR,
|
||||
"Name": DEFAULT_EMAIL_FROM
|
||||
},
|
||||
"To": [
|
||||
{
|
||||
"Email": to,
|
||||
"Name": "You"
|
||||
}
|
||||
],
|
||||
"Subject": subject
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
if (html):
|
||||
data["Messages"][0]["HTMLPart"] = body
|
||||
else:
|
||||
data["Messages"][0]["TextPart"] = body
|
||||
|
||||
success = mailjet.send.create(data=data)
|
||||
|
||||
# For some reason returns 200 OK instead of 201 Created...
|
||||
if success.status_code != 200:
|
||||
raise Exception(f'Failed to send email: {success.json()}')
|
||||
if response.status_code != 202:
|
||||
raise Exception(f'Failed to send email: {response.body}')
|
||||
|
||||
except Exception as ex:
|
||||
logging.exception('Failed to send email.')
|
||||
@@ -109,4 +94,4 @@ def send_signup_email(to, subject, id, uuid, content):
|
||||
|
||||
def admin_send_email_signupees(list, subject, content):
|
||||
for to in list:
|
||||
send_email(to, subject, markdown.markdown(content), True)
|
||||
send_email(to.email, subject, markdown.markdown(content), True)
|
||||
|
||||
Reference in New Issue
Block a user