PHP Mailer script to send Contact Us form email.

If the website is running on PHP Server than it’s just easy to create Form. It will only take fifteen to twenty minutes to build a form on a website. The simple static website can also be customized to have a contact us or any other form.
Let’s begin by writing a simple html code for form.
 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">  
 <div class="container" style="background-color:grey; text-align:center; ">  
 <h4><u>Send me a message</u></h4>  
 <form id="contactForm" action="send_form_email.php" method="post">  
      <div class="row">  
           <div class="col-md-6 form-group">  
                <span style="background-color: #FFFF00"><input type="text" class="form-control" name="senderName" id="senderName" placeholder="name"></span><br/>  
                <input type="email" class="form-control" name="senderEmail" id="senderEmail" placeholder="e-mail"><br/>  
                <input type="text" class="form-control" name="subject" id="subject" placeholder="Subject">  
           </div>  
           <div class="col-md-6 form-group">  
                <textarea name="message" class="form-control" id="message" placeholder="Message"></textarea>  
           </div>  
           <div class="col-md-12 form-group">  
                <button type="submit">Send Message</button>  
           </div>  
      </div>  
 </form>  
 </div>  
 
Note: Please remember the name that have been given to textboxes and text area. We have to use same name in PHP script. And the file must be located where our .html file is stored as we have directly specified send_form_email.php in action parameter. If you have placed somewhere else then do specify the proper path. 

Now let’s write the PHP script to send an email.
Create a file send_form_email.php

Copy the below code.

 <?   
   $to = "info@yourdomain.com";   
   $from = "";   
   $subject = "";   
   $message = "";  
   if (isset($_POST["senderEmail"])) {  
   $from = $_POST["senderEmail"]; // sender  
      $name = $_POST["senderName"];  
   $subject = $_POST["subject"];  
      $message.="Name: ";  
      $message.=$_POST['senderName'] . "<br/>";  
      $message.="Message: ";  
   $message .= $_POST['message'];  
   $headers = "From: $fromrn";   
   $headers .= "Content-type: text/htmlrn";   
   mail($to, $subject, $message, $headers);   
      }  
   header('Location: http://www.yourdomain.com');  
 ?>  

 

We need to change the $to email id to wherever you want to send an email.
As the form is submitted by the user and the form is successfully sent then we have to redirect the user to any page. So for that we have to change the header location as per requirement. Here we have redirected to our actual domain site.
Note: This will only work on PHP Server. If we try to submit the form on our local machine then we will see the php file appearing but on live server the actual mail will be sent.
 
For seeing the Demo of this application CLICK HERE.
If you were looking for something and this helped? Or if you
have any other better idea? Feel free to share with us!

Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.