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.


gcb’s picture

Status: Active » Closed (works as designed)

Have 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.

stenjo’s picture

Status: Closed (works as designed) » Active

Thank 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?

 * Subscribe a user to a given list.
function mailchimp_subscribe_user($list, $email, $merge_vars, $message = TRUE, $mcapi = NULL) {
  $success = FALSE;
  if ($mcapi || $mcapi = mailchimp_get_api_object()) {
  return $success;

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?

gcb’s picture

Status: Active » Closed (works as designed)

We 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.