How do I send an HTML form to an email address using PHP?

You have coded up your HTML form, styled it, and looks great on your website. Now, you want to receive an email when a user fills out your form that contains the user-submitted form contents. There are many advanced ways to process an HTML form using PHP, but in this answer, simple is the name of the game.

How To Use PHP To Process A Form Using The Post Method

In short, by using the post method within your form, you will be passing your form details to a PHP file; this PHP file will then read and determine what to do with your form contents.

Let’s Start With Your HTML Form Code

The first bit of code we will need is your HTML form code; you will want to give each form field a name. You will also want to apply the path to your form action PHP file (in this answer, we will save the PHP processor file as “process.php”).

When we put all of this together, the form will look like:

<form action="process.php" method="post">  

<input placeholder="Full Name" type="text" name="name" required> 

<input placeholder="Email Address" type="text" name="email" required>  

<textarea placeholder="Details..." name="details" required </textarea>  

<input type="submit" value="Submit Form"> </form> 

Next Up Is Your PHP Code To Process Your Form To Email

Now, once again, your PHP code to process your HTML form can get relatively complex. For the sake of simplicity, we are only going to cover the basics. Our PHP code will read our form contents, generate an HTML email to deliver the contents via a notification to our email, and then finally redirect the user to a success page.

I added all of the necessary PHP code and code comments to customize your PHP form processor script. You will want to copy the below code and save this file as process.php.


/* Enter the page URL you would like the user to be redirected to after a successful form submission. */
$goto_after_mail = "";

/* Enter your e-mail address for submission notifications. */
$myemail = "";

/* This section is where the script reads your form fields. */
$name= $_REQUEST['name'];
$email= $_REQUEST['email'];

/* The details field is unique and used for textarea form fields. We have special code in place here to allow for line-breaks when included in your e-mail notifications. */
$details = $_REQUEST['details']; 
$details = nl2br($details);

/* Enter your e-mail notification subject below. */
$subject = "Your Contact Form E-mail Subject";

/* Since this form contains name and e-mail fields, we will add these to pre-populate the "from" sender for e-mail notification delivery. */
$header = "From: \"$name\" <$email>\r\n"; 
$header .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

/* Now we construct the HTML e-mail notification template. */
$body = "<head>
	<meta name=\"viewport\" content=\"width=device-width\" />
	<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />



(mail("$myemail", $subject, $body, $header))  ;
header("Location: ".$goto_after_mail);


With the above PHP code processing your HTML form, you can customize your success redirection page location by defining $goto_after_mail, customize your delivery email address by defining $myemail, and customize your delivery email subject by defining $subject.

This PHP form processor code also places the form user email address as the default recipient for your form email notification – this allows you to reply to the submission when reading your email message.

There you have it! Now you should have a basic foundation of how to send an HTML form to email using PHP. From here, you can customize as needed to fit your workflow.




  • The Power of Rate Limiting: Enhancing Website Traffic and Performance

    In the fast-paced digital landscape, where website traffic and user interactions are at an all-time high, ensuring optimal performance and security has become a critical concern for website administrators. One effective technique that can significantly improve both website traffic management and performance is rate limiting. Rate limiting is a method that controls the number of…

  • How do I Remove the “Save my name” Checkbox In WordPress?

    In WordPress, when users leave comments on your blog posts, they are often asked to provide their name, email address, and website. By default, WordPress includes a checkbox labeled “Save my name, email, and website in this browser for the next time I comment.” This checkbox allows users to have their information saved in cookies,…

  • How do you force SSL and fix mixed content with HTACCESS?

    So, you have a shiny new SSL certificate for your website, and now you need to force SSL, but you keep getting mixed content errors. This simple guide will consist of two parts and should get you up and running with a “green lock” in no time. Step 1 — You Need To Modify Your…

  • How do I reset all form fields using pure JavaScript?

    It doesn’t matter why you might want to add a reset form button for your users. It could be that the form has conditional logic, is super long, complex, or that it has form fields that are prone to user error. We will look at a straightforward way to add a form button to reset…

  • How do I create a simple news ticker using HTML and CSS?

    Alright, so you need some text, links, or images to scroll across your website from left to right. Some folks call this a “news ticker”, some call it a “text scroller” — we will stick with news ticker for the sake of keeping our references simple. Sure, you can make this happen with JavaScript but why complicate…

  • How do I make a textarea form field match an input form field?

    Have you ever wondered why the default font for a form textarea field is different when using a placeholder? The default fonts for all web forms are usually determined by the browser used, and technically, it is up to us to define what fonts or styles to display via our CSS. Here is how you…