File size: 1,600 Bytes
734560e |
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 |
# mailchimp_client.py
import mailchimp_marketing as MailchimpMarketing
from mailchimp_marketing.api_client import ApiClientError
import os
MAILCHIMP_API_KEY = os.getenv("MAILCHIMP_API_KEY")
MAILCHIMP_SERVER = os.getenv("MAILCHIMP_SERVER") # e.g., "us19"
LIST_ID = os.getenv("MAILCHIMP_LIST_ID") # create a list and paste its ID here
client = MailchimpMarketing.Client()
client.set_config({
"api_key": MAILCHIMP_API_KEY,
"server": MAILCHIMP_SERVER
})
def create_campaign(subject: str, html_content: str):
try:
campaign = client.campaigns.create({
"type": "regular",
"recipients": {"list_id": LIST_ID},
"settings": {
"subject_line": subject,
"title": subject,
"from_name": "AutoExec AI",
"reply_to": "no‑[email protected]"
}
})
client.campaigns.set_content(campaign["id"], {"html": html_content})
return campaign["id"]
except ApiClientError as error:
print("Error creating campaign: {}".format(error.text))
raise
def send_campaign(campaign_id: str):
try:
client.campaigns.send(campaign_id)
except ApiClientError as error:
print("Error sending campaign: {}".format(error.text))
raise
if __name__ == "__main__":
camp_id = create_campaign(
subject="Welcome to Your AI Fitness Experience!",
html_content="<h1>Welcome!</h1><p>Let’s get you moving with AI‑driven workouts.</p>"
)
send_campaign(camp_id)
print("Campaign sent with ID:", camp_id)
|