3

I have a contact form with a checkbox "Register to the newsletter". If this checkbox is checked, I want to register the email adress (a field in the contact form) to the Mailchimp list, which works fine with the Mailchimp block.

I want to do this in my contact_form_submit fonction in my module, but I can't find a way... Does anyone know how to do this ?

Thanks.

EDIT: I use this code :

if ($values['newsletter'])
{
    $list = array_shift(mailchimp_get_lists());
    mailchimp_subscribe_user($list, $values['mail'], array());
}

This code put the user in the table "cache_mailchimp_user" but not in the mailchimp list subscribers, even after I run the Drupal cron manually... Any ideas ?

1

Assuming you're using the Mailchimp module, in your submit handler you can use the function mailchimp_subscribe_user($list, $email, $merge_vars, $message = TRUE, $mcapi = NULL) located in the mailchimp.module.

  • Well, I thanked you for your anwser, but it didn't totally solve my problem, as you can see in my edited post ;) So I'm still waiting for a solution, I can't find it ! – Alarid Jan 16 '14 at 12:58
  • You should see the set message appear as soon as you submit the form and the user should be added to the list, at least that's what I remember. Are you sure you're passing the correct values? This issue thread may help: drupal.org/node/1881912 – superdrupal Jan 22 '14 at 5:45
1

Drupal 7, Mailchimp module 3.4

$list_id = '12345asd'; // use mailchimp_get_lists() and look for 'id'
$email = 'some_email@gmail.com';
if ( mailchimp_subscribe (
    $list_id,
    $email,
    array (
        'EMAIL' => $email,
        'FNAME' => $first_name, // fields in your mailchimp list
        'LNAME' => $last_name,
    ),
    false,
    false
)) {
    drupal_set_message ( t('Thank you. From now you will receive our promo materials'));
} else {
    drupal_set_message ( 'Error occurred while adding your email to subscribe list', 'error' );
}

NOTE: If mailchimp will deside that your email is invalid module will only put watchdog error. To grab that error and display you can patch mailchimp.module find function mailchimp_subscribe_process and goto approx line 412. and add drupal_set_messaga or whatewer you want.

Approach without patching module (but it's too hacky)

$list_id = '12345asd'; // use mailchimp_get_lists() and look for 'id'
$email = 'some_email@gmail.com';
if ( mailchimp_subscribe (
    $list_id,
    $email,
    array (
        'EMAIL' => $email,
        'FNAME' => $first_name,
        'LNAME' => $last_name,
    ),
    false,
    false
)) {
    drupal_set_message ( t('Thank you. From now you will receive our promo materials'));
} else {
    // Get last watchdog mailchimp error
    $query = db_select ( 'watchdog', 'w' );
    $query -> fields ( 'w' );
    $query -> condition ( 'w.type', 'mailchimp', '=' );
    $query -> condition ( 'w.timestamp', time() - 10, '>' ); // check for message in last 10 seconds
    $query -> condition ( 'w.message', 'An error occurred subscribing @email to list @list. "%message"', '=' );
    $query -> orderBy ( 'w.timestamp', 'DESC' );
    $query -> range ( 0, 1 );
    $ans = $query -> execute();
    if ( $wd = $ans -> fetchAssoc() ) {
        $wd['variables'] = unserialize ( $wd['variables'] );
        $message = $wd['variables']['%message'];
        // Get only error message
        if ( preg_match ( '#Internal\sServer\sError:(.+)$#si', $message, $m )) {
            $message = $m[1];
        }
        drupal_set_message ( $message, 'error' );
    } else {
        drupal_set_message ( 'Error occurred while adding your email to subscribe list', 'error' );
    }
}
0

You should pass list id to this function as the first parameter. Not the entire list array. Also in D7 this function is named just mailchimp_subscribe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.