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
Comment #1
brianbrarian commentedI'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:
Comment #2
wojtha commentedI've run into this issue today. Using Contact Attach like MaxMendez. The docx content type is being split even to three lines!
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.
Comment #3
wojtha commentedNote that I'm using strpos instead of preg_match since the benchmark showed me +36% perfomance gain.
Comment #4
wojtha commentedLets fix this in D8 first.
Comment #5
naxoc commentedCan 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.
Comment #6
Tor Arne Thune commentedAdded 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.
Comment #7
ryan.ryan commentedComment #8
ryan.ryan commentedComment #9
ryan.ryan commentedWithout a contrib module, this can't be fully tested, but the patch and test look good.
Comment #10
Tor Arne Thune commentedGreat. 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.
Comment #11
Tor Arne Thune commentedComment #12
webchickI 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.
Just need a quick line of PHPDoc on top of this function.
Comment #13
kenmiller35 commentedHi,
Will this be fixed in Drupal 7?
Comment #14
Tor Arne Thune commentedSure. It will be less performant but more readable.
Added.
Comment #15
Tor Arne Thune commentedBack to webchick for consideration :)
Comment #16
catchLooks like webchick has this.
Comment #17
webchickAwesome, thanks!
Committed and pushed to 8.x.
Back to 7.x for porting.
Comment #18
Tor Arne Thune commentedGreat, it's a merry christmas after all!
Here's the D7 version. There shouldn't be any problems in backporting this.
Comment #19
Tor Arne Thune commented#18: D7-mail-dont-wrap-mime-lines-1328696-18.patch queued for re-testing.
Comment #20
Tor Arne Thune commented#18: D7-mail-dont-wrap-mime-lines-1328696-18.patch queued for re-testing.
Comment #22
Tor Arne Thune commentedUnrelated fail. Can't be because of this patch.
#18: D7-mail-dont-wrap-mime-lines-1328696-18.patch queued for re-testing.
Comment #23
Tor Arne Thune commented18: D7-mail-dont-wrap-mime-lines-1328696-18.patch queued for re-testing.
Comment #24
Tor Arne Thune commentedRe-rolled.
Comment #26
Tor Arne Thune commentedFixed test.
Comment #31
crystaldawn commentedDid 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.
Comment #32
izmeez commentedTested and looks like patch in #26 needs re-roll for Drupal 7.77
Comment #33
izmeez commentedAttached 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.
Comment #34
crystaldawn commentedThe re-roll in #33 appears to apply to stock 7.77 on my end.
Comment #35
avpadernoThe patch still passes tests on latest Drupal 7.x branch.
Comment #36
avpadernoSee 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.