What best practices should be followed when handling multiple email recipients based on user input in PHP?
When handling multiple email recipients based on user input in PHP, it is important to sanitize and validate the input to prevent any potential security risks such as injection attacks. Additionally, loop through the list of recipients and send individual emails to each recipient to ensure that each email address receives the intended message.
// Sanitize and validate the user input for email recipients
$recipients = filter_var_array($_POST['recipients'], FILTER_VALIDATE_EMAIL);
// Loop through the list of recipients and send individual emails
foreach ($recipients as $recipient) {
$to = $recipient;
$subject = "Your Subject Here";
$message = "Your Message Here";
$headers = "From: your_email@example.com";
// Send the email
mail($to, $subject, $message, $headers);
}
Related Questions
- What are some best practices for dynamically assigning content to HTML elements using PHP and JavaScript?
- How can beginners improve their understanding of PHP to effectively handle database queries?
- How can PHP scripts be optimized for handling larger files and increasing script runtime for compression tasks?