Skip to content
Snippets Groups Projects
Commit 27637ac5 authored by Angie Byron's avatar Angie Byron
Browse files

Issue #1328696 by Tor Arne Thune, wojtha, MaxMendez: Fixed Problem with...

Issue #1328696 by Tor Arne Thune, wojtha, MaxMendez: Fixed Problem with _drupal_wrap_mail_line() and attachment files.
parent e970ca8f
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -520,10 +520,36 @@ function drupal_html_to_text($string, $allowed_tags = NULL) {
* Wraps words on a single line.
*
* Callback for array_walk() winthin drupal_wrap_mail().
*
* Note that we are skipping MIME content header lines, because attached files,
* especially applications, could have long MIME types or long filenames which
* result in line length longer than the 77 characters limit and wrapping that
* line will break the e-mail format. E.g., the attached file hello_drupal.docx
* will produce the following Content-Type:
* Content-Type:
* application/vnd.openxmlformats-officedocument.wordprocessingml.document;
* name="hello_drupal.docx"
*/
function _drupal_wrap_mail_line(&$line, $key, $values) {
// Use soft-breaks only for purely quoted or unindented text.
$line = wordwrap($line, 77 - $values['length'], $values['soft'] ? " \n" : "\n");
$line_is_mime_header = FALSE;
$mime_headers = array(
'Content-Type',
'Content-Transfer-Encoding',
'Content-Disposition',
'Content-Description',
);
// Do not break MIME headers which could be longer than 77 characters.
foreach ($mime_headers as $header) {
if (strpos($line, $header . ': ') === 0) {
$line_is_mime_header = TRUE;
break;
}
}
if (!$line_is_mime_header) {
// Use soft-breaks only for purely quoted or unindented text.
$line = wordwrap($line, 77 - $values['length'], $values['soft'] ? " \n" : "\n");
}
// Break really long words at the maximum width allowed.
$line = wordwrap($line, 996 - $values['length'], $values['soft'] ? " \n" : "\n");
}
......
<?php
/**
* @file
* Definition of Drupal\system\Tests\Mail\WrapMailUnitTest.
*/
namespace Drupal\system\Tests\Mail;
use Drupal\simpletest\UnitTestBase;
/**
* Tests the functionality of drupal_wrap_mail().
*/
class WrapMailUnitTest extends UnitTestBase {
public static function getInfo() {
return array(
'name' => 'Mail wrapping',
'description' => 'Tests drupal_wrap_mail().',
'group' => 'Mail',
);
}
/**
* Makes sure that drupal_wrap_mail() wraps the correct types of lines.
*/
function testDrupalWrapMail() {
$delimiter = "End of header\n";
$long_file_name = $this->randomName(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->randomName(64);
$body = $this->randomName(76) . ' ' . $this->randomName(6);
$wrapped_text = drupal_wrap_mail($headers_in_body . $delimiter . $body);
list($processed_headers, $processed_body) = explode($delimiter, $wrapped_text);
// Check that the body headers were not wrapped even though some exceeded
// 77 characters.
$this->assertEqual($headers_in_body, $processed_headers, 'Headers in the body are not wrapped.');
// Check that the body text is wrapped.
$this->assertEqual(wordwrap($body, 77, " \n"), $processed_body, 'Body text is wrapped.');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment