I know that title's fairly vague but I'm not sure of how many ways there are to acheive this so I didn't want to be too specific.

I have had a play with the following modules but I'm still a little stumped:

Insert View
Viewfield
Rules and Views Integration (with Views periodic execution)

I realise that I may need the Mime Mail module in order to send the View as HTML.

I've seen other issues along the same lines as this but they assume a certain grounding that I seem to be lacking (particularly regarding Rules and Views Integration).

Any help (small prompts right through to full-on how-to guides) would be greatly appreciated.

Comments

bshensky’s picture

Subscribing. I'm in a similar situation, so I'll be happy to share what I uncover.

itangalo’s picture

Status: Active » Fixed

I don't think there's a module that does this, so accomplishing it will require some coding.

There are basically two approaches to doing this:

1) Use PHP and embed the view in the e-mail message directly in the Rules configuration. This is *not* recommended, since storing PHP in your database is bad for several reasons.

2) Write a small Rules plugin, that allows you to load the content of the desired view as a string. (Or even to load an arbitrary view, which would be a neat plugin to contribute to the community.)

Both these approaches would make heavy use of the Views API, in a way similar to this:

if (module_exists('views')) {
  $view = views_get_view('my_view_name'); // Get the Views object.
  $view->set_display('my_display_name'); // Skip this and you will use the default display.
  $view->set_arguments(array(1, 2, 3)); // Skip this to don't use any arguments.
  $view->execute();
  if (isset($view->result)) {
    // Do some stuff with the results. You probably want the rendered output for each row, but I don't know
    // how to get it on top of my head. Sorry. :-/
  }
}

NOTE: The Views module must be loaded to make use of the functions above – unless the module_exists() function loads Views automatically.

A good place to read more about Views API is drupalcontrib.org – for exmple http://drupalcontrib.org/api/drupal/contributions--views--views.module/f....

I hope this can be of some use to get started. Good luck,
//Johan Falk

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

bshensky’s picture

I said I'd follow up. Here you go! Here is the skelton of the formula I have put together to have a view sent nightly to all users of a role nightly. It uses Rules, Rule Sets, Rules Scheduler and Views to do its magic. You will have to salt and pepper it to your situation, but the fundamentals are there.

For nightly reporting:

  • Requirements
    • Modules
      • Rules (rules)
      • Rules Scheduler (rules_periodic_execution)
      • Rules & Views Integration (rules_views)
      • Views (views)
    • You will want to theme the view to be executed in accordance with the email delivery format. If you are using standard plaintext email, you might want also want to install views_bonus and leverage a "Feed" tab that would yield CSV or similar output from the view, free of HTML. Or, you might want to install the various modules that allow you to send HTML or multipart email from Drupal, and the view output can be embedded as it normally is rendered.
  • Rules Sets
    • Rule Set: Nightly Site Admin Email
      • Contains the following Active Rules
        • Label: Requeue for execution tomorrow
          • Do: Schedule "Rule Set: Nightly Site Admin Email"
            • Identifier = "nightly1"
            • Scheduled Evaluation Date = "tomorrow"
            • Weight = "10"
        • Label: Send Report
          • Do: Render a View to a variable
            • View to render = "Defaults"
            • View arguments = "yesterday"
            • Variable rendered view settings
              • Label = "rendered view"
              • Machine readable variable name = "rendered_view"
            • Weight = "-1"
          • Do: Send a mail to all users of a role
            • Recipient roles = "site administrator"
            • Sender = (blank)
            • Subject = "Daily Report Enclosed"
            • Message = "Report for the last day is as follows:\n\n[rendered_view:string]\n\n---End---"
            • Weight = "0"
          • Do: Label: Log to watchdog
            • Severity = "notice"
            • Category = "rules"
            • Message = "I believe I have sent email.  Did I?  [rendered_view:string-raw]"
            • Link = (blank)
            • Weight = "2"


Once this is put in place, you must use the Rules Scheduler UI to schedule the initial run (admin/rules/rule_sets/scheduling).

dddbbb’s picture

@bshensky Wow, thanks for this. I look forward to giving this a try...

pribeh’s picture

subscribe

landing’s picture

subscribe

dreadlocks1221’s picture

Version: 6.x-1.4 » 7.x-2.0

Hi,

I set this up with rules in D7 and when the email sends out, it send 10 copies. Is there a way to fix this?

lomo’s picture

Version: 7.x-2.0 » 6.x-1.4

The existing issue and code, etc, in this is all about Rules 6.x-1.x, not 7.x-2.x. I don't think the version of this "issue" should be changed when someone has a related question (bug). ;-)

tajindersingh’s picture

@bshensky

Thanks for such detailed steps. Really helped.

Just found that Token 1.7 broke Rules integration, but Token 1.18 works fine.

Here sharing just in case someone like me in help come this view so should get confused.

Thanks again.. :)

sorensong’s picture

Do you know of a way to do this in D7? Is "Rules and Views Integration" the missing element for D7 users?

marcusx’s picture

Not nice but working for now. I did it with enabling the php filter and using a small snippet in D7. In the body field:

Hello  [node:field_tn_vorname] [node:field_tn_nachname]!

Some text...

Embed some plaintext view data after here:
======================================

<?php 
  $view = views_get_view('VIEWSNAMEHERE' , TRUE);
  $arg[] = $node->nid; //in my use case the views needs an argument, this might not be neccessary
  $views_result = $view->preview('mail_embed', $arg);
  $output = drupal_html_to_text($views_result);
  print $output; 
?>

regards
...

mrpeanut’s picture

@marcusx — Just wanted to thank you for posting that code. Hours of frustration solved by your comment. I took out a few lines, but it is essentially the same:

  $view = views_get_view('weekly_email' , TRUE);
  $views_result = $view->preview('mail_embed');
  $output = drupal_html_to_text($views_result);
  print $output;
memcinto’s picture

bshensky: Wonderful - thank you. Saved me hours of work. FYI if you are trying to send HTML email via Mime Mail, you'll need to use [rendered_view:string-raw] instead of [rendered_view:string]. Thanks again.

mitchell’s picture

Component: Provided Rules integration » Rules Core

Updated component.

rsutaria’s picture

For Drupal 6.x, you can try this module:

http://drupal.org/project/sunmailer

aidanlis’s picture

For Drupal 7, you can use http://drupal.org/project/mailaview

micnap’s picture

Triggered by a rule? I'm not seeing any rules integration in mailaview.

Mickey

dkhill’s picture

@bshensky

I came back to look at this thread looking for a solution for a Drupal 6 web site. Thanks for providing this snippet of information. I'm in the process of putting together the pieces. I came across where you mention "rules_periodic_execution" module, but I couldn't find it. When I installed the "Rules & Views Integration" (rules_views) module it mentioned the dependency as "Views_periodic_execution". That one exists. Assuming this is what you meant, I went ahead and installed that module.

I'll post my findings here.

-d