Simple HTML Contact Form to send Email in PHP
Fist of All we will create a HTML Form
[html]
<form role="form" action="submit.php" method="post">
<input name="name" required="required" type="text" placeholder="Enter Your Name" />
<input name="email" required="required" type="email" placeholder="Email ID"/>
<input name="mobile" required="required" type="text" placeholder="Enter Mobile No"/>
<textarea name="message" placeholder="Enter Any Message"></textarea>
<input name="submit" type="submit" value="Send a Mail" /></form>
[/html]
Preview will be look like this
Now we will create a file submit.php for send a mail in PHP
[php]
<?php
if(isset($_POST[‘submit’]))
{
$name = $_POST[‘name’]; // Get Name value from HTML Form
$email_id = $_POST[’email’]; // Get Email Value
$mobile_no = $_POST[‘mobile’]; // Get Mobile No
$msg = $_POST[‘message’]; // Get Message Value
$to = "abc@gmail.com"; // You can change here your Email
$subject = "’$name’ has been sent a mail"; // This is your subject
// HTML Message Starts here
$message ="
<html>
<body>
<table style=’width:600px;’>
<tbody>
<tr>
<td style=’width:150px’><strong>Name: </strong></td>
<td style=’width:400px’>$name</td>
</tr>
<tr>
<td style=’width:150px’><strong>Email ID: </strong></td>
<td style=’width:400px’>$email_id</td>
</tr>
<tr>
<td style=’width:150px’><strong>Mobile No: </strong></td>
<td style=’width:400px’>$mobile_no</td>
</tr>
<tr>
<td style=’width:150px’><strong>Message: </strong></td>
<td style=’width:400px’>$msg</td>
</tr>
</tbody>
</table>
</body>
</html>
";
// HTML Message Ends here
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= ‘From: Admin <admin@websapex.com>’ . "\r\n"; // Give an email id on which you want get a reply. User will get a mail from this email id
$headers .= ‘Cc: info@websapex.com’ . "\r\n"; // If you want add cc
$headers .= ‘Bcc: boss@websapex.com’ . "\r\n"; // If you want add Bcc
if(mail($to,$subject,$message,$headers)){
// Message if mail has been sent
echo "<script>
alert(‘Mail has been sent Successfully.’);
</script>";
}
else{
// Message if mail has been not sent
echo "<script>
alert(‘EMAIL FAILED’);
</script>";
}
}
?>
[/php]
Now you are ready to send a mail through HTML form in PHP
naser
thanks . that a nice and simple contacts form .
Tcpwireless
Thanks for the great post
Lance
It works really well for me
mouse click on url_domain
Hello There. I found your blog using msn. This is a really well written article. I will make sure to bookmark it and come back to read more of your useful information. Thanks for the post. I’ll certainly return.
Webonix
This code is not working sir…
admin
Please check with hosting provider.
This code is completely tested.
Amit kumar
What should be change in headers to send a copy of mail to user ?
admin
Add CC option in headers
Mateen
Thank you so much for this. Just the exact codes I need.
Isaac Chandronnait
Great article! We are linking to this great content on our site. Keep up the good writing.
MixJuige
Thank you very much for the invitation :). Best wishes.
PS: How are you? I am from France 🙂
Prabhakaran S
Thank you so much for this you make simple
El Reloj Antiguo XVI
I value the blog post.Really looking forward to read more. Really Great.
Sadia Naseer
Message Show Email sent succesfully when submit form. But message not sent on my email.And i want to known what is Cc or Bcc ,or it is necessary to use in headers.
admin
If you are not receiving email then please check have you entered correct email address in to and if email address is correct then please check your spam folder.
Cc: It is Carbon Copy used for email to send same email to multiple recipients and every recipient and see every one email address.
Bcc: It is Blank Carbon Copy same like Cc but no recipient can see another recipient email address.