[PHP] How to Make a Mail Contact Us Form

reloadmydeagle Jan 22, 2015

  1. reloadmydeagle

    reloadmydeagle I LoVe GoldZ Lifetime Gold XPG Retired Staff
    460/564

    Joined:
    Oct 30, 2013
    Messages:
    1,608
    Likes Received:
    656
    Trophy Points:
    135
    Gender:
    Male
    Location:
    Texas
    Console:
    Xbox One
    Making a contact form is quite easy, but I will just share with you PHP/HTML noobs how to create one. The way this works is you have the form on your website, the person puts in their email, name, subject, and message and hits send message. You will then receive their message via email to reply to them. I recommend using Sublime Text 2 to code, as it is great for organization.
    (Example of a Contact Form: http://i.imgur.com/ZHRZFJ3.png)

    Step 1 - The PHP Code
    • Insert this code into a new Sublime Text document, Notepad document, etc.
    <?php
    // Check for empty fields
    if(empty($_POST['name']) ||
    empty($_POST['email']) ||
    empty($_POST['phone']) ||
    empty($_POST['message']) ||
    !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
    {
    echo "No arguments Provided!";
    return false;
    }

    $name = $_POST['name'];
    $email_address = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];

    // Create the email and send the message
    $to = 'yourname@yourdomain.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
    $email_subject = "New Mail: $name";
    $email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
    $headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
    $headers .= "Reply-To: $email_address";
    mail($to,$email_subject,$email_body,$headers);
    return true;
    ?>
    • Fill out all the info to match your info.
    • Save as contact.php
    [hr]

    Step 2 - The HTML
    • You will need to create a form in your HTML page, and that requires just some small experience in HTML.
    • Here is an example code of a form, just bend it to match your page:
    <form name="sentMessage" id="contactForm" novalidate>
    <div class="row">
    <div class="col-md-6">
    <div class="form-group">
    <input type="text" class="form-control" placeholder="Your Name *" id="name" required data-validation-required-message="Please enter your name.">
    <p class="help-block text-danger">
    </div>
    <div class="form-group">
    <input type="email" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address.">
    <p class="help-block text-danger">
    </div>
    <div class="form-group">
    <input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">
    <p class="help-block text-danger">
    </div>
    </div>
    <div class="col-md-6">
    <div class="form-group">
    <textarea class="form-control" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a message."></textarea>
    <p class="help-block text-danger">
    </div>
    </div>
    <div class="clearfix"></div>
    <div class="col-lg-12 text-center">
    <div id="success"></div>
    <button type="submit" class="btn btn-xl">Send Message</button>
    </div>
    </div>
    </form>
    • Tip: you don't really need any of the HTML above other than the <form> tag and it's elements, submit button, and the textareas.
    • After inserting the HTML into your page and fitting it to look nice, save it and test out that contact form!
    [hr]

    Hope I helped anyone struggling with PHP development! ;)
     
  2. prodigy

    prodigy Trusted Seller since 2014 :)
    95/188

    Joined:
    Jul 7, 2014
    Messages:
    1,490
    Likes Received:
    357
    Trophy Points:
    95
    Gender:
    Male
    Occupation:
    Call of Duty Recovery Seller
    Location:
    Boston, Ma
    Console:
    Computer
    Nice share Rifter keep up the good work.
     
  3. XxStarzxX

    XxStarzxX Banned! BANNED
    205/282

    Joined:
    Mar 3, 2012
    Messages:
    6,119
    Likes Received:
    1,136
    Trophy Points:
    205
    Gender:
    Male
    Location:
    XPG
    Console:
    Xbox One
    On top form here Rifter, keep up the great work ;)
     
  4. DanielQuinn

    DanielQuinn The Krafty one!
    0/47

    Joined:
    Sep 11, 2013
    Messages:
    1,902
    Likes Received:
    520
    Trophy Points:
    0
    Gender:
    Male
    Location:
    Under Your Bed
    Console:
    Xbox
    Great post bud
     
  5. Iamcoolz

    Iamcoolz Forum Administrator Staff Member XPG Administrator
    205/282

    Joined:
    Mar 30, 2012
    Messages:
    1,227
    Likes Received:
    507
    Trophy Points:
    205
    Gender:
    Male
    Location:
    XPG
    Console:
    Xbox One
    To add onto this:

    To prevent bots from spamming up your contact form page add in things such as -

    $email_address = $_POST['email'];

    $email_check = ''/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

    if $email_address,$email_check)) {
    $error_message .= 'This email holds invalid characters, please change it to an existing email.';
    }
    This will prevent people using fake emails to harass you or flood your email.
     
  6. reloadmydeagle

    reloadmydeagle I LoVe GoldZ Lifetime Gold XPG Retired Staff
    135/188

    Joined:
    Oct 30, 2013
    Messages:
    1,608
    Likes Received:
    656
    Trophy Points:
    135
    Gender:
    Male
    Location:
    Texas
    Console:
    Xbox One
    Nice one, may add that in ;)
     
  7. ToxicGas

    ToxicGas Certified Developer Lifetime Gold XPG Retired Staff
    460/564

    Joined:
    Jan 23, 2015
    Messages:
    1,779
    Likes Received:
    1,307
    Trophy Points:
    460
    Gender:
    Male
    Console:
    Computer
    Very helpful thanks for the share bud ;) will be looking into this shortly.
     
  8. DanielQuinn

    DanielQuinn The Krafty one!
    0/47

    Joined:
    Sep 11, 2013
    Messages:
    1,902
    Likes Received:
    520
    Trophy Points:
    0
    Gender:
    Male
    Location:
    Under Your Bed
    Console:
    Xbox
    Thanks ;)
     

Share This Page

Close