I have a custom module that sends notifications messages by mail.
Setting is as follows:
"To" remains empty
" Bcc" is a list of comma separated email adresses

Swift Mailer: it works!
Symfony Mailer: ERROR: Email does not comply with addr-spec of RFC 2822.

Command icon 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

gilbertdelyon created an issue. See original summary.

gilbertdelyon’s picture

Issue summary: View changes
adamps’s picture

Priority: Critical » Normal
Status: Active » Postponed (maintainer needs more info)
Issue tags: -Bcc multiple mails does not work

It's not broken for everyone, so we need more info to debug. Please add more info especially about the list of addresses - you can substitute one character for another for confidentiality.

It should not be recommended to move from Swift Mailer to Symfony Mailer for the moment

That's entirely separate from this issue - please can you remove it here and create a new one if you really wish.

gilbertdelyon’s picture

Issue summary: View changes
gilbertdelyon’s picture

It's not broken for everyone, so we need more info to debug. Please add more info especially about the list of addresses - you can substitute one character for another for confidentiality.

Ill do my best, but for the moment I removed Symfony Mailer from my site in favour of the "good old" Swift Mailer, and it works without any change in the code of my custom module.

That's entirely separate from this issue - please can you remove it here and create a new one if you really wish.

You are right. Its removed.

gilbertdelyon’s picture

@AdamPS,
When a new node of a certain type is published my custom module is sending 2 emails:
- mail 1: notification to users
- mail 2: notification to site admin (with lighter content)

In costum module the PHP sequence is as follows:
Email to users:

$to = '';  // remains empty
$notifdata['users2notify'] = array('name1'=>'name1blabla@domain1', 'name2'=>'name2blabla@domain', 'name3'=>'name3blabla@domain3' );
$params['Bcc'] = implode(', ', array_values($notifdata['users2notify']));

Email to site admin:

$to = 'sitename@sitedomain';  
// No Bcc;

In SwiftMailer:
Mail to users: It works!
Mail to site admin: it works !
So far so good!

In SymfonyMailer:
Mail to users: Mail not sent. Error sending email: Email "" does not comply with addr-spec of RFC 2822.
Mail to site admi: it works !
I also tested other separators ',' (without space) and ';'. Same result.

Uninstall Symfony Mailer. Reinstall Swift Mailer. It works again !

PHP 8.0.19
Drupal 9.3.13
Symfony Mailer Alpha 9

Please ask for additionnal test if you need, (but not before next monday)

adamps’s picture

Email "" does not comply with addr-spec of RFC 2822.

OK so it an error with an address of "" - an empty address. So perhaps the problem is with the 'To' address which you say "remains empty".

Please can you share more of your code? In particular, the call to ->mail() what do you pass for $to?

gilbertdelyon’s picture

the call to ->mail() what do you pass for $to?

here it is:

.
.
$to = '';
$params['Bcc'] = implode(',', array_values($notifdata['users2notify']));
.
.
.
$mailManager = \Drupal::service('plugin.manager.mail');
$mail= $mailManager->mail($module, $key, $to, $langcode, $params, $reply, $send);
.
.

And again: It works in SwiftMailer

gilbertdelyon’s picture

@AdamPS,
I could perform some more tests:
First test:

$to='validname@validdomain';
$Bcc = 'name1@domain1,name2@domain2,name3@domain3';

it works!
so, you are right. No issue with Bcc, but issue with empty "$to"
and this is not the result I want !

Second test:

$to='';
$Bcc = 'name1@domain1,name2@domain2,name3@domain3';

Doesn't work. Error sending email: Email "" does not comply with addr-spec of RFC 2822.

Third test:

//$to='';
$Bcc = 'name1@domain1,name2@domain2,name3@domain3';

It works, but //to=""; should sent to nobody, instead it sends to 'sitemailadress@sitedomain'

So, the question is: With Symfony Mailer what is the value for $to=?? if I want the mail to be sent only to "Bcc" ??

gilbertdelyon’s picture

Issue tags: +Error when sending an email with empty
gilbertdelyon’s picture

Title: Bcc multiple mails does not work » Error when sending an mail with empty "To" and multiple "Bcc"
Issue tags: -Error when sending an email with empty
adamps’s picture

Title: Error when sending an mail with empty "To" and multiple "Bcc" » Allow sending back-compatibility email with empty "To"
Version: 1.0.0-alpha9 » 1.x-dev
Component: Miscellaneous » Code
Status: Postponed (maintainer needs more info) » Active

I guess that the problem is with empty "To" and not related to the multiple "Bcc".

The documentation for MailManagerInterface and MailInterface do not mention that $to can be an empty string. You have tested it successfully with Swift Mailer however it might fail with other mailer implementations such as PhpMail.

lamp5’s picture

After latest updates I have also problems with all of my custom mails. Example:

    \Drupal::service('plugin.manager.mail')
      ->mail('app', 'invite_confirmation', $owner->getEmail(), $owner->getPreferredLangcode(), [
        'affiliate_campaign' => $campaign,
        'user' => $entity,
      ], FALSE, TRUE);

$to is filled but I can see the same error message.

adamps’s picture

@lamp5 If your $to is filled in then maybe it's not the same issue? Please can you post the actual value? You can swap some characters for confidentiality. We need a clear example that is a valid email yet causes an error so other people can reproduce.

lamp5’s picture

Ok I found where is the problem right now:

$mailManager->mail($module, $key, $to, $langcode, $params, $reply, $send);

I set $reply to FALSE, it should be NULL or email address.

ippy’s picture

I wanted to say a quick thanks to @lamp5 for posting c15 as it provided an invaluable clue.
[NB This is not about empty "to" - apologies if it feels like a hijack, but I'm not sure where else to note it]

I spent a long time picking apart a similar scenario with some inherited code during a D9 upgrade (BC and coming from Swift, creating and queuing messages for batch processing), wondering why an apparently valid "to" value was returning "does not comply with addr-spec of RFC 2822."

In this case it turned out that \Drupal::service('plugin.manager.mail') was missing a $reply value entirely. I added a default reply address and, mmm, unsurprisingly perhaps, mail is created and sent as expected.

adamps’s picture

Thanks for the updates in #15/#16. It's a shame that the error message (which comes from the library code) does not indicate which header is causing the problem. Possibly we could try to catch the error and throw a clearer one, but it would be better to raise an issue upstream.

This email remains open for empty To address. I feel it's unclear whether that is allowed or not. However I would accept a patch which presumably would only be adding one if.

adel-by made their first commit to this issue’s fork.

adel-by’s picture

In my case it was content_moderation_notifications module that sometimes sends an empty "To" to the MailManagerReplacement service.
I added a simple check to replace the empty string with null, and the email was sent as expected.
I don't know if this belongs here or content_moderation_notifications but here is the patch.

adamps’s picture

Status: Active » Fixed

  • AdamPS committed 950fd65 on 1.x authored by adel-by
    Issue #3281130 by adel-by, gilbertdelyon, AdamPS, lamp5: Allow sending...
adamps’s picture

Great thanks

Status: Fixed » Closed (fixed)

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