Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi!
I'm maintaining a module where users optionally are posting their e-mail addresses and I'd like to be able to add them to the mailchimp list automatically.
Where can I find information on howt to utilize the api to do this?
mailchimp.api.php only defines the hooks that I might implement in my module, but does not say how I can call the api to subscribe a user.
Comments
Comment #1
gcbHave a look at the functions in mailchimp.module and mailchimp_lists.module -- mailchimp_subscribe_user and mailchimp_lists_get_available_lists might be a good place to start. Beware the ubiquitous use of the variable "$list" which sometimes refers to an array describing a list that exists on MailChimp, and sometimes refers to the list entity created by the mailchimp_lists module.
Comment #2
stenjo CreditAttribution: stenjo commentedThank you for your response.
What you are pointing out is exactly where I got stuck as well. Calling the
mailchimp_subscribe_user()
requires a list as a parameter. How do I prepare that list, using what functions?There is an example of the usage on
function mailchimp_lists_user_subscribe_form_submit($form, &$form_state)
but here the list is obtained from the submission.A simple code example on this would be great. Are you able to provide one where a subscriber is added having nothing else than the list id (integer i think), a name of the subscriber and her e-mail address?
Comment #3
gcbWe haven't built a function to directly add users to a MailChimp list without having a mailchimp_list entity in Drupal tied to the list (deliberately). That's why we request a mailchimp_lists entity in this function call.
Try using mailchimp_lists_load_multiple to get a collection of mailchimp_list entities and filter for one that has the mc_list_id matching the list ID you want, then use that mailchimp_list in your call to mailchimp_subscribe_user.