blog-banner

Creating MailChimp Campaign From View Output

  • Drupal Planet
  • E-MAIL MARKETING
  • MAILCHIMP
  • Views

Drupal Mailchimp




MailChimp is a Web service offering platform for e-mail marketing. It is easy to start, use and scale. In short, we can design, send, and track HTML email campaigns/newsletters with simple tools. There are  lot of things that makes us feel that MailChimp is the right choice for our needs. The list goes on with API to sync from inhouse sites/ applications, intuitive reports, integration with existing services like Facebook, Twitter, and Google analytics, ability to create custom forms, etc. Of course we like MailChimp's humor as well.

 

[[{"type":"media","view_mode":"media_original","fid":"98","attributes":{"alt":"","class":"media-image","style":"box-shadow:0 0 0 rgba(0,0,0,0);","typeof":"foaf:Image"}}]]

 

At KnackForge we often use MailChimp for our client's project and the demand for creating different type of campaigns has been always felt. In this post, I would like to share one of the tricks we implemented in a recent client project. As the title say it's about programmatically creating a campaign from view output in Drupal 7.

 

  1. /**
  2.  * Helper function that calls mailchimp API to create campaign
  3.  * of recent active deals.
  4.  */
  5. function kf_mailchimp_create_campaign() {
  6.   if (!isset($_GET['cron_key']) || ($_GET['cron_key'] != 'kQ7kOy4uRgPJd1FX1QQAERPeSYuPjp1qBW65goYcbDQ')) {
  7.     watchdog('kf_mailchimp', 'Invalid cron key !cron_key has been used to create campaign.', array('!cron_key' => $_GET['cron_key']));
  8.     drupal_exit();
  9.   }
  10.   $data['site_url'] = url('', array('absolute' => TRUE));
  11.   $data['site_logo'] = theme_image(array(
  12.     'path' => drupal_get_path('theme', 'knackforge') . '/logo.png',
  13.     'alt' => 'KnackForge',
  14.     'attributes' => array('border' => 0),
  15.   ));
  16.   $options = array(
  17.     'list_id' = $mc_list_id// Change this to match list id from mailchimp.com.
  18.     'from_email' => variable_get('site_mail'),
  19.     'from_name' => 'KnackForge',
  20.     'to_email' => variable_get('site_name')
  21.   );
  22.   $type = 'regular';
  23.   $q = mailchimp_get_api_object()// Make sure a list has been created in your drupal site.
  24.  
  25.   $results = views_get_view_result('deal_mailchimp', 'page');
  26.   // Check to prevent sending empty newsletter
  27.   if (empty($results)) {
  28.     watchdog('kf_mailchimp', 'No active deals to send for today');
  29.     drupal_exit();
  30.   }
  31.   $data['deals'] = views_embed_view('deal_mailchimp', 'page');
  32.  
  33.   $content = array(
  34.     'html' => theme('kf_mailchimp', $data),
  35.   );
  36.   $options['subject'] = t('Newsletter');
  37.   $options['title'] = $options['subject'] . ' - ' . date('r');
  38.   $options['tracking'] = array(
  39.     'opens' => TRUE,
  40.     'html_clicks' => TRUE,
  41.     'text_clicks' => TRUE
  42.   );
  43.   $options['authenticate'] = false;
  44.   $options['analytics'] = array('google'=>'atphga');
  45.   $cid = $q->campaignCreate($type, $options, $content);
  46.   watchdog('kf_mailchimp', 'Created campaign');
  47.   $result = $q->campaignSendNow($cid);
  48.   watchdog('kf_mailchimp', 'campaignSendNow() response !result', array('!result' => '
    '
    . print_r($result, 1) . '

    '

    ));
  49. }

 

Above is the code that we have been using in one of our production sites. It assumes that the MailChimp module has been installed on your Drupal site. Configured with API key to communicate with mailchimp.com and also expects to have a list created from Drupal admin interface. Though this can be used as such without much modification except a few like cron key, MailChimp list id, machine name of view, etc. code has been modified for blog post typos/ bugs expected :-)

The usecase we had to address was little tricky i.e. to send active deals in given location based on user's profile field value. We achieved it by having a single generic list in Drupal site and syncing remote lists using hook_user_presave(). This may not be clear at first read. The gist is, to keep the code simple we have changed it as above. I hope this would help to get started.

Get awesome tech content in your inbox