Problem/Motivation
In Legacy Mode using a comma in a display name will cause an error.
Error sending email: Email ""Lastname" does not comply with addr-spec of RFC 2822.
Steps to reproduce
Try sending a mail to a recipient with a display name containing a comma.
Drupal::service('plugin.manager.mail')->mail('webform', 'key', '"Lastname, Firstname" <firstname.lastname@example.com>', 'en');
Proposed resolution
MailHelper splits address parts using explode. This should be improved by matching comma seperated parts taking quoted strings into account.
| Comment | File | Size | Author |
|---|---|---|---|
| #38 | symfony_mailer-support_complex_address_strings-3350992-38.patch | 1.03 KB | weseze |
Issue fork symfony_mailer-3350992
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
adamps commentedYes you are right. It is a known and documented restriction on
MailerHelperInterface::parseAddress(), so I would prefer to call it a feature request😃.Since #3254085: Sending mails to multiple email addresses does not work via BC, nearly a year ago, I've changed my views - back-compatibility mode is likely to stay around for many years, even "forever". So yes I am happy to fix this. We should use library code rather than re-inventing. I wonder about
Mail_RFC822in package pear/mail, see docs.Thanks very much for the test. Setting to "Needs Review" so we can see that it fails😃.
Comment #3
lmoeniIn the meantime this patch solves the issue.
Comment #4
adamps commentedAlso see
swiftmailer_parse_mailboxes(), which is even more complex and yet still probably not fully correct or complete with the evolving standards.Thanks the patch is useful for anyone who wants a solution in the meantime. I don't feel it's right to commit it, as it seems better to use library code.
Comment #5
znerol commentedSee also core fix in #3226117: Uncaught RfcComplianceException when email From name contains a comma.
Comment #6
adamps commentedIn my testing of splitting addresses, str_getcsv() is an improvement on explode() however it's not 100%:
- it works if the display name contains a comma
- if fails if the display name contains a less-than (because it removes the quotes within each address)
Comment #7
drcolossos commentedWe had the same issue and the patch fixed the problem. As noted in #5, the str_getcsv() has an issue with "," in an address.
Comment #8
adamps commentedLet's just take the Core fix
It goes in
ImportHelper::parseAddress()in place ofexplode(',', $encoded).Comment #11
koustav_mondal commentedComment #12
koustav_mondal commentedPlease Review changes in new MR.
Comment #13
ankitv18 commentedLooks Good, hence moving into RTBC
Comment #14
adamps commentedThanks. The code in Drupal Core has recently changed to support PHP 8.4. Please can we do the same here?
Comment #15
koustav_mondal commentedComment #17
adwivedi008 commentedHello @adamps
I have implemented the suggestion provided for extending support to PHP 8.4.
Please review and suggest any other changes is required
Moving the issue to Needs Review
Comment #18
prudloff commentedTests are failing: https://git.drupalcode.org/project/symfony_mailer/-/merge_requests/140/p...
Also the MR should probably include the new unit test from the other branch: https://git.drupalcode.org/issue/symfony_mailer-3350992/-/compare/2.x......
Comment #21
koustav_mondal commentedHello @adamps, I've made the changes according to #14 . Please have a look and tell me if any other changes are required.
Comment #22
koustav_mondal commentedMoving it to Needs Review.
Comment #23
adamps commentedThanks the code looks good. I'm confused about the test - it looks like there was a commit that added tests then they were later removed. Please can you check?
MailerHelperis now calledImportHelperin 2.x. The constructor needs only one argument.Comment #24
sayan_k_dutta commentedLooking into it.
Comment #25
sayan_k_dutta commented@adamps, resolved the merge error and added the unit test after fixing the issue. Please review MR 140.
Comment #26
graber commentedJust fixed a resulting issue locally in a EmailAdjuster plugin.. the scenario is that Webform uses tokens in its addresses and sometimes there can be a comma in entity label / field. An email is inited with Drupal\symfony_mailer\Plugin\EmailBuilder\LegacyEmailBuilder setting from_name with a comma and that gets mapped to "from" address in webform_mail. Not even 100% sure where this should be solved but probably here or in a follow-up as those commas will definitely not going to be ever escaped anywhere.
I think comma-separated strings should never be treated as multiple addresses and an array should always be used for strict behavior.
Comment #27
adamps commentedThanks. Unfortunately when I try to merge I got an error.
Rebase failed: Rebase locally, resolve all conflicts, then push the branch.
Merging failed.
The conflict is with #3515427: Trim trailing comma and space in the "to" string which changed the same function. We should make sure that we don't lose the fix from there. Probably we can just copy in the 3 lines starting from:
if (empty(trim($part))) {Comment #28
adamps commentedI entirely agree - and if you use the new API
EmailInterfacethe it works like that. Unfortunately with LegacyEmailBuilder we are stuck with BC - otherwise old code doesn't work which defeats the whole point.Comment #29
sayan_k_dutta commentedResolved the conflict.
Comment #30
adamps commentedThanks. I'm sorry to bother you again but there is now another conflict.
Comment #31
sayan_k_dutta commentedResolved merge conflict and fixed pipeline.
Comment #32
adamps commentedThanks you accidentally removed a space - I suggested a change in the MR to put it back
Comment #33
graber commentedNW over a space? 😂
Can't believe..
Just commit the suggestion and continue reviewing!
Comment #34
adamps commentedD'oh hadn't occurred to me that I could commit my own suggestion😃
Comment #36
adamps commentedThanks everyone
Comment #37
adamps commentedComment #38
weseze commentedI see this was marked as fixed.
We were using the patch from #3 against the 1.5.x branch, which worked for us.
Since 1.6 was released and, as far as I can tell, does not contain a fix for this, and the patch from #3 does not apply anymore, attached is an updated patch against 1.6.
Comment #40
imclean commentedToo late for this issue, but this looks like it could be a useful library for parsing a string of email addresses: https://packagist.org/packages/mmucklo/email-parse (github repository).
Comment #41
scottsawyer@weseze thank you for the patch, we had the exact same experience and your patch got emails sending again.