replace mailjet with sendgrid
This commit is contained in:
@@ -2,9 +2,8 @@ HOST=api.dev.sahkoinsinoorikilta.fi
|
||||
DEBUG=True
|
||||
SECRET_KEY=7p$85^4ibb^p4-=vs44b7!y0e-zemugze18@a#30&71=a8)dp(
|
||||
TG_BOT_TOKEN=
|
||||
EMAIL_HOST=
|
||||
EMAIL_PASSWD=
|
||||
DB_USER=postgres
|
||||
DB_PASSWD=postgres
|
||||
DB_HOST=db
|
||||
DB_PORT=5432
|
||||
EMAIL_API_KEY=
|
||||
|
||||
+1
-2
@@ -2,9 +2,8 @@ HOST=localhost
|
||||
DEBUG=True
|
||||
SECRET_KEY=7p$85^4ibb^p4-=vs44b7!y0e-zemugze18@a#30&71=a8)dp(
|
||||
TG_BOT_TOKEN=
|
||||
EMAIL_HOST=
|
||||
EMAIL_PASSWD=
|
||||
DB_USER=postgres
|
||||
DB_PASSWD=postgres
|
||||
DB_HOST=db
|
||||
DB_PORT=5432
|
||||
EMAIL_API_KEY=
|
||||
|
||||
Generated
+39
-16
@@ -352,17 +352,6 @@ category = "main"
|
||||
optional = false
|
||||
python-versions = "*"
|
||||
|
||||
[[package]]
|
||||
name = "mailjet-rest"
|
||||
version = "1.3.4"
|
||||
description = "Mailjet V3 API wrapper"
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = "*"
|
||||
|
||||
[package.dependencies]
|
||||
requests = ">=2.4.3"
|
||||
|
||||
[[package]]
|
||||
name = "markdown"
|
||||
version = "3.3.4"
|
||||
@@ -534,6 +523,14 @@ python-versions = "*"
|
||||
[package.dependencies]
|
||||
six = ">=1.5"
|
||||
|
||||
[[package]]
|
||||
name = "python-http-client"
|
||||
version = "3.3.2"
|
||||
description = "HTTP REST client, simplified for Python"
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
|
||||
|
||||
[[package]]
|
||||
name = "pytz"
|
||||
version = "2021.1"
|
||||
@@ -582,6 +579,18 @@ dparse = ">=0.5.1"
|
||||
packaging = "*"
|
||||
requests = "*"
|
||||
|
||||
[[package]]
|
||||
name = "sendgrid"
|
||||
version = "6.7.0"
|
||||
description = "Twilio SendGrid library for Python"
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
|
||||
|
||||
[package.dependencies]
|
||||
python-http-client = ">=3.2.1"
|
||||
starkbank-ecdsa = ">=1.0.0"
|
||||
|
||||
[[package]]
|
||||
name = "six"
|
||||
version = "1.15.0"
|
||||
@@ -598,6 +607,14 @@ category = "main"
|
||||
optional = false
|
||||
python-versions = ">=3.5"
|
||||
|
||||
[[package]]
|
||||
name = "starkbank-ecdsa"
|
||||
version = "1.1.0"
|
||||
description = "A lightweight and fast pure python ECDSA library"
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = "*"
|
||||
|
||||
[[package]]
|
||||
name = "tablib"
|
||||
version = "3.0.0"
|
||||
@@ -667,7 +684,7 @@ brotli = ["brotli"]
|
||||
[metadata]
|
||||
lock-version = "1.1"
|
||||
python-versions = "^3.9"
|
||||
content-hash = "c491f605092858575be6858fbed2a379b9e1f93c75920ac3ef2e62ed2ad778fb"
|
||||
content-hash = "b14550d0e5199979586a276f3795f0161986e2ea9beeb8d585fd4d4aaac1cb64"
|
||||
|
||||
[metadata.files]
|
||||
attrs = [
|
||||
@@ -841,10 +858,6 @@ lml = [
|
||||
{file = "lml-0.1.0-py2.py3-none-any.whl", hash = "sha256:ec06e850019942a485639c8c2a26bdb99eae24505bee7492b649df98a0bed101"},
|
||||
{file = "lml-0.1.0.tar.gz", hash = "sha256:57a085a29bb7991d70d41c6c3144c560a8e35b4c1030ffb36d85fa058773bcc5"},
|
||||
]
|
||||
mailjet-rest = [
|
||||
{file = "mailjet_rest-1.3.4-py3-none-any.whl", hash = "sha256:635d53ac3fd61020f309c24ee977ae3458654ab39f9c36fc4b50c74e5d8ad410"},
|
||||
{file = "mailjet_rest-1.3.4.tar.gz", hash = "sha256:e02663fa0369543bcd48c37a146e8143bb12b9f3512af2d5ba6dfbcc99e64a2d"},
|
||||
]
|
||||
markdown = [
|
||||
{file = "Markdown-3.3.4-py3-none-any.whl", hash = "sha256:96c3ba1261de2f7547b46a00ea8463832c921d3f9d6aba3f255a6f71386db20c"},
|
||||
{file = "Markdown-3.3.4.tar.gz", hash = "sha256:31b5b491868dcc87d6c24b7e3d19a0d730d59d3e46f4eea6430a321bed387a49"},
|
||||
@@ -972,6 +985,9 @@ python-dateutil = [
|
||||
{file = "python-dateutil-2.6.0.zip", hash = "sha256:3acbef017340600e9ff8f2994d8f7afd6eacb295383f286466a6df3961e486f0"},
|
||||
{file = "python_dateutil-2.6.0-py2.py3-none-any.whl", hash = "sha256:537bf2a8f8ce6f6862ad705cd68f9e405c0b5db014aa40fa29eab4335d4b1716"},
|
||||
]
|
||||
python-http-client = [
|
||||
{file = "python_http_client-3.3.2.tar.gz", hash = "sha256:67e6a7bea19b03e14dc971480d3531b80becfc203d6c69478561bf7844d52661"},
|
||||
]
|
||||
pytz = [
|
||||
{file = "pytz-2021.1-py2.py3-none-any.whl", hash = "sha256:eb10ce3e7736052ed3623d49975ce333bcd712c7bb19a58b9e2089d4057d0798"},
|
||||
{file = "pytz-2021.1.tar.gz", hash = "sha256:83a4a90894bf38e243cf052c8b58f381bfe9a7a483f6a9cab140bc7f702ac4da"},
|
||||
@@ -1015,6 +1031,10 @@ safety = [
|
||||
{file = "safety-1.10.3-py2.py3-none-any.whl", hash = "sha256:5f802ad5df5614f9622d8d71fedec2757099705c2356f862847c58c6dfe13e84"},
|
||||
{file = "safety-1.10.3.tar.gz", hash = "sha256:30e394d02a20ac49b7f65292d19d38fa927a8f9582cdfd3ad1adbbc66c641ad5"},
|
||||
]
|
||||
sendgrid = [
|
||||
{file = "sendgrid-6.7.0-py3-none-any.whl", hash = "sha256:273bdc0abec649bf6319df7b6267980f79e53ab64e92906d65eea6d4330d00b4"},
|
||||
{file = "sendgrid-6.7.0.tar.gz", hash = "sha256:74b0dcf9a79188948f61f456bd1bf67ffa676a5d388aba1c76bff516566d7084"},
|
||||
]
|
||||
six = [
|
||||
{file = "six-1.15.0-py2.py3-none-any.whl", hash = "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced"},
|
||||
{file = "six-1.15.0.tar.gz", hash = "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259"},
|
||||
@@ -1023,6 +1043,9 @@ sqlparse = [
|
||||
{file = "sqlparse-0.4.1-py3-none-any.whl", hash = "sha256:017cde379adbd6a1f15a61873f43e8274179378e95ef3fede90b5aa64d304ed0"},
|
||||
{file = "sqlparse-0.4.1.tar.gz", hash = "sha256:0f91fd2e829c44362cbcfab3e9ae12e22badaa8a29ad5ff599f9ec109f0454e8"},
|
||||
]
|
||||
starkbank-ecdsa = [
|
||||
{file = "starkbank-ecdsa-1.1.0.tar.gz", hash = "sha256:423f81bb55c896a3c85ee98ac7da98826721eaee918f5c0c1dfff99e1972da0c"},
|
||||
]
|
||||
tablib = [
|
||||
{file = "tablib-3.0.0-py3-none-any.whl", hash = "sha256:41aa40981cddd7ec4d1fabeae7c38d271601b306386bd05b5c3bcae13e5aeb20"},
|
||||
{file = "tablib-3.0.0.tar.gz", hash = "sha256:f83cac08454f225a34a305daa20e2110d5e6335135d505f93bc66583a5f9c10d"},
|
||||
|
||||
+1
-1
@@ -31,11 +31,11 @@ django-app-namespace-template-loader = "^0.4.1"
|
||||
django-filter = "^2.0.0"
|
||||
whitenoise = "^4.1.4"
|
||||
jsonschema = "^3.2.0"
|
||||
mailjet-rest = "^1.3.3"
|
||||
Markdown = "^3.2.2"
|
||||
uWSGI = "^2.0.18"
|
||||
gunicorn = "^20.1.0"
|
||||
Pillow = "^8.1.2"
|
||||
sendgrid = "^6.7.0"
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
coverage = "^5.5"
|
||||
|
||||
+4
-4
@@ -203,10 +203,10 @@ REST_FRAMEWORK = {
|
||||
}
|
||||
|
||||
# Email settings (tested working with gmail)
|
||||
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
|
||||
EMAIL_USE_TLS = True
|
||||
EMAIL_HOST = 'smtp.gmail.com'
|
||||
EMAIL_PORT = 587
|
||||
# EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
|
||||
# EMAIL_USE_TLS = True
|
||||
# EMAIL_HOST = 'smtp.gmail.com'
|
||||
# EMAIL_PORT = 587
|
||||
|
||||
|
||||
# Internationalization
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ GOOGLE_RECAPTCHA_SECRET_KEY = os.getenv("GOOGLE_RECAPTCHA_SECRET_KEY", "YOUR-PRI
|
||||
|
||||
# Email settings (more settings in base.py)
|
||||
EMAIL_API_KEY = os.getenv('EMAIL_API_KEY', '')
|
||||
EMAIL_API_SECRET = os.getenv('EMAIL_API_SECRET', '')
|
||||
# EMAIL_API_SECRET = os.getenv('EMAIL_API_SECRET', '')
|
||||
DEFAULT_EMAIL_FROM = 'SIK'
|
||||
DEFAULT_EMAIL_FROM_ADDR = 'noreply@sahkoinsinoorikilta.fi'
|
||||
ENABLE_AUTOMATIC_EMAILS = True
|
||||
|
||||
@@ -47,7 +47,6 @@ services:
|
||||
- DB_HOST=db
|
||||
- DB_PORT=5432
|
||||
- EMAIL_API_KEY_FILE=/run/secrets/BACKEND_EMAIL_API_KEY
|
||||
- EMAIL_API_SECRET_FILE=/run/secrets/BACKEND_EMAIL_API_SECRET
|
||||
|
||||
secrets:
|
||||
- BACKEND_SECRET_KEY
|
||||
@@ -55,7 +54,6 @@ services:
|
||||
- BACKEND_DB_USER
|
||||
- BACKEND_DB_PASSWD
|
||||
- BACKEND_EMAIL_API_KEY
|
||||
- BACKEND_EMAIL_API_SECRET
|
||||
secrets:
|
||||
BACKEND_SECRET_KEY:
|
||||
external: true
|
||||
|
||||
@@ -83,7 +83,7 @@ class SignupTestCase(APITestCase):
|
||||
def test_delete_signup_token(self):
|
||||
pass
|
||||
|
||||
# TODO: Use some mocking library and check that mailjet is actually called
|
||||
# TODO: Use some mocking library and check that sendgrid is actually called
|
||||
def test_signupee_sendemail(self):
|
||||
form = self.signupForm
|
||||
emailURL = f"/api/signupForm/{form.id}/sendemail/"
|
||||
|
||||
+16
-31
@@ -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
|
||||
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
|
||||
}
|
||||
]
|
||||
}
|
||||
from_email = Email(DEFAULT_EMAIL_FROM_ADDR)
|
||||
to_email = To(to)
|
||||
sub = Subject(subject)
|
||||
|
||||
if (html):
|
||||
data["Messages"][0]["HTMLPart"] = body
|
||||
content = HtmlContent(body)
|
||||
else:
|
||||
data["Messages"][0]["TextPart"] = body
|
||||
content = PlainTextContent(body)
|
||||
|
||||
success = mailjet.send.create(data=data)
|
||||
mail = Mail(from_email, to_email, sub, content)
|
||||
|
||||
# For some reason returns 200 OK instead of 201 Created...
|
||||
if success.status_code != 200:
|
||||
raise Exception(f'Failed to send email: {success.json()}')
|
||||
try:
|
||||
sg = sendgrid.SendGridAPIClient(EMAIL_API_KEY)
|
||||
response = sg.client.mail.send.post(request_body=mail.get())
|
||||
|
||||
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