-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-invites.py
executable file
·84 lines (72 loc) · 2.29 KB
/
send-invites.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
import configparser
import json
import re
import requests
from requests.auth import HTTPBasicAuth
import sys
from urllib.parse import urljoin
def get_auth(config):
return HTTPBasicAuth(config['listmonk']['APIUser'], config['listmonk']['APIPass'])
def get_url(config, path):
return urljoin(config['listmonk']['BaseURL'], path)
def create_subscriber(config, data):
'''
Uses the listmonk api to create the subscriber.
'''
if 'SubscribeLists' in config['listmonk']:
lists = [int(x) for x in config['listmonk']['SubscribeLists'].split(',')]
else:
lists = []
obj = {
"email": data['email'],
"name": data['name'],
"lists": lists,
"status": "enabled",
}
x = requests.post(
get_url(config, '/api/subscribers'),
auth=get_auth(config),
headers={"Content-Type": "application/json; charset=utf-8"},
json=obj)
def send_invite(config, data):
'''
Uses the listmonk api to send a transactional mail.
'''
create_subscriber(config, data)
obj = {
"subscriber_email": data['email'],
"from_email": config['listmonk']['FromEmail'],
"template_id": int(config['listmonk']['TemplateID']),
"data": data,
"content_type": "html"
}
x = requests.post(
get_url(config, '/api/tx'),
auth=get_auth(config),
headers={"Content-Type": "application/json; charset=utf-8"},
json=obj)
if x.ok:
print(f"{data['username']}\t{x.status_code}: {x.reason}", file=sys.stderr)
else:
print(f"{data['username']}\t{x.status_code}: {x.reason} {x.text}", file=sys.stderr)
def main():
config = configparser.ConfigParser()
config.read('enroll.ini')
config.read('tokens.ini')
for line in sys.stdin:
line = line.rstrip()
m = re.match('^([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)$', line)
if m:
invite_data = {
"username": m.group(1),
"name": m.group(2),
"email": m.group(3),
"itoken": m.group(4),
"expire": m.group(5),
}
send_invite(config, invite_data)
else:
print(f"ignoring line: {line}", file=sys.stderr)
if __name__ == '__main__':
main()