Python and mailchimp integration

Viewed 1144

So i have been stuck on this for a week or so and could really do this some advice.

I have a basic website which allows people to sign up to an automated newsletter that i want to send out. After adding their email on the site their address is automatically added to an audience. I have a small python script running that then web scrapes a site and then if that returns a certain criteria it will send an automated email out via mailchimp to all the contacts in the mailchimp audience. What i am having issue with is actually creating and sending out the email via mail chimp. I have been through https://mailchimp.com/developer/api/marketing/campaigns/add-campaign/ a few times and can't seem to get it working. I am able to create a new campaign succesfully as well as the audience and subject line. I am unable to workout how to actually send the email with the content i want inside it however. It just saves itself as a draft and thats it. When i try to preview the email there is nothing in it and when i click edit next to the campaign everything is ticked except for the 'content. I have excluded th web scraping part of my program but below is the test i am running to create and sent out via the mailchimp api

import mailchimp_marketing as MailchimpMarketing
from mailchimp_marketing.api_client import ApiClientError
from mailchimp3 import MailChimp
data = {
            "recipients" :
            {
                "list_id": 'XXXXXXXX'
            },
            "settings":
            {
                "subject_line": 'Subject of email',
                "from_name": 'from_name',
                "reply_to": 'reply_email',
            },
            "type": "regular"
        }

try:
  client = MailchimpMarketing.Client()
  #print(client)
  client.set_config({
    "api_key": "XXXXXXXXXXXXXXXX",
    "server": "XXXXXXX"
  })
  #client = MailChimp(mc_api='XXXXXXXXXXXX', mc_user="XXXXXXXXX")
  client.campaigns.create(data)
  response = client.campaigns.get('campaign_id')
  #client.campaigns.send()
  print(response)
except ApiClientError as error:
  print("Error: {}".format(error.text))

This succesfully creates the campaign except without the content that i want to add and simply saves the email as a draft without send. So i guess my question is how to i edit the email content and then how do i actually initiate the send.

Thanks for any help

1 Answers

I also didn't find a minimal example on the web. Plus the examples in the mailchimp api documentation are severely lacking for python (only curl seems correct). Here's a minimal example:

from mailchimp_marketing import Client, api_client

# find this out at https://mailchimp.com/en/help/about-api-keys/
API_KEY = '…'

# log into your Mailchimp account and look at the URL in your browser.
# You’ll see something like https://us19.admin.mailchimp.com/
# the us19 part is the server prefix.
SERVER_PREFIX = 'us19'

try:
  client = Client()
  client.set_config({
    "api_key": API_KEY,
    "server": SERVER_PREFIX
  })

# find out list id: https://mailchimp.com/en/help/find-audience-id/
  campaign_data = dict(
    type='regular',
    recipients=dict(list_id='…'),
    settings=dict(
      subject_line='lorem ipsum',
      from_name='John Doe',
      reply_to='john@doe.com',
    )
  )
  campaign = client.campaigns.create(campaign_data)
  print(campaign)
  campaign_id = campaign['id']
  content_data = dict(
    plain_text='lorem ipsum',
    html='<p><strong>lorem</strong><br />ipsum</p>'
  )
  response = client.campaigns.set_content(campaign_id, content_data)
  print(response)

  response = client.campaigns.send(campaign_id)
  print(response)
except api_client.ApiClientError as e:
  print("Error: {}".format(error.text))
Related