Render a node as plain text for sending by mail 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-&gt;getType();
  if ($type === 'foo') {
    $to = 'test@examples.com';

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

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

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

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

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


If I understood correct $view_builder-&gt;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?
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.