How can PHP developers handle sending confirmation emails to users who fill out a form, while also sending a copy to a predefined email address, without compromising security or functionality?
Issue: PHP developers can handle sending confirmation emails to users who fill out a form, while also sending a copy to a predefined email address, by using the PHP `mail()` function to send the emails. To ensure security, developers should sanitize user input to prevent injection attacks and validate email addresses before sending. Additionally, using a separate email address for the predefined recipient can help maintain security and functionality.
<?php
// Sanitize user input
$user_email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
// Validate email address
if (filter_var($user_email, FILTER_VALIDATE_EMAIL)) {
// Send confirmation email to user
$user_subject = "Confirmation Email";
$user_message = "Thank you for filling out the form!";
mail($user_email, $user_subject, $user_message);
// Send copy to predefined email address
$predefined_email = "predefined@example.com";
$predefined_subject = "Copy of Confirmation Email";
$predefined_message = "A copy of the confirmation email sent to the user.";
mail($predefined_email, $predefined_subject, $predefined_message);
} else {
echo "Invalid email address";
}
?>
Related Questions
- How can you create custom functions in PHP, and what are the benefits of doing so?
- What are some common pitfalls to avoid when implementing streaming functionality on a PHP server for a custom player?
- What are the advantages and disadvantages of storing a Techtree in arrays in a file versus in a database in terms of memory usage and script performance in PHP?