Using CiviCRM fields in MailChimp

dewolfe001's picture

I have a Drupal 7.21 / CiviCRM 4.2.2 install.
It uses CiviCRM, MailChimp and Profile2. I need to put the first name and last name from a CiviCRM contact into the MailChimp for the mailouts. What I planned on using to make the linkage is Custom Token. I didn't want to write a custom module to pull this off if Custom Token were available to do almost the same job.

Here is what I built:

<?php
if (module_exists('civicrm')) {
  
$user = user_load(arg(1));
civicrm_initialize(TRUE);

require_once
'api/v2/UFGroup.php';

 
$userID = civicrm_uf_match_id_get( $user->uid );

  
$retrieve = array( contact_id => $userID  );

   require_once
'api/v2/Contact.php';
  
$getContact = civicrm_contact_get( $retrieve );
       
  
$bits = array_shift($getContact);  
   return
$bits['first_name'];
}
?>

My questions:
- Is there an even leaner way to pull this off? That is, get the contact fields into Drupal's install of MailChimp?
- How do I pass the UID reference to the Custom Token so that it knows to look up and produce the relevant CivCRM record?

Thanks in advance,

Mike

Comments

There are really 2 questions

kreynen's picture

There are really 2 questions here...

  1. How to expose token values from CiviCRM to Drupal?
  2. Ways to integrate Drupal/CiviCRM w/ MailChimp?

Unfortunately I think the answers to Q2 will likely make solving the CiviCRM tokens issue a moot point for you. Still something I'd like to find a good solution for. I spent some time working with tokens before updating http://drupal.org/project/civicrm_realname. If it turns out there is a reason to manage MailChimp from the Drupal side of your configuration, you might want to look at the function to convert Drupal username to CiviCRM Contact name...

http://drupalcode.org/project/civicrm_realname.git/blob/refs/heads/7.x-1...

Similar logic to what you wrote, but using version 3 of the api. CiviCRM Realname automatically replaces the Drupal username anywhere hook_username_alter() is used.

For Q2, I'd like to know why you're trying to integration MailChimp on the Drupal site? Why not manage that in CiviCRM where all you contact values would be available? Have you looked at...

http://blog.mailchimp.com/mailchimp-integrates-with-nonprofit-software-a...
http://civicrm.org/extensions/mandrill-transactional-emails

CiviCRM

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: