wrapMail example


class TestHtmlMailCollector extends TestMailCollector {

  /** * {@inheritdoc} */
  public function format(array $message) {
    // Join the body array into one string.     $message['body'] = implode(PHP_EOL, $message['body']);
    // Wrap the mail body for sending.     $message['body'] = MailFormatHelper::wrapMail($message['body']);
    return $message;
  }

}
// Prepare render array.       $render = ['#markup' => Markup::create($html)];

      // Send a test message that mail_cancel_test_mail_alter should cancel.       \Drupal::service('plugin.manager.mail')->mail('mail_html_test', 'render_from_message_param', 'relative_url@example.com', $language_interface->getId()['message' => $render]);
      // Retrieve sent message.       $captured_emails = \Drupal::state()->get('system.test_mail_collector');
      $sent_message = end($captured_emails);

      // Wrap the expected HTML and assert.       $expected_html = MailFormatHelper::wrapMail($expected_html);
      $this->assertSame($expected_html$sent_message['body'], "Asserting that {$attribute} is properly converted for mails.");
    }

    // Test protocol relative URLs.     foreach (['href', 'src'] as $attribute) {
      // Reset the state variable that holds sent messages.       \Drupal::state()->set('system.test_mail_collector', []);

      $html = "<$tag_name $attribute=\"//example.com/protocol-relative\">protocol relative url in mail test</$tag_name>";
      $expected_html = "<$tag_name $attribute=\"//example.com/protocol-relative\">protocol relative url in mail test</$tag_name>";

      
    // characters as per RFC 821.     $this->assertLessThanOrEqual(1000, $maximum_line_length);
  }

  /** * Tests that trailing whitespace is removed before newlines. * * @see \Drupal\Core\Mail\MailFormatHelper::wrapMail() */
  public function testRemoveTrailingWhitespace() {
    $text = "Hi there! \nEarth";
    $mail_lines = explode("\n", MailFormatHelper::wrapMail($text));
    $this->assertNotEquals(" ", substr($mail_lines[0], -1), 'Trailing whitespace removed.');
  }

  /** * Tests that trailing whitespace from Usenet style signatures is not removed. * * RFC 3676 says, "This is a special case; an (optionally quoted or quoted and * stuffed) line consisting of DASH DASH SP is neither fixed nor flowed." * * @see \Drupal\Core\Mail\MailFormatHelper::wrapMail() */
  

  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() */
case '/h2':
            // Pad the line with dashes.             $output = static::htmlToTextPad($output($tagname == '/h1') ? '=' : '-', ' ');
            array_pop($indent);
            // Ensure blank new-line.             $chunk = '';
            break;

          // Horizontal rulers.           case 'hr':
            // Insert immediately.             $output .= static::wrapMail('', implode('', $indent)) . "\n";
            $output = static::htmlToTextPad($output, '-');
            break;

          // Paragraphs and definition lists.           case '/p':
          case '/dl':
            // Ensure blank new-line.             $chunk = '';
            break;
        }
      }
      
/** * @covers ::wrapMail */
  public function testWrapMail() {
    $delimiter = "End of header\n";
    $long_file_name = $this->randomMachineName(64) . '.docx';
    $headers_in_body = 'Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document; name="' . $long_file_name . "\"\n";
    $headers_in_body .= "Content-Transfer-Encoding: base64\n";
    $headers_in_body .= 'Content-Disposition: attachment; filename="' . $long_file_name . "\"\n";
    $headers_in_body .= 'Content-Description: ' . $this->randomMachineName(64);
    $body = $this->randomMachineName(76) . ' ' . $this->randomMachineName(6);
    $wrapped_text = MailFormatHelper::wrapMail($headers_in_body . $delimiter . $body);
    [$processed_headers$processed_body] = explode($delimiter$wrapped_text);

    // Check that the body headers were not wrapped even though some exceeded     // 77 characters.     $this->assertEquals($headers_in_body$processed_headers, 'Headers in the body are not wrapped.');
    // Check that the body text is wrapped.     $this->assertEquals(wordwrap($body, 77, " \n")$processed_body, 'Body text is wrapped.');
  }

}
Home | Imprint | This part of the site doesn't use cookies.