Sending Emails with PHP from an HTML Form

Sending emails with PHP is a common requirement for many web applications, especially when dealing with user feedback, contact forms, or newsletter subscriptions. In this tutorial, we will explore how to send an email using PHP when a user submits an HTML form.

Step 1: Create the HTML Form

The first step is to create an HTML form that will collect the necessary information from the user. The form should include fields for the user’s name, email address, and message.

<form action="" method="post">
    First Name: <input type="text" name="first_name"><br>
    Last Name: <input type="text" name="last_name"><br>
    Email: <input type="text" name="email"><br>
    Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
    <input type="submit" name="submit" value="Submit">
</form>

Step 2: Handle Form Submission with PHP

To handle the form submission, we need to create a PHP script that will send an email using the mail() function. We will also include error handling to ensure that the email is sent successfully.

if (isset($_POST['submit'])) {
    $to = "[email protected]"; // recipient's email address
    $from = $_POST['email']; // sender's email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $subject = "Form Submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];

    $headers = "From: " . $from;
    if (mail($to, $subject, $message, $headers)) {
        echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    } else {
        echo "Failed to send email.";
    }
}

Step 3: Send a Copy of the Message to the Sender (Optional)

If you want to send a copy of the message to the sender, you can add another mail() function call with the sender’s email address as the recipient.

$subject2 = "Copy of your form submission";
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers2 = "From: " . $to;
mail($from, $subject2, $message2, $headers2);

Best Practices

  • Always validate user input to prevent spam and malicious activities.
  • Use a secure connection (HTTPS) when sending emails to prevent interception of sensitive information.
  • Consider using a library or framework that provides email functionality, such as PHPMailer or SwiftMailer, for more advanced features and better security.

Troubleshooting

If you encounter issues with sending emails, check the following:

  • Ensure that your PHP installation has the mail() function enabled.
  • Verify that your SMTP server settings are correct.
  • Check your email account’s spam folder for undelivered messages.

By following these steps and best practices, you can successfully send emails with PHP from an HTML form. Remember to always test your code thoroughly to ensure that it works as expected.

Leave a Reply

Your email address will not be published. Required fields are marked *