E-mail recipients from ORM and Swiftmailer

Sliverstripe 4.11.
I get some e-mail adresses with ORM and I want to use them as recipients to send an email with Swiftmailer. But whatever I did Swiftmailer never accepts my solution and I get “Address in mailbox given [example1@domain.com,example2@domain.com,example3@domain.com] does not comply with RFC 2822, 3.6.2”
My code looks like this.
foreach ($Verein as $V) {
$Mails.= $V->Verein->MailAdresse.",";


I try to create a variable that looks like it should if I would just write the adresses into the “setTo”. When I have just one result with the ORM it works. More than one and I get the error I mentioned above.
I tried several things but ran out of ideas. Maybe someone has a solution for me.

If you are adding multiple addresses, you need to pass them as an array (SilverStripe\Control\Email\Email | SilverStripe API)

Currently, you’re building a string and passing that as a single recipient. Something along the following lines should work:

foreach ($Verein as $V) {
  $Mails[] = $V->Verein->MailAdresse;

It would probably be worth adding a bit of checking if you don’t already have it, to make sure you don’t pass an empty set of recipients. You could also even do without the foreach loop if you felt like it:


if ($Verein->count() > 0) {

Hi Tim,

thank you very much for your help. I tried your solution before and it didn’t work. I’m sure I made a mistake somewhere and didn’t realize it. I tried it again after I wrote this post and it worked. That was a surprise.

I didn’t know that I could do that without the foreach. So I learned something.

I appreciate the time you invested answering to my post.