go top

MEGA Sale

  • Colection of 65 PHP scripts for $4.29 each

Get 65 PHP scripts in a bundle for $4.29 each!

View Offer

Make contact form and send email in PHP

Tuesday, 6th December, 2011  /  PHP Tutorials  /  456 Comments
Having a contact form on your web site is vital when you need to know what your site visitors think about your web site. We will first create a simple contact form with 3 fields - Email address, Name, Comments. I will use a table to align the 3 fields and the Send button. Create a new file and paste the code below in it. Save it as test.php and upload it to your web server. Now, you have a web page (http://www.yourdomain.com/test.php) with a simple contact form on it.

<form action="test.php" method="post"> 
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"></textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>


Then we will need the actual PHP code which will send the email when the above form is submitted. We will need to define the email that the message should be sent to ($ToEmail) and also the subject for the message that will be sent ($EmailSubject). Change youremail@site.com to your email address where the message should be sent and also add an appropriate subject for you message. The $mailheader variable is used to define the email message header. We set the From, Reply-To and Content-type fields for the message. There are some more fields that can be used but for this example we will only use these 3. Depending on your server configuration you may need to have the From and Reply-to fields be a valid email address from your server.If you have a domain name mysite.com, then you should use a valid email address such as contact@mysite.com. In this example I am sending the email using the actual email address that is submitted via the form on site. Next all the data submitted via the web form is taken from the $_POST variable and saved in the $MESSAGE_BODY variable. Using the nl2br function you will make all the new lines in your comments box appear as new lines in your email message too. Having all the needed data for our email message we will use the mail() function which will send that email for us.

<?php 
$ToEmail = 'youremail@site.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br />";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br />";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br />";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>


All we have to do now is to combine the web form and email sending code into a single page. We will use an IF statement to check if the form has been submitted and if so it will send that email and will show a "Your message was sent" message on the screen instead of the web form.

<?php 
if ($_POST["email"]<>'') {
$ToEmail = 'youremail@site.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br />";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br />";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br />";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
Your message was sent
<?php
} else {
?>
<form action="test.php" method="post">
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"></textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
<?php
};
?>


You can also try out our FREE Sliding Contact Form.
Share on:

456 Comments to "Make contact form and send email in PHP"

Matt Caudill

Matt Caudill / March 18, 2022 at 02:00 am

Hi,

I wanted to introduce myself in a way that shows I’m interesting, witty and clever. Alas, I wrote this email instead.

I come bearing just what you’ve always wanted, another request! I’ll get this over as quickly as possible.

Are you accepting link insertions in old blog posts on your website?

If yes, how much would you charge for this?

If not, will you be interested in a 3-way link exchange that can help boost your website traffic?

Consider my fingers, toes and all appendages crossed!

Cheers,
Celestina Jordan
Chief Dreamer
Craigs9

Dan Reg

Dan Reg / December 10, 2021 at 02:20 am

I typed this whole thing in and checked and checked and it still comes back as failure. Why? Where are the error log that I can find out why?
Thanks

viji

viji / March 12, 2021 at 07:03 am

TEST

Charles K. Ebert

Charles K. Ebert / February 5, 2021 at 22:08 pm

Hello,

WriteAppReviews is the brand-new platform that let

John F. Houston

John F. Houston / February 4, 2021 at 22:35 pm

Hey ,

Do you have any idea how much traffic & sales you will make the MINUTE you reach Page #1 on google & youtube for your buyer keywords?

A LOOTTTT!!

There is a secret way

Kelley Herring

Kelley Herring / February 3, 2021 at 23:39 pm

Hi,

My name is Kelley Herring.

My educational background is nutritional biochemistry. Over the years, I

Thomas Swinton

Thomas Swinton / February 3, 2021 at 11:03 am

Hi,

Each year billions of dollars are distributed to the general public through government, federal and private funding programs in the form of grants, low interest and no- interest loans. Unfortunately, many people don

Charles K. Ebert

Charles K. Ebert / February 2, 2021 at 10:16 am

Hello,

WriteAppReviews is the brand-new platform that let

Nova R. Moreno

Nova R. Moreno / February 1, 2021 at 22:19 pm

Hello,

I

Matt Bacak

Matt Bacak / February 1, 2021 at 14:51 pm

Hi,

Some people follow everything the 'gurus' teach. To a "T".

Others go against the grain. They challenge the status quo.

That's what I do. And that's how I

Add your comment

Captcha

    Please, be polite and helpful and do not spam or offend others! We promise you will be treated the same way!

    Log in to your account to post your comments. If you still haven't joined our community yet, you can create your FREE account now!

    Posting tip:
    If you use code in your comments, please put it in these tags [php], [sql], [css], [js] PHP code example: [php] echo date("Y-m-d"); [/php]

    Thank you,
    PHPJabbers Team

    PHP Scripts

    Check our extensive collection of top-notch PHP Scripts that will enhance your website!


    Commercial PHP scripts

    Free Scripts

    Add great new functionalities to your website with our Free Scripts collection.


    Free scripts