3

I'm trying to send a rendered node (which contains multiple entity references) via email for a newsletter.

I'm using workbench moderation and workbench email to manage the different states I need and mailsystem and mimemail to format my emails.

In my module, I added an event subscriber in which I use the onTransition function of workbench email to detect the state of my node and send a mail if need be.

In CustomNewsletterEventSubscriber.php:

public function onTransition(WorkbenchModerationTransitionEvent $event) {
  $o_entity = $event->getEntity();
  $s_entity_type = $o_entity->getEntityTypeId();

  switch ($event->getStateAfter()) {
    case 'testmailsent':
      $view_builder = \Drupal::entityManager()->getViewBuilder($s_entity_type);
      $full_output = $view_builder->view($o_entity );
      $html = \Drupal::service('renderer')->renderRoot($full_output );

      $langcode = \Drupal::config('system.site')->get('langcode');
      $module = 'custom_newsletter';
      $key = 'testmailsent';
      $to = $account->getEmail();
      $reply = '[email protected]';
      $send = TRUE;

      $params['message'] = $html;
      $params['subject'] = 'Message subject';

      $mailManager = \Drupal::service('plugin.manager.mail');
      $mailManager->mail($module, $key, $to, $langcode, $params, $reply, $send);
      break;
  }
}

In custom_newsletter.module:

function custom_newsletter_mail($key, &$message, $params) {
  $headers = array(
    'MIME-Version' => '1.0',
    'Content-Type' => 'text/html; charset=UTF-8;',
    'X-Mailer' => 'Drupal'
  );

  foreach ($headers as $key => $value) {
    $message['headers'][$key] = $value;
  }

  $message['from'] = \Drupal::config('system.site')->get('mail');
  $message['subject'] = $params['subject'];
  $message['body'][] = $params['message'];
}

Doing this I can send my mails in HTML format. My problem is that although it looks the way I want on my site and my newsletter node is rendered with my custom templates, it is not the case in my emails. In my mails the templates used are from the standard classy theme as you can see:

<!-- THEME DEBUG --><!-- THEME HOOK: 'mimemail_messages' --><!-- FILE NAME SUGGESTIONS:
   * mimemail-messages--testmailsent.html.twig
   x mimemail-messages.html.twig
--><!-- BEGIN OUTPUT from 'modules/contrib/mimemail/templates/mimemail-messages.html.twig' --><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        </head>
  <body id="mimemail-body">
    <div id="center">
      <div id="main">


<!-- THEME DEBUG -->
<!-- THEME HOOK: 'node' -->
<!-- FILE NAME SUGGESTIONS:
   * node--26--full.html.twig
   * node--26.html.twig
   * node--newsletter-standard--full.html.twig
   * node--newsletter-standard.html.twig
   * node--full.html.twig
   x node.html.twig
-->
<!-- BEGIN OUTPUT from 'core/themes/classy/templates/content/node.html.twig' -->

My template "node--newsletter-standard.html.twig" exists and is used when I view the page on my site. So it seems that when I render the node in my module, it doesn't check/find other templates and uses the most basic ones... I have tried all sorts of things but I' am quite desperate at this point. Can anyone tell me what I'm doing wrong? Is it in my view builder, my renderer or an ywhere else?

Thanks in advance.

PS: Don't hesitate to ask any informations I might not have given even though I tried to be thorough.
PS2: I'm french so, please excuse any english mistakes I've made :)

Edit and resolution :

Okay so, I resolved the issue by using the renderer inside the hook_mail instead of using it prior to the mail (see teh edited code below). Hope It helps anyone with the same issue.

In CustomNewsletterEventSubscriber.php:

public function onTransition(WorkbenchModerationTransitionEvent $event) {
  $o_entity = $event->getEntity();
  $s_entity_type = $o_entity->getEntityTypeId();

  switch ($event->getStateAfter()) {
    case 'testmailsent':
      $view_builder = \Drupal::entityManager()->getViewBuilder($s_entity_type);
      $full_output = $view_builder->view($o_entity );

      $langcode = \Drupal::config('system.site')->get('langcode');
      $module = 'custom_newsletter';
      $key = 'testmailsent';
      $to = $account->getEmail();

      $params['message'] = $html;
      $params['subject'] = 'Message subject';

      $mailManager = \Drupal::service('plugin.manager.mail');
      $mailManager->mail($module, $key, $to, $langcode, $params);
      break;
  }
}

In custom_newsletter.module:

function custom_newsletter_mail($key, &$message, $params) {
  $headers = array(
    'MIME-Version' => '1.0',
    'Content-Type' => 'text/html; charset=UTF-8;',
    'X-Mailer' => 'Drupal'
  );

  foreach ($headers as $key => $value) {
    $message['headers'][$key] = $value;
  }

  $message['from'] = \Drupal::config('system.site')->get('mail');
  $message['subject'] = $params['subject'];
  $message['body'][] = \Drupal::service('renderer')->render($params['message']);
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.