How can a While loop be used to send emails to multiple contacts in PHP?
To send emails to multiple contacts in PHP using a While loop, you can first store the list of contacts in an array. Then, iterate through the array using a While loop to send an email to each contact. Inside the loop, you can use the PHP `mail()` function to send the email.
<?php
$contacts = ['contact1@example.com', 'contact2@example.com', 'contact3@example.com'];
$subject = 'Test Email';
$message = 'This is a test email message.';
foreach ($contacts as $contact) {
$headers = 'From: your_email@example.com';
mail($contact, $subject, $message, $headers);
echo "Email sent to: $contact <br>";
}
?>
Related Questions
- How can the limitation of executing header("Location:...") before HTML output be circumvented in PHP?
- How can the use of browser shortcuts like F5 and Ctrl + F5 impact the behavior of PHP forms?
- What are some common pitfalls to avoid when implementing features like clickable map tiles in a browser game using PHP, and how can they be mitigated in Symfony2 development?