Commit a8c1a177 authored by Adam Shepherd's avatar Adam Shepherd Committed by Adam Shepherd
Browse files

Issue #3271797 by AdamPS: Wrap email body

parent b23b1ea7
Loading
Loading
Loading
Loading
+3 −1
Changes for config/install/symfony_mailer.mailer_policy._.yml: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -5,7 +5,9 @@ id: _
configuration:
  mailer_default_headers: {}
  mailer_url_to_absolute: {}
  mailer_html_to_text: {}
  mailer_inline_css: {}
  mailer_wrap_and_convert:
    rewrap: false
    swiftmailer: false
  email_theme:
    theme: _active_fallback
+11 −4
Changes for config/schema/symfony_mailer.schema.yml: 11 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -199,10 +199,6 @@ symfony_mailer.email_adjuster_plugin.mailer_default_headers:
  type: mapping
  label: 'Default headers'

symfony_mailer.email_adjuster_plugin.mailer_html_to_text:
  type: mapping
  label: 'HTML to text'

symfony_mailer.email_adjuster_plugin.mailer_inline_css:
  type: mapping
  label: 'Inline CSS'
@@ -210,3 +206,14 @@ symfony_mailer.email_adjuster_plugin.mailer_inline_css:
symfony_mailer.email_adjuster_plugin.mailer_url_to_absolute:
  type: mapping
  label: 'URL to absolute'

symfony_mailer.email_adjuster_plugin.mailer_wrap_and_convert:
  type: mapping
  label: 'Wrap and convert'
  mapping:
    rewrap:
      type: boolean
      label: 'Rewrap'
    swiftmailer:
      type: boolean
      label: 'Emulate swiftmailer'
+15 −0
Changes for src/MailerHelper.php: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\symfony_mailer\Processor\EmailAdjusterManagerInterface;
use Drupal\symfony_mailer\Processor\EmailBuilderManagerInterface;
use Html2Text\Html2Text;

/**
 * Provides the mailer helper service.
@@ -118,6 +119,20 @@ class MailerHelper implements MailerHelperInterface {
    return $config ?? [];
  }

  /**
   * {@inheritdoc}
   */
  public function htmlToText(string $html) {
    // Convert to plain text.
    // - Core uses MailFormatHelper::htmlToText(). However this is old code
    //   that's not actively maintained there's no need for a Drupal-specific
    //   version of this generic code.
    // - Symfony Mailer library uses league/html-to-markdown. This is a bigger
    //   step away from what's been done in Drupal before, so we won't do that.
    // - Swiftmailer uses html2text/html2text, and that's what we do.
    return (new Html2Text($html))->getText();
  }

  /**
   * {@inheritdoc}
   */
+11 −0
Changes for src/MailerHelperInterface.php: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -45,6 +45,17 @@ interface MailerHelperInterface {
   */
  public function policyFromAddresses(array $addresses);

  /**
   * Transforms an HTML string into plain text.
   *
   * @param string $html
   *   The string to be transformed.
   *
   * @return string
   *   The transformed string.
   */
  public function htmlToText(string $html);

  /**
   * Returns the configuration factory.
   *
+0 −31
Changes for src/Plugin/EmailAdjuster/HtmlToTextEmailAdjuster.php: 0 added lines, 31 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\symfony_mailer\Plugin\EmailAdjuster;

use Drupal\symfony_mailer\Processor\EmailAdjusterBase;
use Drupal\symfony_mailer\EmailInterface;
use Html2Text\Html2Text;

/**
 * Defines the HTML to text Email Adjuster.
 *
 * @EmailAdjuster(
 *   id = "mailer_html_to_text",
 *   label = @Translation("HTML to text"),
 *   description = @Translation("Create a plain text part from the HTML."),
 *   weight = 800,
 * )
 */
class HtmlToTextEmailAdjuster extends EmailAdjusterBase {

  /**
   * {@inheritdoc}
   */
  public function postRender(EmailInterface $email) {
    if (!$email->getTextBody()) {
      // @todo Or maybe use league/html-to-markdown as symfony mailer does.
      $email->setTextBody((new Html2Text($email->getHtmlBody()))->getText());
    }
  }

}
Loading