mgbam commited on
Commit
65bc4b3
·
verified ·
1 Parent(s): 1b757f2

Update mailchimp_client.py

Browse files
Files changed (1) hide show
  1. mailchimp_client.py +29 -29
mailchimp_client.py CHANGED
@@ -1,47 +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)
 
1
  # mailchimp_client.py
2
+
3
+ import os
4
  import mailchimp_marketing as MailchimpMarketing
5
  from mailchimp_marketing.api_client import ApiClientError
 
6
 
7
+ # Read credentials from env (set these in HF Space Settings → Secrets)
8
+ API_KEY = os.getenv("MAILCHIMP_API_KEY")
9
+ SERVER = os.getenv("MAILCHIMP_SERVER") # e.g. "us19"
10
+ LIST_ID = os.getenv("MAILCHIMP_LIST_ID")
11
+
12
+ if not all([API_KEY, SERVER, LIST_ID]):
13
+ raise ValueError("Missing one of MAILCHIMP_API_KEY, MAILCHIMP_SERVER, or MAILCHIMP_LIST_ID")
14
 
15
  client = MailchimpMarketing.Client()
16
  client.set_config({
17
+ "api_key": API_KEY,
18
+ "server": SERVER
19
  })
20
 
21
+ def create_and_send_campaign(subject: str, html_content: str):
22
+ """
23
+ Create a Mailchimp campaign and send it immediately.
24
+ """
25
  try:
26
+ # 1. Create campaign
27
  campaign = client.campaigns.create({
28
  "type": "regular",
29
  "recipients": {"list_id": LIST_ID},
30
  "settings": {
31
  "subject_line": subject,
32
+ "title": subject,
33
+ "from_name": "AutoExec AI",
34
+ "reply_to": "no-[email protected]"
35
  }
36
  })
37
+ camp_id = campaign["id"]
 
 
 
 
38
 
39
+ # 2. Set campaign content
40
+ client.campaigns.set_content(camp_id, {"html": html_content})
41
+
42
+ # 3. Send campaign
43
+ client.campaigns.send(camp_id)
44
+ return camp_id
45
+
46
+ except ApiClientError as e:
47
+ raise RuntimeError(f"Mailchimp error: {e.text}")