Make contact form and send email in PHP

posted on July 3, 2006, 12:04 am

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">&nbsp;</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">&nbsp;</td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form> 
<?php 
}; 
?>

You can combine that contact form example with the CAPTCHA verification to prevent spammers from flooding your mail box. Did you check our Contact Form script?

Comments
siddig posted on June 27, 2009, 8:39 pm
Hi
I cant doing that
Can I sent to you my website
And you help to but this code in my site

Chad posted on June 8, 2009, 10:16 am
I Have a Question?
Can I send the Email of the Form to a Yahoo Mail Account?

Hope to Hear a Reply Soon. Thanks

nurfaezah majid posted on March 26, 2009, 10:13 am
received one letter

Bailey posted on February 23, 2009, 12:24 am
How do I make a multiple choice, radio button, and a drop down selection?

edward posted on February 21, 2009, 7:24 pm
I wrote on my php code list:<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'Results from Contact form';

// Your email address. This is where the form information will be sent.
$emailadd = 'mail@rdsnetworks.net';

// Where to redirect after form is processed.
$url = 'http://www.rdsnetworks.net/confirmation.html';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';


And i put all my information there.

This is my site at the moment:http://www.tr-corum.com

Tavares Hanks posted on February 6, 2009, 8:26 pm
I just tried this tutorial and when I hit send it gives me the message that it has been sent, but I have not received the email yet. What have I done wrong?

Shandi posted on January 29, 2009, 7:16 am
I tried the code you posted. When hit submit it just displays the php... What am I doing wrong??

Jay Martin posted on January 15, 2009, 1:00 am
This is how Enom says to send Email using PHP. Can we make this work with the contact from script that I purchased from you?

Question
How can I send email using PHP?


Answer
There are few steps:

First of all, we need an email transport for your application.
Go to this link: http://sourceforge.net/project/showfiles.php?group_id=26031

- Download the zip file to your computer and expand all to your local folder.
- Create a new folder in your hosting. In this example I named the folder: php-mailer
- Upload all of the zip contents you have expanded in your local computer TO the php-mailer
folder at the hosting


Now, lets get the email script. Go to this link: http://www.enom.com/help/hostinghelp.asp?hosthelp=16
Open a new file in your text editor, cut and paste the code, and save it as sendemail.php.


In your editor, you might need to configure line 3-8:

$YourEmailAddress = 'DESTINATION@YOUREMAIL.COM'; //Email address you want the mail to go to
$Yourname = 'YOURNAME'; //Who the email is sent from - this is a name not an email
$MailUsername ='username@yourdomain.com'; //Email address you are using to send the email
$MailPassword = 'youremailpassword'; //This is the password of the email account used to send the email
$MailServer = 'smtp.yourdomain.com'; //Assigned mail server
$MailType = "HTML"; // Can use HTML or TEXT -case doesn't matter

Save the file.


Still in the editor, line 10:
require("class.phpmailer.php");

You need to point where the class is located. It's part of the mail transport package as described in section #1 above. So, in this case, if you follow the folder and file naming in this example, the command should be:

require("php-mailer/class.phpmailer.php");


The last one. Line 121:
<form name=\"form1\" method=\"post\" action=\"index.php\">

You need to change index.php to the same file name you're working right now. In this example, it should be:

<form name=\"form1\" method=\"post\" action=\"sendemail.php\">


Please notice, this email application is sending email using your email account you have created in your hosting or POP3 account. If you don't have any email account in your hosting (POP3), you might need to create it first.



Jay Martin posted on January 14, 2009, 7:36 am
I purchased the form creator. Enom is my hosting server and they do not support PHP mail(). Here is their reply.

Question
Is PHP mail() enabled on your hosting servers?


Answer
No, the PHP mail() function is not enabled on our hosting servers. This is due to the fact the function is not able to send mail using SMTP authorization with a username and password.

You may be able to toggle your PHP script to use a method to send mail that does send a username and password to the SMTP server.

Can you help me with this?

Joe Bradfield posted on January 6, 2009, 11:03 pm
How could i post 2 forms on 1 page that could allow the visitor to post feedback on each item and show them whether or not they have posted feedback or not
view here
-------
Veselin: I think you need something like a rate/comment system and not a simple contact form. Please contact us for details.

Pages:   1   2   3   4   5   6   7   
   
If you have any additions, suggestions or modifications for this example please contact us or use the form below.
   
Your name:
Your email: (email address will not be posted on the web site)
Comment:
Verification