How can PHP developers integrate email sending functionality into contact forms?
To integrate email sending functionality into contact forms, PHP developers can use the `mail()` function to send emails from the server. This function allows developers to specify the recipient email address, subject, message body, and additional headers. By including this function in the form submission process, developers can automatically send an email notification whenever a user submits a contact form.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$recipient = "youremail@example.com";
$subject = "Contact Form Submission";
$message = $_POST["message"];
$headers = "From: " . $_POST["email"];
if (mail($recipient, $subject, $message, $headers)) {
echo "Email sent successfully!";
} else {
echo "Email sending failed.";
}
}
?>
Related Questions
- What are the best practices for handling character encoding in PHP when reading data from external sources like CSV files?
- How can the use of Window functions in databases impact PHP solutions for calculating differences in values?
- What are the advantages and disadvantages of implementing CAPTCHA in PHP forms to prevent spam submissions?