Views

Creating Daily Newsletter from Views Output using Mailchimp Campaign.

MailChimp is a popular email delivery service provider.it 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.

STEPS:

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 mailchimp.com.

    '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');

    drupal_exit();

  }

  

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

For Example on click of Title it should show either Description or an Image.

You can see the example and code See this LINK

To achieve the same in drupal follow the steps.

Create a Content Type.

Title and body will be there default.So you need to add an Image field.

Add some 4-5 Contents for the Content type.

Go to View add create a new View

   -Style: unformatted list and unselect all the items in the style setting.

   -Fields:

    Content: title ( Select Exclude the title From the display)

                 Global: View result counter (Select  Exclude the title From the display)

                Content: Image:   - Select Rewrite output Result 

                                                 - Put this code:

[field_image]

                                    .Select the field from Replacement Pattern to see the appropriate item:

                                    E.g, [counter] - View Result conter

                                            [title] - Title Field

                                            [field_image] - Image Field

                        Click on "Advance" -> Theme: Information. 

                          you can see "Style Output".Copy the Last name among the options to create a file. Also click on Style Output & Copy the code.

                          --Go to sites/all/themes/yourtheme/templates and create a new file with the copied name.

                         --Open the file and paste the code from "Style Output".

                         Just before the Foreach loop starts,add <div id="myaccordion" class="accordion"> and close it after the Foreach ends.

                       Example Code :               <?php if (!empty($title)): ?>

                                                       <h3><?php print $title; ?></h3>

                                                    <?php endif; ?>

                                                    <div id="myaccordion" class="accordion">

                                                      <?php foreach ($rows as $id => $row): ?>

                                                        <?php print $row; ?>

                                                      <?php endforeach; ?>

                                                    </div>

Save the view.

One more thing is that if you are using Bootstrap as a base theme you will get this functionality by default working but if not then download and add these CSS & JS files.

1)bootstrap.min.css

2)bootstrap.min.js

3)http://code.jquery.com/jquery.min.js

It should be working now.