0

I like to use a node as mail body in Drupal 8. I have written a simple module which works fine except for output formatting:

function foo_notification_node_insert($node) {
  $type = $node->getType();
  if ($type === 'foo') {
    $to = 'test@examples.com';

    // passed to hook_mail
    $params = array(
      "node" => $node
    );

    \Drupal::service('plugin.manager.mail')->mail(
      'foo_notification',
      'notice',
      $to,
      'de',
      $params
    );
  }

function foo_notification_mail($key, &$message, $params) {
    $node = $params["node"];

    // subject
    $message['subject'] = "New node: " . $node->getTitle();

    // render body
    $view_builder = \Drupal::entityTypeManager()->getViewBuilder($node->getEntityTypeId());
    $view = $view_builder->view($node, 'default', 'de');
    $message['body'][] = render($view); 
}

If I understood correct $view_builder->view($node, 'default', 'de'); returns a render array. This one is rendered to HTML by render($view);. Drupal mail service converts HTML in message body to plain text. And that looks ugly.

Rendered mail body looks like:

---------------- My node title -------------------------------------
first field label
               first field value



      second field label
                second field value



      third field label
                third field value contains a longer text which break
at some point and is continued like this. Just for demonstration a
few more lines.
This ist how it looks like if it contains a line break. It's ugly.

The node has multiple fields and I like to use same view for representation on page (HTML) and in mail (plain text) - especially the order and formatting of node fields.

So what's the correct solution?

  • I haven't tried it in 8 but in 7 the MIME Mail module worked really well. There's a dev version for 8. – Darvanen Nov 10 '16 at 22:27
1
+50

To render your node in plain text, you can try one of the following approaches as per your needs:

Approach 1: Remove the tags!

Add new-lines characters where appropriate and remove all tags from the Drupal rendered output (output might be ugly).

Approach 2: Use custom template

Create a custom view mode for your node (may be name it plain) and override node template to generate plain text for the specific view mode.

Approach 3: Send HTML mail

I read your question well and saw that you need to send plain text, but if a cute kitten doesn't die because of it, you can send your mail in HTML if acceptable.

  • 1
    Thanks for your detail led answer showing different approaches. I decided to send it as an HTML mail. – jelhan Nov 15 '16 at 15:52
2

The render engine of Drupal cannot render Node as plain text (at least natively).

I think you have to choice between send the mail in HTML format with is very easy by changing the $message['header'] of your mail or installing Mail system with Swift Mailer (very easy too). Or you have to parse the field value yourself by escaping HTML markups (bad way IMO).

I strongly recommend you to send your mail in HTML format if you want to enjoy the benefits of the drupal render engine, which is made for generate HTML output.

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.