|
|
|
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") |
|
LIST_ID = os.getenv("MAILCHIMP_LIST_ID") |
|
|
|
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) |
|
|