Php form to mail

Here is an example of a PHP form that sends an email using the mail() function:

<?php
// Set the form fields
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

// Set the email recipient
$recipient = '[email protected]';

// Set the email subject
$subject = 'Contact Form Submission';

// Set the email body
$body = "Name: $name\nEmail: $email\nMessage: $message";

// Send the email
mail($recipient, $subject, $body);

// Display a success message
echo 'Thank you for contacting us!';
?>

And here is an example of the HTML form that submits to this PHP script:

<form action="contact.php" method="post">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name"><br><br>
  <label for="email">Email:</label>
  <input type="email" id="email" name="email"><br><br>
  <label for="message">Message:</label>
  <textarea id="message" name="message"></textarea><br><br>
  <input type="submit" value="Send">
</form>

This is a very basic example, and you may want to add additional validation and error handling to your form. You can also use a PHP mail library such as PHPMailer to send emails with more advanced features.

Here are some tips to keep in mind:

Also, you can use PHPMailer library which is more powerful and flexible than the built-in mail() function.

<?php
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Recipient Name');

$mail->Subject = 'Contact Form Submission';
$mail->Body = "Name: $name\nEmail: $email\nMessage: $message";

if (!$mail->send()) {
    echo 'Error sending email';
} else {
    echo 'Email sent successfully';
}
?>

You can also use a PHP framework like Laravel or CodeIgniter which have built-in support for sending emails.