Create contact form - send email-PHP
Create contact form - send message to email in PHP
For every website, it is necessary having a contact form to share the thoughts of site visitors when they visit the site and what they are thinking about your site.
In this blog, we are going to create a contact form and send email using PHP. We will first create a simple contact form with 3 fields -
- Name,
- Email,
- Message.
I will use a simple HTML form with the 3 fields and the Send button.
Create a new file and paste the code below in it. Save it as contact.php and upload it to your web server.
Now, you have a web page (http://www.yourdomain.com/contact.php) with a simple contact form on it
Code snippet
<form method="POST" name="mailform" action=""> <p> Name: <input type="text" name="name" > </p> <p> Email: <input type="text" name="email" > </p> <p> Message: </br> <textarea name="message" rows=8 cols=30></textarea> </p> <input type="submit" value="Submit" name='submit' value='Send'> </form>
Next you need write actual PHP code which will send the email, when the above form is submitted.
We will need to set
email $ToEmail
subject $EmailSubject.
mailheader $mailheader is used to define the email message header with
From, Reply-To and Content-type fields for the message.
Code snippet
<?php
$ToEmail = 'youremail'; $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"].""; $message_body .= "Email: ".$_POST["email"].""; $message_body .= "Message: ".$_POST["email"]."";
mail($ToEmail, $EmailSubject, $message_body, $mailheader) or die ("Failure");
?>
Here is the full code of contact.php.
contact.php
<html> <head> <title> contact form sending email in PHP</title>
</head>
<body>
<h2> contact form sending email </h2>
<?php
if(isset($_POST["submit"])){
$ToEmail = 'youremail';
$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"]."";
$message_body .= "Email: ".$_POST["email"]."";
$message_body .= "Message: ".$_POST["message"]."";
if(mail($ToEmail, $EmailSubject, $message_body, $mailheader))
echo "messege sent successfully";
else
echo "failed";
}
?>
<form method="POST" name="mailform" action="">
<p> Name: <input type="text" name="name" > </p>
<p> Email: <input type="text" name="email" > </p>
<p> Message: </br>
<textarea name="message" rows=8 cols=30></textarea> </p>
<input type="submit" value="Submit" name='submit' value='Send'>
</form>
</body>
</html>

Comments
Post a Comment