I use the Contact attach module to send files with the contact module. In general, it works good, but when sending .docx files or files with long names, the email is not received correctly.

Debugging, when I comment out the line 426 on the include/mail.inc I correctly receive the email.

$line = wordwrap($line, 996 - $values['length'], $values['soft'] ? " \n" : "\n");

The function containing that line is _drupal_wrap_mail_line().

/**
 * Wraps words on a single line.
 *
 * Callback for array_walk() within drupal_wrap_mail().
 */
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");

  // Break really long words at the maximum width allowed.
  $line = wordwrap($line, 996 - $values['length'], $values['soft'] ? " \n" : "\n", TRUE);
}

Comments

brianbrarian’s picture

I'm experiencing a similar issue with a custom email form that uses the Upload element module to upload a file to send as an attachment.

Sending a docx file does not work (a .txt file containing hieroglyphics is received), apparently because of the lengthy name for the mime type. In my case, commenting out this line in the _drupal_wrap_mail_line function in includes/mail.inc allows the attachment to be sent properly:

$line = wordwrap($line, 77 - $values['length'], $values['soft'] ? "  \n" : "\n");
wojtha’s picture

Version: 6.22 » 6.x-dev

I've run into this issue today. Using Contact Attach like MaxMendez. The docx content type is being split even to three lines!

--b4a452e28855b54e258a93b65aeb7b87
Content-Type:  
application/vnd.openxmlformats-officedocument.wordprocessingml.document;  
name="hello.docx"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="hello.docx"

UEsDBBQABgAIAAAAIQApwh5lnAEAABIGAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
...
--b4a452e28855b54e258a93b65aeb7b87--

It seems that Drupal mail isn't designed to handle multipart messages. I've tried several approaches to fix it, but there is no easy way out of this - except the solution considered by brianbrarian which means we resign on wrapping the text of the e-mail.

wojtha’s picture

Status: Active » Needs review
StatusFileSize
new1.61 KB

Note that I'm using strpos instead of preg_match since the benchmark showed me +36% perfomance gain.

$line = "LKGELKGMLKEMGLEKGMLKENGLEKGN";
$max = 1000000;

$start = microtime(TRUE);
for ($i = 1; $i < $max; ++$i) {
  $line_is_mime_header = strpos($line, 'Content-Type: ') === 0
    || strpos($line, 'Content-Transfer-Encoding: ') === 0
    || strpos($line, 'Content-Disposition: ') === 0
    || strpos($line, 'Content-Description: ') === 0;
}
$strpos = microtime(TRUE) - $start;
dvm($strpos, 'strpos:');

$start = microtime(TRUE);
for ($i = 1; $i < $max; ++$i) {
  preg_match('/^(Content-Type|Content-Transfer-Encoding|Content-Disposition|Content-Description): /', $line);
}
$preg_match = microtime(TRUE) - $start;
dvm($preg_match, 'preg_match:');

dvm(number_format(($preg_match - $strpos) / $preg_match * 100) . '%');
wojtha’s picture

Version: 6.x-dev » 8.x-dev
StatusFileSize
new1.63 KB

Lets fix this in D8 first.

naxoc’s picture

Can anyone give me a hint on how to test this without a contrib module? Is there a way to attach stuff in core?

I agree that this should fixed in core, I just don't know how to test this.

Tor Arne Thune’s picture

Added a unit test for drupal_wrap_mail() to prove that the fix does the trick. There is no way to send mails with attachments from the UI without contrib modules, so this can't be tested easily as a functional test.
Patch without wojtha's fix is supposed to fail.

ryan.ryan’s picture

Assigned: Unassigned » ryan.ryan
ryan.ryan’s picture

Assigned: ryan.ryan » Unassigned
ryan.ryan’s picture

Status: Needs review » Reviewed & tested by the community

Without a contrib module, this can't be fully tested, but the patch and test look good.

Tor Arne Thune’s picture

Great. In that case I'm re-rolling #6 to convert the test to comply with PSR-0, as other system tests in D8 are now converted to comply with PSR-0.

Tor Arne Thune’s picture

webchick’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/includes/mail.incundefined
@@ -513,10 +513,23 @@ function drupal_html_to_text($string, $allowed_tags = NULL) {
+  $line_is_mime_header = strpos($line, 'Content-Type: ') === 0 || strpos($line, 'Content-Transfer-Encoding: ') === 0 || strpos($line, 'Content-Disposition: ') === 0 || strpos($line, 'Content-Description: ') === 0;
+  if (!$line_is_mime_header) {

I wonder if we could re-write this against an array of headers? That would make it easier to modify later if we run across others with problems.

+++ b/core/modules/system/lib/Drupal/system/Tests/Mail/WrapMailUnitTest.phpundefined
@@ -0,0 +1,42 @@
+  function testDrupalWrapMail() {

Just need a quick line of PHPDoc on top of this function.

kenmiller35’s picture

Hi,
Will this be fixed in Drupal 7?

Tor Arne Thune’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new3.65 KB

I wonder if we could re-write this against an array of headers? That would make it easier to modify later if we run across others with problems. -webchick

Sure. It will be less performant but more readable.

Just need a quick line of PHPDoc on top of this function. -webchick

Added.

Tor Arne Thune’s picture

Status: Needs work » Reviewed & tested by the community

Back to webchick for consideration :)

catch’s picture

Assigned: Unassigned » webchick
Status: Needs review » Reviewed & tested by the community

Looks like webchick has this.

webchick’s picture

Version: 8.x-dev » 7.x-dev
Assigned: webchick » Unassigned
Status: Reviewed & tested by the community » Patch (to be ported)

Awesome, thanks!

Committed and pushed to 8.x.

Back to 7.x for porting.

Tor Arne Thune’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new3.48 KB

Great, it's a merry christmas after all!

Here's the D7 version. There shouldn't be any problems in backporting this.

Tor Arne Thune’s picture

Tor Arne Thune’s picture

Status: Needs review » Needs work

The last submitted patch, D7-mail-dont-wrap-mime-lines-1328696-18.patch, failed testing.

Tor Arne Thune’s picture

Status: Needs work » Needs review

Unrelated fail. Can't be because of this patch.

#18: D7-mail-dont-wrap-mime-lines-1328696-18.patch queued for re-testing.

Tor Arne Thune’s picture

Status: Needs review » Needs work

The last submitted patch, 24: D7-mail-dont-wrap-mime-lines-1328696-24.patch, failed testing.

Tor Arne Thune’s picture

Status: Needs work » Needs review
StatusFileSize
new3.47 KB

Fixed test.

  • webchick committed 27637ac on 8.3.x
    Issue #1328696 by Tor Arne Thune, wojtha, MaxMendez: Fixed Problem with...

  • webchick committed 27637ac on 8.3.x
    Issue #1328696 by Tor Arne Thune, wojtha, MaxMendez: Fixed Problem with...

  • webchick committed 27637ac on 8.4.x
    Issue #1328696 by Tor Arne Thune, wojtha, MaxMendez: Fixed Problem with...

  • webchick committed 27637ac on 8.4.x
    Issue #1328696 by Tor Arne Thune, wojtha, MaxMendez: Fixed Problem with...
crystaldawn’s picture

Did anyone bother to put this into D7? Having a patch is ok, but not having to patch D7 is even better. I highly doubt this patch would still be valid for current D7 dev or stable.

izmeez’s picture

Tested and looks like patch in #26 needs re-roll for Drupal 7.77

izmeez’s picture

Attached is patch from comment #26 re-rolled against Drupal 7.77 and since we don't have a use for it maybe someone who does might test it and comment back. Thanks.

crystaldawn’s picture

The re-roll in #33 appears to apply to stock 7.77 on my end.

avpaderno’s picture

Issue summary: View changes

The patch still passes tests on latest Drupal 7.x branch.

avpaderno’s picture

See also #1447236: DefaultMailSystem implements MailSystemInterface::format() incorrectly, which changes the same function changed in this patch. Whichever patch gets committed first, the other one needs to be re-rolled.

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.