MailChimp is a great tool for email marketing although the adding of emails to the platform can be a chore. In the post we will be going through how to add a list emails and client data to the MailChimp platform using their API and a Python script.
To begin with please ensure you do have your API key and Audience ID ready, these can both be found in MailChimp including the free accounts – You do not need to be paying MailChimp user to achieve this.
we start by opening the Excel spreadsheet full of recipient names and emails via Openpyxl and define the sheet name.
wb = openpyxl.load_workbook('C:/Desktop/Mailing List.xlsx')
sheet = wb["Sheet1"]
With the sheet now open we will iterate through all entries and define the columns for the first name, last name and email address.
for i in range(1,sheet.max_row+1):
firstname = (sheet.cell(row=i,column=1).value)
lastname = (sheet.cell(row=i,column=2).value)
email = (sheet.cell(row=i,column=3).value)
We then define our MailChimp client and the email addresses to our list.
try:
client = MailchimpMarketing.Client()
client.set_config({
"api_key": "YOUR_API_KEY","server": "usXX"})
response = client.lists.set_list_member("230842e0la", "subscriber_hash", {"email_address": email , "status_if_new": "subscribed"})
Once the email address has been added, we update the entry with the first name and last name merge fields. MailChimp allows you to refer to an email within a list by using the MD5 hash equivalent (all lower cases). So we has our email in python, I have a line that will change all characters – Making life a little easier.
email = re.sub('([A-Z]{1})', r'\1',email).lower()
email = email.encode('utf-8')
result = hashlib.md5(email).hexdigest()
With the hash ready we can move on to updating the merge fields.
try:
client = MailchimpMarketing.Client()
client.set_config({
"api_key": "YOUR_API_KEY","server": "usXX"})
response = client.lists.update_list_member("230842e0la", result, {'merge_fields': {'FNAME': firstname, 'LNAME': lastname }})
except ApiClientError as error:
print("Unuccessful In Updating"+ str(email))
Full source code for this project can be found below.
from openpyxl import Workbook
from win32com import client
import openpyxl
import mailchimp_marketing as MailchimpMarketing
from mailchimp_marketing.api_client import ApiClientError
import hashlib
import re
wb = openpyxl.load_workbook('C:/Desktop/Mailing List.xlsx')
sheet = wb["Sheet1"]
for i in range(1,sheet.max_row+1):
firstname = (sheet.cell(row=i,column=1).value)
lastname = (sheet.cell(row=i,column=2).value)
email = (sheet.cell(row=i,column=3).value)
try:
client = MailchimpMarketing.Client()
client.set_config({
"api_key": "YOUR_API_KEY","server": "usXX"})
response = client.lists.set_list_member("230842e0la", "subscriber_hash", {"email_address": email , "status_if_new": "subscribed"})
email = re.sub('([A-Z]{1})', r'\1',email).lower()
email = email.encode('utf-8')
result = hashlib.md5(email).hexdigest()
try:
client = MailchimpMarketing.Client()
client.set_config({
"api_key": "YOUR_API_KEY","server": "usXX"})
response = client.lists.update_list_member("230842e0la", result, {'merge_fields': {'FNAME': firstname, 'LNAME': lastname }})
except ApiClientError as error:
print("Unuccessful In Updating"+ str(email))
except ApiClientError as error:
print("Unsuccessful In Adding - User Already Exists " + str(email))