Howto send emails to more recipients

Silverstripe's Email class uses the standard php mail() function for sending emails. Sending an email to one recipient is clear, put the mail adress in the "to" parameter. If you want to send to more than one email adress, you have to pass one string with the adresses comma separated, see php manual.

If you have many recipients or an array of recipients you can do:

    //make an array of email adresses
    $recipients = ['adress1@foo.com', 'adress2@bar.org'];

    //generate string for php's mail function
    $to = join(', ', $recipients);

    $email = Email::create()
        ->setTo($to);

    //do something else

    $email->send();

Instead of looping over the array we can just use php's join() function.

Rate this post