# How can I embed a view in an email sent by Rules without resorting to the PHP filter?

I want to embed a view in an email sent by the Rules module using the standard "Send mail" action. I have the Mime Mail and Mail System modules installed, and I can send HTML email when it is included in the body of a rule.

Based on this question, I tried the Insert View module, but that only allows views to be inserted into node content, not rules (which are entities, but Insert View does not support generic entities). I tried adding [view:view_name=display_name] to the message body of my rule, but the view did not appear. To make sure it wasn't working, I added another rule with the "show a message" action and tried to embed the view there, which also didn't work. However, I can place the exact same code inside a node body and the view DOES appear, so it appears that Insert View does not work with Rules.

Entity Views Attachment does support generic entities, but this "attaches" the view, whereas what I am trying to do is embed the view in a rule's email message body like this:

EMBEDDED VIEW

MY TEXT - FOOTER

I also only need to do this for one email, so if possible, I would like to add something like a custom token for Rules to embed the view, but I'm not sure how to go about that.

Using Insert View, Mime Mail and Mail System module, you can send views in emails.
After installing and enabling the above modules, you will need to configure Mail System settings and Text Format settings.

• In the Mail System settings,
Change all settings to use MimeMailSystem class to send mails.
Note: Site-wide default MailSystemInterface class should be MimeMailSystem.

• In the text formats settings,
Enable Insert View Filter in each text format you require.

The problem for you isn't with the rules. It should be the Mail System settings. I hope this would work.

• Could you please tell me more about your Rules/Insert View configuration? I updated my question with details of why I was unable to get Insert View working. – Patrick Kenny Oct 15 '15 at 2:41
• Did you check your text formats settings in the configuration? make sure you have enabled Insert View Filter in the text format you are using to write message body. If its working with your nodes and if its not in the rules, its probably you would be missing some configuration, most probably the text formats. – siddhu151dj Oct 15 '15 at 13:30
• And I assume you have the supported modules like mime_mail already installed. – siddhu151dj Oct 15 '15 at 15:12
• Yes, I enabled Insert VIew Filter for all text formats. However, on the rules message body for the Send Mail action, I don't see any choice about which text format to use. If I put in HTML, the HTML is rendered in the email sent with Mime Mail/Mail System, etc., but the view embedded with Insert View is not. – Patrick Kenny Oct 15 '15 at 16:25
• I have tried using actions Send HTML Email and Show a message. Even they didn't work for me. But its working for me when I use Send mail. If you aren't, try to use Send mail instead of send HTML email action. – siddhu151dj Oct 15 '15 at 17:08

Ultimately, I solved this by using views_embed_view() in the Mime Mail template in my theme directory.

Specifically, I created files like mimemail-message--key.tpl.php for the relevant mail keys and then I added views_embed_view() and other PHP code in the relevant template files.

My site has access-by-term module ABT, (good module to control access to data) this means that if i insert a view in my email, content will be not accesible by anyone not loggedin with certain permissions/roles/abt etc, so my email will be usesless, a blank email. This is what i did:

I installed views rules module, now built a rule to 'build views email data & send it', inside my rule, i had to run 3 rules-views, one main-view to pull UID's to be emailed, other 2 views are nested inside main-view, to acomplish this look here, inside main-view loop i added 2 TEXT empty variables, one for each children-views-rules, having all data accesible stored in tokens sent by views-rules, i built my view row by row with a cutom module using hook_rules_action_info() returning each row already formatted, now in my rule, after each row returned i add an action: set-a-data-value: [my-text-var]=[my-text-var][my-row] (i did this 2 times, var1 & var2), now inside main-view-loop but outside inner views-loops in body content of my email i just added my variables containing all rows [my-text-var1] [my-text-var2], done! works beautifull.

NOTE: i'm using a SMTP mail server, so i can not use MIME mail or similar modules, my email looks like an HTML email, but is just text, be aware of what you can do retunrning \$var[] arrays from rules_custom_action_info()

Use views_embed_view function. Its pretty simple to embed views

• It might help to show an example here. – Shawn Conn Oct 14 '15 at 19:39
• views_embed_view('<views name>', '<display_name>', <contextual filter value>) – Shabir A. Oct 14 '15 at 19:54
• My question asks how to do this without using the PHP filter, so where would I add the function? – Patrick Kenny Oct 15 '15 at 2:25

I have the same situation trying to embed a view in a email and using the action Send mail instead of Send HTML email in rules solved the problem for me aswell.