9

I have created my own custom node.tpl.php file. I now want to email the entire node, each time the user creates a new node of that content type (I have made sure that all the HTML in my node.tpl.php file is email friendly).

How can I do this? Ideally I would like the email to go to a specific email address automatically when the node is saved.

I have found that a combination of Rules and HTML Mail can achieve what I want. Except... When creating a rule action, there isn't the option to email the entire node (including the node.tpl.php theming). Rules only gives the option to email out specific node fields (without theming).

Any suggestions would be most appreciated!

20
+150

Here's another approach. (The code is available in this sandbox.)

nodemail.info

name = Nodemail
description = Sends node e-mails.
core = 7.x

nodemail.install'

<?php
function nodemail_enable() {
  $current = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
  $addition = array('nodemail' => 'NodemailMailSystem');
  variable_set('mail_system', array_merge($current, $addition));
}

function nodemail_disable() {
  $mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
  unset($mail_system['nodemail']);
  variable_set('mail_system', $mail_system);
}

nodemail.module

<?php
class NodemailMailSystem extends DefaultMailSystem {
  public function format(array $message) {
    $message['body'] = implode("\n\n", $message['body']);
    $message['body'] = drupal_wrap_mail($message['body']);
    return $message;
  }
}

function nodemail_mail($key, &$message, $params) {
  switch ($key) {
    case 'node_mail':
      $message['headers']['Content-Type'] = 'text/html; charset=UTF-8;';
      $message['subject'] = $params['subject'];
      $message['body'][] = $params['body'];
      break;
  }
}

function nodemail_node_insert($node) {
  if ($node->type == 'mycontenttype') {
    $params['subject'] = 'Node "' . $node->title . '" was created';
    $params['body'] = render(node_view($node));
    $to = variable_get('site_mail', '');
    $from = 'noreply@example.com';
    $lang = language_default();
    drupal_mail('nodemail', 'node_mail', $to, $lang, $params, $from);
  }
}

The install file contents and NodemailMailSystem class are used to allow this module to send html e-mails. The 2 other functions are implementations of hook_mail() and hook_node_insert() that deal with sending the e-mails when a node of mycontenttype is created. One thing you'll have to note is that Drupal will use the node template file of the theme that is being used for node creation pages (or the core node.tpl.php if the theme doesn't have it) for the node view. You may also want to check out the node_view() and drupal_mail() functions that are used here. This whole thing should work with Drupal 7 core functionality (no contributed modules are needed).

  • Thanks! That works except… On my site, the template for a node changes according the taxonomy term. So if the user tags the node with term 1, template 1 is used to render the node. (see drupal.stackexchange.com/questions/23688/… ). When I use your code, only the default node.tpl.php file is used (and not the template that is used to render the node). Is there any way to fix this? (Sorry if I seem like a nuisance for not mentioning this earlier - I thought it would not be an issue because the rendered node does contain the template). – big_smile Mar 9 '12 at 18:28
  • @big_smile The problem is probably what I mentioned in my answer: the admin theme that is being used for node creation. Possible fixes that I can think of currently: 1) use your default theme for content editing/creation (uncheck checkbox in 'admin/appearance') 2) copy the necessary code from template.php and the node templates to the admin theme folder and clear caches 3) create custom templates in your module for the e-mails. – Madis Mar 9 '12 at 21:58
6

You could create a token that was the rendered node, or create a custom Rules action that will send the rendered node.

You want to look at

$build = node_view($node);
$html = render($build);

Updated with code

This code shows how to add a property to all nodes which can can be made accessible in rules. I've created this a module called googletorp

/**
 * Implements hook_entity_property_info_alter().
 */
function googletorp_entity_property_info_alter(&$info) {
  // Add the current user's shopping cart to the site information.
  $info['node']['properties']['rendered_node'] = array(
    'label' => t("Rendered_node"),
    'description' => t('The full rendered node.'),
    'getter callback' => 'googletorp_render_node',
  );
}

/**
 * Return a rendered node as HTML.
 */
function googletorp_render_node($node) {
  return render(node_view($node));
}

The first function is a hook that adds the property to the node, where in a callback is defined to provide the data. The second function is the actual callback that returns the rendered node.

In order for this to work, you need to install the entity tokens module which is a part of the entity API module, but this is required by rules anyways.

  • Thanks - that's exactly what I need. But how do you do that? I have downloaded Rules and Token, but there are no options to send the rendered node. – big_smile Feb 29 '12 at 9:50
  • @big_smile You would have to create this functionality yourself - there are several ways of doing this. – googletorp Feb 29 '12 at 15:13
  • Thanks for taking the time to reply. To be honest, your answers aren't very helpful. If I knew how to create the functionality myself, I wouldn't be asking a question. Obviously, I don't expect you to post the full answer. However, it would be helpful if you could provide links to resources that elaborate on how to perform your suggestions, – big_smile Feb 29 '12 at 19:09
  • 2
    @big_smile So you downvote me because I didn't hand feed you? Have you tried google, have you tried the project homepage? I found extensive Rules docs in 30 secs, I'm sure you can do the same. Don't be lazy. – googletorp Mar 1 '12 at 10:08
  • 1
    I am not lazy and don't expect to be fed. Your answer was very vague and doesn't at all imply that the answer can be found in the Rules doc. In fact, "You would have create this functionality yourself" implies that I would have to produce something that is outside of Rules. This is why I downvoted your answer - it was too vague to be helpful. However, looking at your profile, I can see you are very helpful and based on this I don't think it was your intention to be vague. I did not think the down vote would be very upsetting. I would reverse it if I could. I hope there are no hard feelings. – big_smile Mar 2 '12 at 10:10
1

Another option would be use my module Entity2Text(DRupal 7).

Right now it give a "textexport" token for every View Mode of an entity. This works well for a lot field types but has some problem more complex ones like Address Field.

I am also going to add "htmlexport". If you want to try that out checkout out this branch: http://drupalcode.org/project/entity2text.git/shortlog/refs/heads/7.x-1-htmlexport

You would still have to use Rules and MimeMail(or probably htmlmail mentioned above).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.