mgbam commited on
Commit
734560e
·
verified ·
1 Parent(s): ebce4e7

Create mailchimp_client.py

Browse files
Files changed (1) hide show
  1. mailchimp_client.py +47 -0
mailchimp_client.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # mailchimp_client.py
2
+ import mailchimp_marketing as MailchimpMarketing
3
+ from mailchimp_marketing.api_client import ApiClientError
4
+ import os
5
+
6
+ MAILCHIMP_API_KEY = os.getenv("MAILCHIMP_API_KEY")
7
+ MAILCHIMP_SERVER = os.getenv("MAILCHIMP_SERVER") # e.g., "us19"
8
+ LIST_ID = os.getenv("MAILCHIMP_LIST_ID") # create a list and paste its ID here
9
+
10
+ client = MailchimpMarketing.Client()
11
+ client.set_config({
12
+ "api_key": MAILCHIMP_API_KEY,
13
+ "server": MAILCHIMP_SERVER
14
+ })
15
+
16
+ def create_campaign(subject: str, html_content: str):
17
+ try:
18
+ campaign = client.campaigns.create({
19
+ "type": "regular",
20
+ "recipients": {"list_id": LIST_ID},
21
+ "settings": {
22
+ "subject_line": subject,
23
+ "title": subject,
24
+ "from_name": "AutoExec AI",
25
+ "reply_to": "no‑[email protected]"
26
+ }
27
+ })
28
+ client.campaigns.set_content(campaign["id"], {"html": html_content})
29
+ return campaign["id"]
30
+ except ApiClientError as error:
31
+ print("Error creating campaign: {}".format(error.text))
32
+ raise
33
+
34
+ def send_campaign(campaign_id: str):
35
+ try:
36
+ client.campaigns.send(campaign_id)
37
+ except ApiClientError as error:
38
+ print("Error sending campaign: {}".format(error.text))
39
+ raise
40
+
41
+ if __name__ == "__main__":
42
+ camp_id = create_campaign(
43
+ subject="Welcome to Your AI Fitness Experience!",
44
+ html_content="<h1>Welcome!</h1><p>Let’s get you moving with AI‑driven workouts.</p>"
45
+ )
46
+ send_campaign(camp_id)
47
+ print("Campaign sent with ID:", camp_id)