mailchimp campaign

Creating Daily Newsletter from Views Output using Mailchimp Campaign.

MailChimp is a popular email delivery service functionality of create campaing for drupal site.

but it doesnt support with views output.

This guide will provide how to create automatic mailchimp campaign(Newsletter) form views output on daily basis and send to user who associacted with this campaign.


NOTE: Please follow the comments next to the code lines.


-----Implement hook_cron-----

So that this action execute periodically


function hook_cron() {

  $options = array(

    'subject' => 'Sitename Daily Newslatter',

    'title' => 'title', /* title of email */

    'list_id' =>  '0350f1354', // Change this to match list id from

    'from_email' => variable_get('site_mail'), /* site email id */

    'from_name' => 'from name',/* user name or sitename who send newslatter */

    'template_id' => '',


  $type = 'regular';

  $mailchimp_object = mailchimp_get_api_object(); /* create object of mailchimp api */


  /* Now get results of view */  

  $views_result = views_get_view_result('view_name', 'block'); /* provide argument view machine name and type of view */


  // Check to prevent sending empty newsletter

  if (empty($views_result)) {

    watchdog('Daily Newslatter', 'Find No content to send for today');




  $viewresult  = "views_embed_view('view_name', 'block');"; /* provide argument view machine name and type of view */ 


  /* Use filter type display suite code so put your result between   <?php //result// ?> */  


  $result = "<?php  

  $"."result" ." = $viewresult". "

  return $"."result; ?>";


  /* create array of template which will store the structure what should be seen in your mail. */

  $template['html']['value'] = $result; /* content result display in campaign */

  $template['html']['format'] = 'ds_code'; /* specifiy display Suite code filter type */

  $content = mailchimp_campaign_render_template($template);


  /* provide various option set based on your nedds */

  $options += array(

    'generate_text' => TRUE,

    'tracking' => array(

      'opens' => TRUE,

      'html_clicks' => TRUE,

      'text_clicks' => TRUE,




  /*  Now call mailchimp api campaignCreate for create mailchimp campaign */

  $campaign_id = $mailchimp_object->campaignCreate($type, $options, $content);

  watchdog('Daily Newslatter', 'Campaign succesfully created with campaign id !campaign_id', array('!campaign_id' => $campaign_id));


  /* Note if campaing_id is not generate means your campaing not succesfully */ 


  /* Call mailchimp api campaignSendNow for sending created mailchimp campaign to users */  

  $result = $mailchimp_object->campaignSendNow($campaign_id); 

  watchdog('Daily Newslatter', 'Campaign succesfully created');



Hope this will be helpful.