-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailchimp_service.py
More file actions
110 lines (93 loc) · 3.25 KB
/
Copy pathmailchimp_service.py
File metadata and controls
110 lines (93 loc) · 3.25 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
import mailchimp_marketing as MailchimpMarketing
from mailchimp_marketing.api_client import ApiClientError
def get_mailchimp_client():
api_key = os.environ.get("MAILCHIMP_API_KEY")
server_prefix = os.environ.get("MAILCHIMP_SERVER_PREFIX")
if not api_key or not server_prefix:
return None
client = MailchimpMarketing.Client()
client.set_config({
"api_key": api_key,
"server": server_prefix
})
return client
def subscribe_to_newsletter(email, list_id=None):
client = get_mailchimp_client()
if not client:
return {"error": "Mailchimp not configured"}
if not list_id:
list_id = os.environ.get("MAILCHIMP_LIST_ID")
if not list_id:
return {"error": "Mailchimp List ID not configured"}
try:
response = client.lists.add_list_member(list_id, {
"email_address": email,
"status": "subscribed",
})
return {"status": "success", "id": response['id']}
except ApiClientError as e:
return {"error": str(e.text)}
except Exception as e:
return {"error": str(e)}
def create_campaign(subject_line, preview_text, title, from_name, reply_to, list_id=None):
client = get_mailchimp_client()
if not client:
return {"error": "Mailchimp not configured"}
if not list_id:
list_id = os.environ.get("MAILCHIMP_LIST_ID")
if not list_id:
return {"error": "Mailchimp List ID not configured"}
try:
campaign_config = {
"type": "regular",
"recipients": {
"list_id": list_id
},
"settings": {
"subject_line": subject_line,
"preview_text": preview_text,
"title": title,
"from_name": from_name,
"reply_to": reply_to
}
}
response = client.campaigns.create(campaign_config)
return {"status": "success", "id": response['id']}
except ApiClientError as e:
return {"error": str(e.text)}
except Exception as e:
return {"error": str(e)}
def set_campaign_content(campaign_id, html_content):
client = get_mailchimp_client()
if not client:
return {"error": "Mailchimp not configured"}
try:
response = client.campaigns.set_content(campaign_id, {"html": html_content})
return {"status": "success"}
except ApiClientError as e:
return {"error": str(e.text)}
except Exception as e:
return {"error": str(e)}
def send_campaign(campaign_id):
client = get_mailchimp_client()
if not client:
return {"error": "Mailchimp not configured"}
try:
client.campaigns.send(campaign_id)
return {"status": "success"}
except ApiClientError as e:
return {"error": str(e.text)}
except Exception as e:
return {"error": str(e)}
def get_campaign_reports():
client = get_mailchimp_client()
if not client:
return {"error": "Mailchimp not configured"}
try:
response = client.reports.get_all_campaign_reports()
return {"status": "success", "reports": response['reports']}
except ApiClientError as e:
return {"error": str(e.text)}
except Exception as e:
return {"error": str(e)}