Python for Entrepreneurs Transcripts
Chapter: Building your email list
Lecture: Using the MailChimp API

Login or purchase this course to watch this video and the rest of the course contents.
0:03 Alright, now we are ready, we've got our mail list service, ready to manage subscribers, adding, removing, updating subscribers,
0:11 all those kinds of things, we've got our forms and our controller, a GET / POST / Redirect pattern ready to do this gathers the data from the users,
0:19 all that is left is to actually call the MailChimp API itself. So it probably doesn't surprise you to know that
0:30 MailChimp is available right here on PyPi. So all we've got to do is "pip install mailchimp", now, there is a couple of ways we can do this,
0:39 remember, we're using this virtual environment, yours probably is different but this is the one I am using for this demo
0:45 so I need to make sure whatever I do I do it here, now we could come up here, this is pretty sleek,
0:49 I could just import MailChimp like this, now notice, MailChimp has a little red thing saying there is no module named MailChimp,
0:57 but PyCharm is smart and it knows that this is a package on PyPi and we could install it, so one way to install it is
1:03 just hit Enter and let PyCharm do it here; another one is we could actually go to the interpreter and say
1:08 I want to add MailChimp and it will go find it, here, so it's just to show you, the third way you can install this of course is to go over here
1:17 and activate that virtual environment, so source or ". activate", the path to it,
1:24 and then we could just "pip install mailchimp", it's going to download, install it, boom, everything is ready to go,
1:34 one thing that you need to be careful of with MailChimp is this docopt requirement, the current version with docopt is 0.6.3,
1:42 but this won't run with 6.3 this only runs with 0.4.0 so you have to pin that version here for docopt, which is really frustrating
1:50 but whatever, that's the way it works. OK, so we have this installed, we need to put this as a requirement in our setup.py
1:56 so when we install this into our server, it's automatically going to bring along the dependencies MailChimp, docopt and so on,
2:04 after all that, we are ready to create this, call this API here, so the way it works is we say mailchimp.Mailchimp like so,
2:12 and what we need to pass is you can see is the API key, so this is going to be MailingListService.mailchimp_api maybe we should call that key,
2:20 I don't know, whatever, that is what we're going to use, and then it's just literally one function call, so maybe we want to do some tests,
2:27 we'll say "if not email... return false", we don't really want to take an empty email,
2:32 now, the MailChimp API throws exceptions and we maybe don't want to throw
2:39 a straight up exception, so we could just catch them and return True or False,
2:42 so let's do this, we'll say "api", now in here there is a lot of things, folders, lists,
2:48 so what we care about are lists, and on the list, we can do things like batch subscribe,
2:54 or get the clients, we'll look at growth history but what we just want to do is subscribe
2:58 and there is a whole bunch of options, I'll show you what will work well,
3:02 so first of all, we're going to pass in the list id, like so, so that goes in,
3:09 remember that came out of our ini file or configuration file, through the dunder init,
3:14 back over to here; next, we have to pass the email and we pass this as a dictionary, which is kind of funky, but it's OK, it'll work,
3:23 so we are going to have our email, and we probably want to do things like strip it, so if there is any spaces,
3:31 maybe lower-case it, so it's sort of canonically just the lower case, no white space variant here, the next thing is merge_vars,
3:38 we are not going to use merge_vars here, these play important roles for things like first name, last name, they allow you to do segmentation,
3:47 so like paying customers versus free customers, and all sorts of stuff like that, we'll talk about that when we get to the concept section,
3:55 but we're going to set two other things here, we're going to set double_optin to be in false,
3:59 so what double_optin is and you can decide what you put here,
4:03 MailChimp, if I say True, will actually send and email to the email address entered saying
4:08 "hey it looks like you want to subscribe to this mailing list, click this button to confirm your subscription there."
4:14 I am going to assume people coming to my site are qualified leads, maybe you want to set that to be True, the default is True, and so on.
4:21 So we'll say update_existing, this is pretty important, if you are doing this multiple times,
4:26 or different levels, especially in the segmentation stuff, so if this is already here and sort of failing to subscribe, I just want to update them,
4:33 and replace_interests is false. So, interest this doesn't matter so much here but I want to put it here anyway
4:40 so when you get to the next section, if you go farther with MailChimp that you don't accidentally leave this out, so what is this about,
4:47 this has to do with interest groups, and you can use interest groups for segmentation, so like I said, paying customers, people who bought this course,
4:54 people who came from here and there, if you say True there, the default, it will only allow them to be in one group, if you say False,
5:01 you will be able to build them up so a customer can be both the paying customer and originating from a web cast and interested in data science,
5:08 or whatever it is like your groups are consisting of. OK, so we've got one little squiggly thing here, what is this unhappy about? The missing except,
5:19 OK, so if that worked, we are going to return True, if it doesn't work, let's catch an exception, do a print and later we could log this to somewhere
5:28 like roll bar or something, but we'll just catch it, like so, and we'll print "Error during mailing",
5:35 so here we can print that out and then let's return False. OK, so I think we're ready, what do you guys think?
5:42 Do you want to try this? do you want to sign up to our email list? We could email ourselves, that sounds cool, let's run it.
5:47 OK, here we are again, it's running, it didn't crash, that's good that means that we've probably setup MailChimp alright,
5:55 so I'll come down I'll put my email address in again, and let's get notified, actually let me use my other one,
6:01 the one I used to register at MailChimp, so, I'll use my michael@michaelckennedy.net, are we ready?
6:07 Go! Awesome, you're on the list, OK, so I think that worked, we actually could have caught the response form the API and print it out,
6:16 the response, but let's just go look at the MailChimp results see if we're now on our list.
6:21 Anyway, here is what I decided to write: "Awesome, you are on the list", something fun and playful.
6:25 "Thanks for signing up to be notified about upcoming events and albums", remember this is our band, "your address is in good hands, we won't spam you
6:32 and by the way, hey why don't you sign up and follow our twitter account as well? Like if you are willing to sign up on the list
6:37 you are probably willing to follow our twitter account." So, this is great, let's go check it at MailChimp.
6:43 Awesome, look, our Blue / Yellow Band has a subscriber, exciting, let's go look who is it, want to bet? I think I know who it is,
6:51 awesome, michael@michaelckennedy.net. So, check this out, we just subscribed it, apparently it's evening time,
6:59 maybe I should go eat dinner, I don't know, we can even see more information about ourselves here like what groups we're in,
7:04 I can make this customer or this person an important person in my list so I can manage them separately, maybe they are like the head of an account,
7:12 at some company I am trying to work with, so I want to make sure I treat them well, you can see all the activity about like past campaigns,
7:19 how I as a subscriber am basically responding. MailChimp does e-commerce tracking for certain things, I don't use that but you could here,
7:29 social profiles like I could, you know, I think it will start pulling stuff like if it finds my Gmail registered somewhere but that email I just used,
7:38 is not really registered anywhere, it's just something that I know will get to me, so I doubt you'll see it anywhere in that list.
7:45 Alright, so here you go, how are we, we were signed up via the API, we did not detect the language because guess what,
7:54 I didn't use the form that they filled out, instead I used just the API, and this is the time zone in GMT. Great, so this is a really good start,
8:02 but, hopefully we end up with thousands of people in this list, not just me signing up to myself, but that's how you use MailChimp.
8:08 Pretty easy, create an account, create a list, get the list id, create API key, "pip install" the MailChimp API and boom api.list.subscribe.


Talk Python's Mastodon Michael Kennedy's Mastodon