add email, don't send by default, only queue
This commit is contained in:
parent
2873707e50
commit
073f8add07
2 changed files with 35 additions and 5 deletions
|
@ -8,6 +8,10 @@ from django.utils.timezone import datetime, timedelta
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
headers = {
|
||||
'Content-Type': 'application/json;charset=utf-8'
|
||||
}
|
||||
|
||||
|
||||
def send_week(year, month, day):
|
||||
from . import views
|
||||
|
@ -34,17 +38,42 @@ def send_email(name, subject, body):
|
|||
'type': 'regular',
|
||||
'body': body
|
||||
}
|
||||
headers = {
|
||||
'Content-Type': 'application/json;charset=utf-8'
|
||||
}
|
||||
url = settings.LISTMONK_API + 'campaigns'
|
||||
auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD)
|
||||
r = requests.post(url, json=data, headers=headers, auth=auth).json()
|
||||
if 'id' in r.get('data', {}):
|
||||
url = settings.LISTMONK_API + 'campaigns/%s/status' % r['data']['id']
|
||||
list_id = r.get('data', {}).get('id')
|
||||
if list_id and settings.LISTMONK_SEND:
|
||||
url = settings.LISTMONK_API + 'campaigns/%s/status' % list_id
|
||||
data = {"status": "running"}
|
||||
r = requests.put(url, json=data, headers=headers, auth=auth).json()
|
||||
return True
|
||||
else:
|
||||
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
|
||||
|
||||
|
|
|
@ -167,6 +167,7 @@ LISTMONK_USER = "listmonk"
|
|||
LISTMONK_PASSWORD = "listmonk"
|
||||
LISTMONK_API = "http://localhost:9000/api/"
|
||||
LISTMONK_LISTS = []
|
||||
LISTMONK_SEND = False
|
||||
|
||||
|
||||
RATELIMIT_CACHE_BACKEND = "app.brake_backend.BrakeBackend"
|
||||
|
|
Loading…
Reference in a new issue