I'm getting an extra space in the mail subject when the subject has many characters. One such instance is when receiving the password reset email. It ends up being "Subject: Replacement login information for Some User Name at Some Long Site Name ((two spaces here)Secondary Name Here)".

I traced the extra space in the subject to mimemail.module where drupal_html_to_text is run. I then traced the point where the extra space was being introduced and it is in drupal/includes/mail.inc in function _drupal_wrap_mail_line. As the subject is longer than the 77 characters for the wordwrap call, two extra spaces and a newline are added as a string break character. As it is necessary to rewrite any HTML that might happen to end up in the subject (see: #369983: Plain text Email subject shows & as _amp; and " as _quot; ), the drupal_html_to_text call can't be eliminated.

It seems to me from #438058: Remove line feeds in subject that people were experiencing mail client breakage from the subject newlines, even though RFC 2822 says they should be OK. To fix this, the str_replace call was then added to remove the newlines.

An extra space would still exist in long subjects even after the str_replace. A quick fix is to replace in the mimemail.module for mimemail_prepare_message:
$subject = str_replace("\n", '', trim(drupal_html_to_text($subject)));
with:
$subject = str_replace("\n", '', str_replace(" \n", '', trim(drupal_html_to_text($subject))));

I'm not sure if this has any undesirable side-effects, however. Obviously my change could be refactored to look better as well. After the change, my email subjects look like they should without the extra space.

Files: 
CommentFileSizeAuthor
#1 mimemail-1605230-1.patch634 bytessgabe

Comments

Status:Active» Needs review
StatusFileSize
new634 bytes

There is no need to call str_replace twice, since we can use an array to designate multiple needles. Please test the attached patch and report back.

Status:Needs review» Fixed

Committed to both branches.

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Issue summary:View changes

Added result of change to post.

Just wanted to add to this issue, as I was having a related issue, and am using the newest version of MIME Mail. I too was getting extra spaces in my mail subjects whenever there were more than 60 characters. I traced this back to mimemail.inc and the mimemail_rfc_headers function (line 21). The original code was :

if (drupal_strlen($value) > 60) {
      // If there's a semicolon, use that to separate.
      if (count($array = preg_split('/;\s*/', $value)) > 1) {
        $value = trim(join(";$crlf    ", $array));
      }
      else {
        $value = wordwrap($value, 50, "$crlf    ", FALSE);

In order to fix the extra spaces issue, I took the extra spaces after the $crlf out. The new code is:

if (drupal_strlen($value) > 60) {
      // If there's a semicolon, use that to separate.
      if (count($array = preg_split('/;\s*/', $value)) > 1) {
        $value = trim(join(";$crlf ", $array));
      }
      else {
        $value = wordwrap($value, 50, "$crlf ", FALSE);

And appears to fix my issue of random spaces (4 of them in a row) appearing in my long email subjects. If there are any consequences of doing this, please let me know, but it seems to work great.

@ekidman Since this was a specific issue which has been fixed by a patch, please post your findings in the recently reopened #380334: Extra space in Subject and Reply-to headers, thank you!