add email, don't send by default, only queue

This commit is contained in:
j 2023-09-28 11:33:42 +02:00
parent 2873707e50
commit 073f8add07
2 changed files with 35 additions and 5 deletions

View file

@ -8,6 +8,10 @@ from django.utils.timezone import datetime, timedelta
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
headers = {
'Content-Type': 'application/json;charset=utf-8'
}
def send_week(year, month, day): def send_week(year, month, day):
from . import views from . import views
@ -34,17 +38,42 @@ def send_email(name, subject, body):
'type': 'regular', 'type': 'regular',
'body': body 'body': body
} }
headers = {
'Content-Type': 'application/json;charset=utf-8'
}
url = settings.LISTMONK_API + 'campaigns' url = settings.LISTMONK_API + 'campaigns'
auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD) auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD)
r = requests.post(url, json=data, headers=headers, auth=auth).json() r = requests.post(url, json=data, headers=headers, auth=auth).json()
if 'id' in r.get('data', {}): list_id = r.get('data', {}).get('id')
url = settings.LISTMONK_API + 'campaigns/%s/status' % r['data']['id'] if list_id and settings.LISTMONK_SEND:
url = settings.LISTMONK_API + 'campaigns/%s/status' % list_id
data = {"status": "running"} data = {"status": "running"}
r = requests.put(url, json=data, headers=headers, auth=auth).json() r = requests.put(url, json=data, headers=headers, auth=auth).json()
return True return True
else: else:
return False return False
def email2name(email):
name = email.split("@")[0]
name = name.replace(".", " ")
name = " ".join([part.capitalize() for part in name.split(" ")])
return name
def add_email(email):
url = settings.LISTMONK_API + 'subscribers'
auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD)
exists = url + '?' + "list_id=&query=email='%s'&page=1&order_by=id&order=desc" % email
if not len(requests.get(exists, auth=auth).json()['data']['results']):
data = {
"email": email,
"name": email2name(email),
"status": "enabled",
"lists": settings.LISTMONK_LISTS,
}
r = requests.post(url, json=data, headers=headers, auth=auth).json()
if r.get("data", {}).get("id"):
return True
else:
logger.errror("failed to add email to list %s => %s", email, r)
return False
return False

View file

@ -167,6 +167,7 @@ LISTMONK_USER = "listmonk"
LISTMONK_PASSWORD = "listmonk" LISTMONK_PASSWORD = "listmonk"
LISTMONK_API = "http://localhost:9000/api/" LISTMONK_API = "http://localhost:9000/api/"
LISTMONK_LISTS = [] LISTMONK_LISTS = []
LISTMONK_SEND = False
RATELIMIT_CACHE_BACKEND = "app.brake_backend.BrakeBackend" RATELIMIT_CACHE_BACKEND = "app.brake_backend.BrakeBackend"