How can PHP developers incorporate form input data into email headers to improve email organization and readability?
When sending emails from a PHP form, developers can incorporate form input data into email headers to improve organization and readability by using the "From" and "Reply-To" headers. This allows recipients to easily identify the sender and reply to the correct email address.
<?php
// Get form input data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Set email headers
$headers = "From: $name <$email>\r\n";
$headers .= "Reply-To: $email\r\n";
// Send email
$to = 'recipient@example.com';
$subject = 'New message from website';
$mail_success = mail($to, $subject, $message, $headers);
if ($mail_success) {
echo 'Email sent successfully';
} else {
echo 'Failed to send email';
}
?>