Update mailchimp_client.py
Browse files- 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 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
client = MailchimpMarketing.Client()
|
| 11 |
client.set_config({
|
| 12 |
-
"api_key":
|
| 13 |
-
"server":
|
| 14 |
})
|
| 15 |
|
| 16 |
-
def
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
|
|
|
| 18 |
campaign = client.campaigns.create({
|
| 19 |
"type": "regular",
|
| 20 |
"recipients": {"list_id": LIST_ID},
|
| 21 |
"settings": {
|
| 22 |
"subject_line": subject,
|
| 23 |
-
"title":
|
| 24 |
-
"from_name":
|
| 25 |
-
"reply_to":
|
| 26 |
}
|
| 27 |
})
|
| 28 |
-
|
| 29 |
-
return campaign["id"]
|
| 30 |
-
except ApiClientError as error:
|
| 31 |
-
print("Error creating campaign: {}".format(error.text))
|
| 32 |
-
raise
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 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-reply@yourdomain.com"
|
| 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}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|