You are here

function mailsystem_wrap_mail in Mail System 6.2

Same name and namespace in other branches
  1. 8.2 html_to_text.inc \mailsystem_wrap_mail()
  2. 7.3 html_to_text.inc \mailsystem_wrap_mail()
  3. 7.2 html_to_text.inc \mailsystem_wrap_mail()

Perform format=flowed soft wrapping for mail (RFC 3676).

We use delsp=yes wrapping, but only break non-spaced languages when absolutely necessary to avoid compatibility issues.

We deliberately use variable_get('mail_line_endings', MAIL_LINE_ENDINGS) rather than "\r\n".

Parameters

$text: The plain text to process.

array $options: (optional) An array containing one or more of the following keys:

  • indent: A string to indent the text with. Only '>' characters are repeated on subsequent wrapped lines. Others are replaced by spaces.
  • max: The maximum length at which to wrap each line. Defaults to 80.
  • stuff: Whether to space-stuff special lines. Defaults to TRUE.
  • hard: Whether to enforce the maximum line length even if no convenient space character is available. Defaults to FALSE.
  • pad: A string to use for padding short lines to 'max' characters. If more than one character, only the last will be repeated.
  • break: The line break sequence to insert. The default is one of the following:

    • "\r\n": Windows, when $text does not contain a space character.
    • "\n": Non-Windows, when $text does not contain a space character.
    • " \r\n": On Windows, when $text contains at least one space.
    • " \n": Non-Windows, when $text contains at least one space.

See also

drupal_mail()

2 calls to mailsystem_wrap_mail()
mailsystem_html_to_text in ./html_to_text.inc
Transform an HTML string into plain text, preserving the structure of the markup. Useful for preparing the body of a node to be sent by e-mail.
_mailsystem_html_to_text in ./html_to_text.inc
Helper function for drupal_html_to_text().

File

./html_to_text.inc, line 37
Copy of drupal_html_to_text improvements from issue #299138.

Code

function mailsystem_wrap_mail($text, array $options = array()) {
  static $defaults;
  if (!isset($defaults)) {
    $defaults = array(
      'indent' => '',
      'pad' => '',
      'pad_repeat' => '',
      'max' => 80,
      'stuff' => TRUE,
      'hard' => FALSE,
      'eol' => variable_get('mail_line_endings', MAIL_LINE_ENDINGS),
    );
  }
  $options += $defaults;
  if (!isset($options['break'])) {

    // Allow soft-wrap spaces only when $text contains at least one space.
    $options['break'] = (strpos($text, ' ') === FALSE ? '' : ' ') . $defaults['eol'];
  }
  $options['wrap'] = $options['max'] - drupal_strlen($options['indent']);
  if ($options['pad']) {
    $options['pad_repeat'] = drupal_substr($options['pad'], -1, 1);
  }

  // The 'clean' indent is applied to all lines after the first one.
  $options['clean'] = _mailsystem_html_to_text_clean($options['indent']);

  // Wrap lines according to RFC 3676.
  $lines = explode($defaults['eol'], $text);
  array_walk($lines, '_mailsystem_wrap_mail_line', $options);

  // Expand the lines array on newly-inserted line breaks.
  $lines = explode($defaults['eol'], implode($defaults['eol'], $lines));

  // Apply indentation, space-stuffing, and padding.
  array_walk($lines, '_mailsystem_indent_mail_line', $options);
  return implode($defaults['eol'], $lines);
}