What are the potential pitfalls of using the setTo method in a foreach loop with Swift Mailer?

Using the setTo method in a foreach loop with Swift Mailer can potentially overwrite the recipient address with each iteration, resulting in only the last recipient receiving the email. To solve this issue, you can create a new message instance inside the loop and set the recipient address for each iteration.

// Create a new message instance inside the foreach loop and set the recipient address for each iteration
foreach ($recipients as $recipient) {
    $message = (new Swift_Message())
        ->setSubject('Your subject here')
        ->setFrom(['your_email@example.com' => 'Your Name'])
        ->setTo([$recipient])
        ->setBody('Your message here');

    $result = $mailer->send($message);
}