go top

MEGA Sale

  • Colection of 65 PHP scripts for $4.29 each

Make contact form and send email in PHP

Limited offer!

Get a huge collection of PHP scripts in a special bundle!

Mega Sale Price
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.

Special Offer!

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

Mega Sale Offer

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

  • John Cornetta

    John Cornetta

    September 12, 2020 at 22:51 pm

    Hi,

    John Cornetta here, I am Co-founder of Groove Digital. Recently, I and my partner Mike Filsaime broke all-time records by building trust and transparancy with our users.

    We crossed over 200000 users and I am sure that you too must have heard of the latest buzz going around with us!

    Yes, Our lifetime deal will be gone forever.

    Why people are trusting this platform even in these harsh times? Because this is the future and gives you the power to build your online business assets without paying thousands of dollars to the developer using hastle-free drag and drop features.

    Witness the NEXT GENERATION builder of Online Assets. Be it any business from sales to service or consulting.

    Please read every word very, very carefully.
    >> https://groovepages.groovesell.com/a/oSux0WS2ruvw

    Our Guarantee:-
    We guarantee you're going to be impressed with this LIFETIME DEAL. You will never pay us again. If it's not right for you, let us know within 30 days and we'll happily refund that payment and part as friends.

    Best,
    John
    Groove Digital

    P.S.

  • John Cornetta

    John Cornetta

    September 10, 2020 at 20:53 pm

    Hi,

    John Cornetta here, I am Co-founder of Groove Digital. Recently, I and my partner Mike Filsaime broke all-time records by building trust and transparancy with our users.

    We crossed over 200000 users and I am sure that you too must have heard of the latest buzz going around with us!

    Yes, Our lifetime deal will be gone forever.

    Why people are trusting this platform even in these harsh times? Because this is the future and gives you the power to build your online business assets without paying thousands of dollars to the developer using hastle-free drag and drop features.

    Witness the NEXT GENERATION builder of Online Assets. Be it any business from sales to service or consulting.

    Please read every word very, very carefully.
    >> https://groovepages.groovesell.com/a/oSux0WS2ruvw

    Our Guarantee:-
    We guarantee you're going to be impressed with this LIFETIME DEAL. You will never pay us again. If it's not right for you, let us know within 30 days and we'll happily refund that payment and part as friends.

    Best,
    John
    Groove Digital

    P.S.

  • John Cornetta

    John Cornetta

    September 4, 2020 at 18:54 pm

    Hi,

    John Cornetta here, I am Co-founder of Groove Digital. Recently, I and my partner Mike Filsaime broke all-time records by building trust and transparancy with our users.

    We crossed over 100000 users and I hope you too must have heard of the latest buzz going around with us!

    Yes, Our lifetime deal will be gone forever.

    Why people are trusting this platform even in these harsh times? Because this is the future and gives you the power to build your online business assets without paying thousands of dollars to the developer using hastle-free drag and drop features.

    Witness the NEXT GENERATION builder of Online Assets. Be it any business from sales to service and consulting.

    Please read every word very, very carefully.
    BONUS INVITE LINK
    >> https://groovepages.groovesell.com/a/oSux0WS2ruvw

    Our Guarantee:-
    We guarantee you're going to be impressed with this LIFETIME DEAL. You will never pay us again. If it's not right for you, let us know within 30 days and we'll happily refund that payment and part as friends.

    Best,
    John
    Groove Digital

    P.S.

  • John Cornetta

    John Cornetta

    September 4, 2020 at 14:03 pm

    Hi,

    John Cornetta here, I am Co-founder of Groove Digital. Recently, I and my partner Mike Filsaime broke all-time records by building trust and transparancy with our users.

    We crossed over 100000 users and I hope you too must have heard of the latest buzz going around with us!

    Yes, Our lifetime deal will be gone forever.

    Why people are trusting this platform even in these harsh times? Because this is the future and gives you the power to build your online business assets without paying thousands of dollars to the developer using hastle-free drag and drop features.

    Witness the NEXT GENERATION builder of Online Assets. Be it any business from sales to service and consulting.

    Please read every word very, very carefully.
    BONUS INVITE LINK
    >> https://groovepages.groovesell.com/a/oSux0WS2ruvw

    Our Guarantee:-
    We guarantee you're going to be impressed with this LIFETIME DEAL. You will never pay us again. If it's not right for you, let us know within 30 days and we'll happily refund that payment and part as friends.

    Best,
    John
    Groove Digital

    P.S.

  • Mike Filsaime

    Mike Filsaime

    September 3, 2020 at 23:35 pm

    Hi,

    Mike Filsaime here, Co-founder of Groove Digital.

    I hope you have heard of the latest buzz going around with us! Yes, Our lifetime deal is ending.

    Last chance to become a beta-tester and grab our lifetime deal!

    Witness the NEXT GENERATION builder of Online Industry. A need and asset of every possible business on the planet.

    LIMITED TIME. Please read every word very, very carefully

    >> https://bit.ly/Groovefunnels-Lifetime

    TAKE ACTION OR REGRET LATER. YOUR FINAL CHANCE TO JUMP ON THE PLATFORM AND GRAB THE LIFETIME DEAL.

    Our Guarantee:-
    We guarantee you're going to be impressed with GroovePages and this LIFETIME DEAL. You will never pay us again for GroovePages access. If it's not right for you, let us know within 30 days and we'll happily refund that payment and part as friends.

    Best,
    Mike
    Groove Digital

    P.S.

  • Michael A. Prall

    Michael A. Prall

    September 2, 2020 at 01:15 am

    Hey! Let me introduce you to our community!

    Get your Lifetime Access to powerful shopping cart software. It allows you to list and sell any products online. It can handle digital or physical products and even services.

    It works in ANY business niche. You can create subscriptions and payment plans, and it integrates with all of the major payment processors.

    * Free for LIFE
    * No Games. No Fine Print.
    * No Credit Needed Ever!
    * Was $99/mo, Now it

  • Michael A. Prall

    Michael A. Prall

    August 31, 2020 at 22:57 pm

    Hey! Let me introduce you to our community!

    Get your Lifetime Access to powerful shopping cart software. It allows you to list and sell any products online. It can handle digital or physical products and even services.

    It works in ANY business niche. You can create subscriptions and payment plans, and it integrates with all of the major payment processors.

    * Free for LIFE
    * No Games. No Fine Print.
    * No Credit Needed Ever!
    * Was $99/mo, Now it

  • Mike Filsaime

    Mike Filsaime

    August 19, 2020 at 21:36 pm

    Hi,

    Mike Filsaime here, Co-founder of Groove Digital.

    I hope you have heard of the latest buzz going around with us! Yes, Our lifetime deal is ending.

    Last chance to become a beta-tester and grab our lifetime deal!

    Witness the NEXT GENERATION builder of Online Industry. A need and asset of every possible business on the planet.

    LIFETIME ACCESS: ONE TIME-PAYMENT

    >> https://bit.ly/Groovesell-Lifetime

    TAKE ACTION OR REGRET LATER. YOUR FINAL CHANCE TO JUMP ON THE PLATFORM AND GRAB THE LIFETIME DEAL.

    Our Guarantee:-
    We guarantee you're going to be impressed with GroovePages and this LIFETIME DEAL. You will never pay us again for GroovePages access. If it's not right for you, let us know within 30 days and we'll happily refund that payment and part as friends.

    Best,
    Mike
    Groove Digital

    P.S.

  • Abdeali Jamnagarwala

    Abdeali Jamnagarwala

    August 7, 2020 at 17:37 pm

    Hi,

    Are you a little concerned regarding your current online marketing? I think you

  • Fergal Downes

    Fergal Downes

    August 6, 2020 at 11:44 am

    Hey Folks!

    I am Fergal Downes introducing you to my EMPIRE SYSTEM.

    My Newbie Friendly Method Reveals How We Make $128 Per Day With FREE Traffic In Just 30 Minutes Per Day!
    ? Works for anyone, period
    ? Newbie-friendly method
    ? Unlimited FREE Traffic System
    ? No Creating Videos
    ? No Google, Facebook or Bing Ads
    ? $128 per day formula
    ? 60-day money back guarantee Get results with this or we will give you 100% of your money back inside the next 60 days.

    ? https://bit.ly/The-Empire-Traffic

    It

Add your comment

Captcha
    • 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