What is the recommended way to create a form that sends input via email without opening the email program using PHP?
To create a form that sends input via email without opening the email program using PHP, you can utilize the PHP `mail()` function. This function allows you to send an email directly from your server without the need for an email client. Simply set the appropriate headers and parameters within the `mail()` function to specify the recipient, subject, message, and any additional headers.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$to = "recipient@example.com";
$subject = "Form Submission";
$message = "Name: " . $_POST['name'] . "\n";
$message .= "Email: " . $_POST['email'] . "\n";
$message .= "Message: " . $_POST['message'];
$headers = "From: sender@example.com" . "\r\n" .
"Reply-To: sender@example.com" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
echo "Email sent successfully!";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
Message: <textarea name="message"></textarea><br>
<input type="submit" value="Submit">
</form>
Keywords
Related Questions
- What are the potential pitfalls of sending emails from a local server using PHP's mail() function?
- What common syntax errors can lead to a "Parse error" in PHP scripts?
- What are the potential consequences of incorrect character encoding in a PHP forum database, and how can it impact user experience?