I'm having an issue where google keeps bouncing my emails coming from a Drupal site.

This is the reported error:
Messages with multiple addresses in From: 550 5.7.1 header are not accepted.

This is coming from a password reset email and it's simply using Drupal's built-in mail management.

The problem turns out out to be that the site name has a comma in it and google's mail server is reading the From address in the header as multiple addresses.

e.g.
From: Site Name, Inc. <admin@example.com>

I made a fix for this that involves adding quotes around the name so that any commas within are treated as text.

Comments

kleinmp created an issue. See original summary.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

echoz’s picture

Thanks, this confirmed the same problem for me of email sent to gmail from a site name with an apostrophe.

sjerdo’s picture

Status: Active » Needs review
StatusFileSize
new2.03 KB
new2.02 KB

This error not only seems to occur with commas. When a site name contains ", ' or < followed by >, it also isn't allowed.
We should add quotes around the name and escape double quotes.

I have added a patch which escapes double quotes. I have also updated the test.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jody lynn’s picture

StatusFileSize
new1.69 KB

Rerolled patch as it no longer applies on 8.4

Status: Needs review » Needs work

The last submitted patch, 6: from-email-2898750-6.patch, failed testing. View results

msankhala’s picture

Status: Needs work » Needs review
StatusFileSize
new1.71 KB

Patch rerolled.

Status: Needs review » Needs work

The last submitted patch, 8: email-from-header-2898750-8.patch, failed testing. View results

longwave’s picture

I am not sure this is the right fix, as shown by the tests this would appear to corrupt MIME encoded headers - according to the spec it looks like these should not be quoted. http://www.rfc-editor.org/rfc/rfc2047.txt says

+ An 'encoded-word' MUST NOT appear within a 'quoted-string'.

We can either special-case the result of Unicode::mimeHeaderEncode when it comes back unchanged but the string still contains unwanted characters, or maybe Unicode::mimeHeaderEncode needs extending to consider encoding the unwanted characters as well. Right now it assumes the entire ASCII set is valid.

allella’s picture

This bug got me on two D8 installs with commas in the sitename.

#10 / longwave made the point of where to fix the issue.

1) Within Unicode::mimeHeaderEncode()
or
2) Within MailManager doMail()

RFC2047 is focused on "textual header information in character sets other than US-ASCII" and the MIME standards all seem to address non-ASCII formats so tampering with mimeHeaderEncode() seems inappropriate since the method and the RFC it references are geared to non-ASCII. So, this would mean a fix that involves doMail().

Should we do a variation of longwave's suggestion

$encoded_site_name = Unicode::mimeHeaderEncode($site_config->get('name'), TRUE);

// if the encoded site name contains only ASCII characters
if (preg_match('/[^\\x20-\\x7E]/', $encoded_site_name)) {
    // wrap the ASCII in double-quotes to account for special ASCII characters, like commmas
    $encoded_site_name = '"' . addcslashes($encoded_site_name, '"') . '"';
}

$headers['From'] =  $encoded_site_name. ' <' . $site_mail . '>';

or a variation

$encoded_site_name = Unicode::mimeHeaderEncode($site_config->get('name'), TRUE);

// if the encoded site name matches the original value then it contains only ASCII characters
if ( $site_config->get('name') == $encoded_site_name)
{
    // wrap the ASCII in double-quotes to account for special ASCII characters, like commas
    $encoded_site_name = '"' . addcslashes($encoded_site_name, '"') . '"';
}

$headers['From'] =  $encoded_site_name. ' <' . $site_mail . '>';

This also begs the question of if the $reply variable in doMail() also needs to get the same treatment as the From because Reply-To also allows for a name portion before the email address.

allella’s picture

allella’s picture

Status: Needs work » Closed (duplicate)

This issue isn't limited to just Google/Gmail. Any RFC valid email receiver would have the issue.

Closing this as a duplicate since more recent conversations were had on an issue that's tagged as Major and "mail system"
https://www.drupal.org/project/drupal/issues/2745039