I had to implement a custom email builder. All works fine if the emails went to one recipient, but when I adding multiple recipients the system throws this error:
"mail@test.com, mail2@test.com" does not comply with addr-spec of RFC 2822"
(that is the error message if there are spaces after the commas; otherwise a different system level message is given)
This is very similar to issue 3254085, "Sending mails to multiple email addresses does not work via BC", but that one was for BC or legacy mode, versus this one here is for a custom email handler (EmailBuilder).
And multiple recipients do in fact work with legacy_mode, but not for a custom EmailBuilder. (Sidenote: I suspect that some other EmailBuilders that ship with this module *may* have the same problem, as only some have constructors that take a MailerHelper class, like LegacyEmailBuilder).
I am writing this up, so that maybe somebody else who has the problem can find the solution faster.
The basic solution was this: you cannot pass the multiple addresses as a string (as we used to with hook_mail), but it has to be an array of "Addresses" (Drupal\symfony_mailer\Address ).
There is already a function in MailerHelper called "parseAddresses" that does exactly that.
So in my case, I called that code from by my EmailBuilder->build() routine to process the recipients [you may be able to do this more upstream in fromArray or createParams, etc. ], and that solved the problem.
What I would suggest for this module, is that this recipient processing should be done automatically as part of implementing an EmailBuilder, maybe as part of the base class (EmailBuilderBase), or possibly using MailerHelperTrait.
Or if that is not an option, at the very least it should state this in the documentation for custom EmailBuilders.
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | symfony_mailer.address-docs.3387961-8.patch | 2.03 KB | adamps |
Comments
Comment #2
hoporr commentedComment #3
adamps commentedIt should already work as you request. Please give the code you had that wasn't working.
Comment #4
hoporr commentedBelow is the code that ended up working [ where MODULENAME is the name of the module ]
It is called like this:
$mailManager = \Drupal::service('plugin.manager.mail');
$result = $mailManager->mail('MYMODULE', $template_key, $recipient, $langcode, $params, $scaf_from_email, TRUE);
where $recipient failed if it was something like "a@a.com, b@b.com"
I had to insert the lines in the build() function
;
Here is the whole code:
Comment #5
adamps commentedThanks I understand now.
Some background on addresses:
Symfony\Component\Mime\Address.php) - this contains information in structured format suitable for use in code or from configuration.Here is how this module currently works:
So basically you are trying to put encoded data where a class is expected. Please can you explain where your address string "a@a.com, b@b.com" comes from? That code should instead be using an Address.
parseAddress()has this commentComment #6
adamps commentedComment #7
hoporr commentedIt is built explicitly:
This is how it worked with swiftmailer, mailsystem and hook_mail, and the comment above parseAddress() basically states that.
I certainly can move the Address part up to the caller.
However, my point was, when I ported the code this was not documented. If it had not been for that patch in the issue about the legacy mailer, I would not have found this. Other people going through this process may face the same issue.
For that reason, it should be at least documented somewhere (like in the "how to port from swiftmailer" article), or even better, catch this somehow in the baseclass so that it is transparent.
Comment #8
adamps commentedOK then you totally misunderstood what I was trying to explain with that comment😃.
Sure, I'm willing to take a little time to make things clearer. I've added a table row in https://www.drupal.org/docs/contributed-modules/symfony-mailer-0/develop..., and here is a patch that updates the comments.
Comment #9
adamps commentedComment #10
hoporr commentedcomments make it clearer. Reviewed and tested by me. Thank you.
Comment #11
adamps commentedThanks