htmlToText example

    $this->drupalLogout();
    user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);

    // Test the auto-reply for form 'foo'.     $email = $this->randomMachineName(32) . '@example.com';
    $subject = $this->randomMachineName(64);
    $this->submitContact($this->randomMachineName(16)$email$subject, 'foo', $this->randomString(128));

    // We are testing the auto-reply, so there should be one email going to the sender.     $captured_emails = $this->getMails(['id' => 'contact_page_autoreply', 'to' => $email]);
    $this->assertCount(1, $captured_emails);
    $this->assertEquals(trim(MailFormatHelper::htmlToText($foo_autoreply))trim($captured_emails[0]['body']));

    // Test the auto-reply for form 'bar'.     $email = $this->randomMachineName(32) . '@example.com';
    $this->submitContact($this->randomMachineName(16)$email$this->randomString(64), 'bar', $this->randomString(128));

    // Auto-reply for form 'bar' should result in one auto-reply email to the sender.     $captured_emails = $this->getMails(['id' => 'contact_page_autoreply', 'to' => $email]);
    $this->assertCount(1, $captured_emails);
    $this->assertEquals(trim(MailFormatHelper::htmlToText($bar_autoreply))trim($captured_emails[0]['body']));

    // Verify that no auto-reply is sent when the auto-reply field is left blank.
'%uid' => $node->getOwnerId(),
      '%url' => $node->toUrl('canonical', ['absolute' => TRUE])->toString(),
      '%node_type' => node_get_type_label($node),
      '%title' => $node->getTitle(),
      '%teaser' => $node->teaser,
      '%body' => $node->body,
    ];
  }
  $subject = strtr($context['subject']$variables);
  $body = strtr($context['message']$variables);
  $message['subject'] .= str_replace(["\r", "\n"], '', $subject);
  $message['body'][] = MailFormatHelper::htmlToText($body);
}

/** * Alter the list of mail backend plugin definitions. * * @param array $info * The mail backend plugin definitions to be altered. * * @see \Drupal\Core\Annotation\Mail * @see \Drupal\Core\Mail\MailManager */

  protected function assertHtmlToText(string $html, string $text, string $message, ?array $allowed_tags = NULL): void {
    preg_match_all('/<([a-z0-6]+)/', mb_strtolower($html)$matches);
    $tested_tags = implode(', ', array_unique($matches[1]));
    $message .= ' (' . $tested_tags . ')';
    $result = MailFormatHelper::htmlToText($html$allowed_tags);
    $this->assertEquals($text$result, Html::escape($message));
  }

  /** * Tests supported tags of \Drupal\Core\Mail\MailFormatHelper::htmlToText(). */
  public function testTags() {
    global $base_path$base_url;
    $tests = [
      // @todo Trailing linefeeds should be trimmed.       '<a href = "https://www.drupal.org">Drupal.org</a>' => "Drupal.org [1]\n\n[1] https://www.drupal.org\n",
      

  public function format(array $message) {
    // Join the body array into one string.     $message['body'] = implode("\n\n", $message['body']);

    // Convert any HTML to plain-text.     $message['body'] = MailFormatHelper::htmlToText($message['body']);
    // Wrap the mail body for sending.     $message['body'] = MailFormatHelper::wrapMail($message['body']);

    return $message;
  }

  /** * Sends an email message. * * @param array $message * A message array, as described in hook_mail_alter(). * * @return bool * TRUE if the mail was successfully accepted, otherwise FALSE. * * @see http://php.net/manual/function.mail.php * @see \Drupal\Core\Mail\MailManagerInterface::mail() */
Home | Imprint | This part of the site doesn't use cookies.