Check your version

This video covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

Subscribe a Drupal User Account to Your List

Video loading...

  • 0:02
    Using MailChimp with Drupal 7
  • 0:04
    Subscribe a Drupal User Account to Your List Series Sponsored by MailChimp
  • 0:08
    One of the main advantages of using the MailChimp module to integrate Drupal
  • 0:13
    with the MailChimp service is that you can allow users of your site
  • 0:17
    to manage their MailChimp subscription
  • 0:20
    from the Drupal account on your site using the Drupal user interface.
  • 0:24
    In this tutorial, I want to take a look at some of the end goal,
  • 0:29
    allow someone that has created Drupal accounts
  • 0:32
    on our site to manage their subscription
  • 0:35
    in MailChimp by doing things like editing their local account
  • 0:39
    and deciding from there whether or not they're subscribed to the mailing list.
  • 0:42
    In order to do that, we're going to take a look at
  • 0:46
    the relationship between Drupal entities and MailChimp,
  • 0:50
    specifically the MailChimp list field provided by the MailChimp lists module.
  • 0:56
    We're going to configure the MailChimp lists field
  • 1:00
    on Drupal user entities so that when you create a new account
  • 1:05
    or edit an existing account, that field is present and allows you to subscribe
  • 1:10
    or unsubscribe from a MailChimp list.
  • 1:12
    This is one of my favorite parts about the MailChimp module and its integration
  • 1:17
    with the MailChimp service; is the stability to take a field and apply it to any entity in Drupal.
  • 1:22
    We're going to use user accounts, but it will work with any, and allow it to manage a subscription
  • 1:27
    of that entity to a mailing list.
  • 1:30
    Now, this tutorial assumes that you've already got a list
  • 1:34
    or maybe multiple lists configured in MailChimp,
  • 1:38
    because we'll need that so that you can subscribe to a list,
  • 1:41
    and it's also going to use Drupal's entity and field
  • 1:44
    user interface a bit, so I'm assuming that you're at least familiar
  • 1:48
    with the concept of adding fields to entities.
  • 1:52
    If you're not, I recommend taking a look at our tutorials on that before you proceeding with this one.
  • 1:57
    By the end of this tutorial, you should be able to configure
  • 2:00
    your Drupal site to allow users to manage their MailChimp subscription from within Drupal,
  • 2:05
    as well as allow new users to opt into your mailing list
  • 2:09
    when they sign up for an account on your site.
  • 2:12
    Let's go ahead and get started.
  • 2:13
    So just to show that this is the case, I've already got two lists
  • 2:16
    configured for my MailChimp account at admin.MailChimp.com.
  • 2:20
    So if you don't have a list, I'm assuming that you do already, go ahead and create those.
  • 2:25
    Over on our Drupal site,
  • 2:27
    the first thing that we're going to need to do is enable the MailChimp lists module.
  • 2:32
    So I'm going to go to the Modules tab and I'm going to scroll down,
  • 2:35
    and find MailChimp Lists,
  • 2:39
    and I'll check that off.
  • 2:41
    It's got a couple of dependencies which we already
  • 2:43
    have present in our code base, so they'll just get enabled for us as necessary.
  • 2:48
    But if they weren't there, you would want to make sure
  • 2:49
    that you also had entity API
  • 2:52
    and entity tokens present.
  • 2:55
    So really just the entity module, entity tokens is part of that.
  • 2:58
    We already had to have libraries in order to enable the MailChimp module in the first place.
  • 3:02
    So just click MailChimp lists here and save configuration.
  • 3:07
    Go ahead and enable the entity module.
  • 3:10
    The MailChimp lists module
  • 3:12
    is the module that's responsible for providing the
  • 3:15
    MailChimp list field
  • 3:17
    that we can apply to any entity in Drupal
  • 3:20
    in order to allow subscription management.
  • 3:22
    Once the module has been enabled, I'm going to go to Configuration,
  • 3:25
    scroll down and go to MailChimp under web services,
  • 3:29
    and you can see now that there are two tabs
  • 3:32
    up here that were not present before— Fields and Lists.
  • 3:37
    Fields is currently empty because we haven't configured any MailChimp subscription fields.
  • 3:42
    But once we do, you'll be able to see them listed here.
  • 3:45
    And then Lists, if I click on that, displays a list
  • 3:50
    of the mailing lists that we've got configured in MailChimp—
  • 3:54
    our Daily Flavor Updates and our Weekly Newsletter.
  • 3:57
    I want to point out this link right here—"Refresh lists from MailChimp."
  • 4:00
    This is important because what happens is
  • 4:02
    the MailChimp lists module needs some information
  • 4:05
    about your lists in order to manage subscriptions on them.
  • 4:09
    So it makes a request to the MailChimp API and pulls down that information,
  • 4:13
    and then it caches it all locally.
  • 4:16
    If you ever make changes to the settings of your lists within MailChimp,
  • 4:19
    if those aren't automatically reflected onto the Drupal sites,
  • 4:23
    so, say, for example, we change the name of one of these lists,
  • 4:27
    you can come here and click "Refresh lists from MailChimp,"
  • 4:30
    which will allow you to clear the cache and force the MailChimp module to
  • 4:33
    go off and grab new data from MailChimp.com and cache it locally in Drupal.
  • 4:38
    It caches it in order to improve performance.
  • 4:41
    So we said that what we wanted to do was make it so that
  • 4:43
    users on our site could manage their subscription directly from their user account.
  • 4:49
    So we're going to go to Configuration,
  • 4:51
    Account settings, and Manage fields.
  • 4:55
    This is the user interface for adding new fields
  • 4:58
    to Drupal's user entities.
  • 5:00
    I'm going to add a new field here, and we'll call it "Subscriptions" like so.
  • 5:06
    I can select a field type, and from the field type drop down,
  • 5:11
    I have this MailChimp Subscription option.
  • 5:14
    This is what's provided by the module that we just enabled,
  • 5:16
    and there's one widget for the field type,
  • 5:19
    subscription form, so I'll go ahead and click Save.
  • 5:22
    I'm now asked to provide some field settings.
  • 5:25
    So the first thing I need to do
  • 5:27
    is choose which of the lists
  • 5:31
    that I have configured in MailChimp
  • 5:33
    does this field relate to.
  • 5:35
    I'm going to say this field relates to our weekly newsletter.
  • 5:38
    And then I can also set some additional settings
  • 5:41
    when somebody is using this field to subscribe to the weekly newsletter
  • 5:46
    mailing list in MailChimp, do we want to require them to double opt in
  • 5:50
    by clicking a confirmation link in an email
  • 5:52
    that's sent to them from MailChimp.
  • 5:54
    And/or do we want to send them a welcome email.
  • 5:58
    I'm going to leave both of these blank, these are just toggles for enabling functionality
  • 6:03
    that exists in the MailChimp module.
  • 6:05
    I'll click Save field settings, and then we'll go look at a couple of other configuration options.
  • 6:10
    So we already set the label here.
  • 6:13
    We'll leave this field optional.
  • 6:15
    We don't want to require people to subscribe to our mailing list.
  • 6:17
    Because this is the user entity type,
  • 6:20
    we have an additional configuration option here
  • 6:22
    to display on user registration form.
  • 6:25
    And I'm going to say yes, let's display this on the user registration form
  • 6:28
    so that when someone creates a new account,
  • 6:31
    we'll give them the opportunity to subscribe to our list.
  • 6:34
    Help text to go with the field, Enable Interest Groups—
  • 6:38
    this is related to MailChimp—
  • 6:41
    if we wanted to allow people to choose from interest groups that we had configured
  • 6:45
    for a list in MailChimp, we could check this box here,
  • 6:49
    and those options would show up when someone is editing their account.
  • 6:53
    We don't currently have any interest groups configured,
  • 6:56
    but I'll check the box anyways.
  • 6:58
    And then here we've got Merge Fields.
  • 7:00
    This is really cool—what this does is it allows you to take the value
  • 7:05
    of properties or fields attached to any entity in Drupal
  • 7:10
    and merge those values into fields for your MailChimp mailing list.
  • 7:15
    When we first created the Weekly Newsletter mailing list,
  • 7:19
    Drupal added a first name and last name field for us by default.
  • 7:23
    We don't have anything currently
  • 7:25
    that maps to those, so I'll just leave those blank for now.
  • 7:28
    But what we do need to do is make sure that we select
  • 7:31
    a field or property of this entity type
  • 7:36
    that will contain an email address, and this is required.
  • 7:39
    For user accounts, that's going to be the email property
  • 7:42
    on the Drupal user entity.
  • 7:44
    Whatever field you choose here,
  • 7:47
    you need to make sure that it's one that's going to contain an email address
  • 7:51
    whenever someone creates a new entity of this type,
  • 7:54
    because this is the address that will be used to subscribe the user
  • 7:58
    to the MailChimp mailing list.
  • 8:02
    When somebody fills out the form to create a new user on our account,
  • 8:06
    and they add their email address, if they click the "subscribe to our mailing list" button,
  • 8:10
    what it will do is take the email address
  • 8:13
    from the user email property
  • 8:17
    and use that as the email address that is sent to MailChimp.
  • 8:21
    Do we want to default subscriptions to on or off?
  • 8:25
    I'm going to leave it as off, we'll let people opt into subscribing.
  • 8:29
    Then these are the settings that we just looked at on the previous page.
  • 8:32
    So I click Save settings.
  • 8:34
    Once I've done that, if I go to my
  • 8:37
    administrators account, so I navigate to
  • 8:40
    my profile and then I click on the Edit tab
  • 8:42
    and scroll down, you'll notice that there is an additional field displayed here—
  • 8:47
    subscriptions, subscribe—so this is the
  • 8:50
    option to subscribe to our mailing list.
  • 8:53
    It looks like we should probably add a little bit of help text here.
  • 8:55
    So let's do that quick to make this clearer.
  • 8:57
    So I'm going to go back to Configuration, Account settings, Manage fields,
  • 9:02
    click Edit for our subscription field,
  • 9:04
    and let's say "Subscribe to our weekly newsletter."
  • 9:11
    So save that setting.
  • 9:14
    Now when I edit my profile by going to my profile and clicking on the Edit tab,
  • 9:18
    there's a little more descriptive text here.
  • 9:21
    It makes it a little more obvious what's going on when I click this box.
  • 9:24
    Let's demonstrate that it works.
  • 9:26
    I need to change the email address here.
  • 9:29
    When I first set up this account, I just used [email protected],
  • 9:33
    but MailChimp doesn't like @example.com email addresses.
  • 9:37
    So I'll use a real email address.
  • 9:40
    Then I'll click Subscribe.
  • 9:43
    Click Save.
  • 9:45
    That's right, I need to enter my password when I make changes like this to my account.
  • 9:48
    Click Save.
  • 9:54
    If I slipped over to MailChimp now,
  • 9:55
    so back on admin.mailchimp.com,
  • 9:59
    if I go to Weekly Newsletter,
  • 10:02
    I should see two subscribers—there we go!
  • 10:06
    The one that was subscribed in a previous tutorial
  • 10:09
    where we filled out the subscription form,
  • 10:11
    and then the subscription that just occurred
  • 10:14
    when I clicked the subscribe box
  • 10:17
    for the user account on my Drupal site.
  • 10:20
    If I switch back to the Drupal site,
  • 10:22
    I can revert that by unchecking the subscribe box and clicking save.
  • 10:26
    If I switch to MailChimp again and refresh the page,
  • 10:30
    you'll notice that that subscription is now gone.
  • 10:33
    Pretty awesome, so we've given users the ability to manage their subscription
  • 10:37
    by editing their account in Drupal.
  • 10:40
    They never have to go to the MailChimp website,
  • 10:42
    they don't have to bother with clicking on the unsubscribe link at the bottom of the newsletter.
  • 10:46
    They can subscribe and unsubscribe right here.
  • 10:49
    In addition to that, if I log out,
  • 10:51
    when I create a new account on my site,
  • 10:55
    I also get the subscription field because we selected the option
  • 10:59
    to display this on the user registration form.
  • 11:01
    So I can create a new account like test1.
  • 11:04
    I'll just call this test1.
  • 11:07
    Subscribe, I still don't have any interest groups to choose from,
  • 11:11
    but if I configured those in MailChimp, they'd show up here.
  • 11:13
    And now click Create New Account.
  • 11:15
    Now my account is pending administrator approval.
  • 11:18
    You can see though over in MailChimp that subscription request has already gone through.
  • 11:23
    So I'm subscribed to the mailing list.
  • 11:25
    Once my account is approved by an administrator,
  • 11:27
    I'll also be able to disable
  • 11:29
    or re-enable the subscription at any time.
  • 11:32
    So that's it.
  • 11:34
    That's using the MailChimp Lists module
  • 11:38
    to synchronize a list of mailing lists from MailChimp,
  • 11:42
    and then creating a field and adding it to an entity
  • 11:44
    in order to allow that entity to be subscribed to a list.
  • 11:49
    In our case, allowing users, which are a Drupal entity type,
  • 11:53
    to subscribe to a mailing list in MailChimp.
  • 11:55
    In this tutorial, we took a look at the
  • 11:58
    relationship between Drupal entities and MailChimp subscriptions,
  • 12:02
    and we talked about how the MailChimp List module
  • 12:04
    provides a new field that you can add to any entity
  • 12:07
    in order to allow that entity to effectively be subscribed
  • 12:10
    to your MailChimp mailing list.
  • 12:13
    And then we added a field
  • 12:15
    to our Drupal user entity type,
  • 12:18
    which allowed us to enable users of our site
  • 12:22
    to manage their subscription right from within Drupal.
  • 12:24
    It also served as a good demonstration of how the integration between the MailChimp list field
  • 12:30
    and MailChimp API works.
  • 12:33
    In the next lesson, we'll take a look at collecting additional information
  • 12:37
    from users who are signing up for our list
  • 12:39
    and merging that into MailChimp merge fields
  • 12:42
    so we have that information in MailChimp as well.

Subscribe a Drupal User Account to Your List

Loading...

One of the main advantages of using the MailChimp module to integrate Drupal with MailChimp is that you can allow users of your site with a Drupal account to manage their MailChimp subscriptions right through the Drupal user interface. In this tutorial we'll look at adding a MailChimp Lists field to Drupal's user entity type and then configuring that field so that we can allow users to subscribe/unsubscribe from a MailChimp list by signing in to their account. We'll also look at how we can allow users to opt-in to a MailChimp list when they register for a new account on our Drupal site.

The MailChimp Lists module is super flexible, and allows you to create a relationship between any fieldable Drupal entity and a list in MailChimp. While we're going to be using the module exclusively with user entities there are a lot of other options available. Basically, any entity with an email address can be subscribed to a list by adding the MailChimp Lists field to the entity type, choosing the list that subscriptions should be added to, and telling the field which other property, or field, on entities of this type contains a valid email address.

By the end of this tutorial you should be able to configure your Drupal site to allow users to manage their MailChimp subscription from within Drupal. As well as allow new users to opt-in to your mailing lists when they sign up for an account.

Further your learning

  • Why does the MailChimp Lists module use a Drupal field API field to track subscriptions?
  • Can you describe the primary difference(s) between allowing users to subscribe via the MailChimp Lists module, and allowing users to subscribe via the MailChimp signup module?
  • Further exploration: What happens if you delete the account of a user who is subscribed to a MailChimp list?
  • Further exploration: Can you create a new node type that can be subscribed to a MailChimp List? Give a use case for doing so.

Series Sponsored by MailChimp

Downloads: 
Log in or sign up to download companion files.
Additional resources: