Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4

Thread: PHP email forms

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    1
    Member #
    32434
    Hey, I'm not a beginner but I still wouldnt be the best but I'm quite stuck and its starting to get a bit annoying as i cant solve it..... but im designing a website using html and css style sheets but i have a contact us page and I trying to have a email hooked up to it so when a person fills out the form it automatically gets sent to my email address, I have been told PHP is the only way to do it but i have researched it and I dont really understand the steps that i have to take to complete the contact us page..... any help wud be grateful.... thanks

  2.  

  3. #2
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    It's probably easier than you think.

    First question, do you have a server which can execute PHP? If yes then continue.

    You'll want to test some PHP out, and check the settings. If you make a file called phpinfo.php (or any name of your choice, this doesn't matter) and put in the following source:

    PHP Code:
    <?php phpinfo(); ?>
    That will give you all information about your PHP. You can check if you have sendmail/smtp setup, this should be installed already if you're on linux shared hosting for example.

    If that's all good, then you can continue.

    To test sending mail, make a new php file called testmail.php (or any name of your choice) and put in the following source:

    PHP Code:
    <?php mail('your_email@address.com''test subject''test body'); ?>
    Replacing the email address with your own. If you then visit that script in your URL and you receive email. You're good to go!

    The next step is to make a simple form, this only needs to be HTML. Insert the following source:

    PHP Code:

    <form method="post">
      <
    ul>
        <
    li>
          <
    label for="name">Name</label>
          <
    input type="text" name="name" id="name" value="" />
        </
    li>
        <
    li>
          <
    label for="email">Email</label>
          <
    input type="text" name="email" id="email" value="" />
        </
    li>
        <
    li>
          <
    label for="telephone">Telephone</label>
          <
    input type=" telephone " name="telephone" id="name" value="" />
        </
    li>
        <
    li>
          <
    input type="submit" value="Send Email" />
        </
    li>
      </
    ul>
    </
    form
    That's all you need for the form. It won't send anything at the moment, but will submit the data to itself so once submitted you can access all of the post variables.

    So then at the top of the file with the form, you can put all the necessary mail sending. The simplest way this can be is like this:

    PHP Code:
    <?php
    if(isset($_POST['name'])) {

      
    $message "You have received an email message from {$_POST['name']}, their details are as follows:
      - Telephone: 
    {$_POST['telephone']}
      - Email: 
    {$_POST['email']}";

      
    mail("your_email@address.com""Contact form message"$message);

    }
    ?>
    All together it would look like this:

    PHP Code:
    <?php
    if(isset($_POST['name'])) {

      
    $message "You have received an email message from {$_POST['name']}, their details are as follows:
      - Telephone: 
    {$_POST['telephone']}
      - Email: 
    {$_POST['email']}";

      
    mail("your_email@address.com""Contact form message"$message);

    }
    ?>

    <form method="post">
      <ul>
        <li>
          <label for="name">Name</label>
          <input type="text" name="name" id="name" value="" />
        </li>
        <li>
          <label for="email">Email</label>
          <input type="text" name="email" id="email" value="" />
        </li>
        <li>
          <label for="telephone">Telephone</label>
          <input type=" telephone " name="telephone" id="name" value="" />
        </li>
        <li>
          <input type="submit" value="Send Email" />
        </li>
      </ul>
    </form>
    This can be massively expanded on. You'd want validation and feedback error messages, some form of captcha to stop spambots and perhaps file submission, more fields, or HTML in the email that's sent.

    Even still, there's not much to it all! The above code is untested so if there are any errors I apologise =]
    mlseim and Zboost like this.
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)

  4. #3
    Senior Member RDesignista's Avatar
    Join Date
    Feb 2012
    Location
    Coconut Tree City
    Posts
    822
    Member #
    30921
    Liked
    123 times
    Hey,

    I usually suck at PHP, but I kinda got the PHP email thing down. I want to add on what Ganners said, to help cement the idea down. There are probably several ways to do it, but this is how I do it:

    Quick note: to be able to test, you will need to upload files onto your server first, so the server can send emails.

    Quick note2: if you really don't care to learn PHP and only need a form once, I suggest just using Wufoo's form builder. Making a good form really is time-consuming.

    1. Set up a form. Regarding the input fields, name them, because we will call on the data inputted by using variables in the next step.

    <form method="post" action="sendmail.php">
    <label>Name</label><input name="customer_name">
    <label>Email</label><input name="customer_email">
    <label>Message</label><input type="textarea" name="customer_message">
    </form>

    2. Understand form variables. After people submit the form in step1, the data is stored on the server for you to use (assuming you're using the POST or GET method <form method="post">). This is how to call on the data:

    $_POST['customer_name']
    $_POST['customer_email']
    $_POST['customer_message']

    If you echo $_POST['customer_name'], you'd get whatever they typed in.

    3. Make the email function. I like to have the email function on the next page (<form... action="send_email.php">). On this page, we will do things like validate data (Is the email in proper format? Are required fields filled?). However, I will just focus here on the email function.

    $email=$_POST['customer_email']; //put the data in an easier-to-type variable
    $email_to = "youremail@gmail.com"; //email to receive data
    $email_subject = "Someone Has Submitted Data"; //email subject

    //at this point, we are going to be making a message. we want it to look tidy, so we will using "\n", which are line breaks in php and spaces to make the email look okay. the ".=" is to concatenate string data (aka bunch of words) into a variable. The "." is used to concatenate both string data and variables.

    $email_message .= "Hi. Someone Submitted A Message:\n";
    $email_message .= "Name: ".$_POST['customer_name']."\n";
    $email_message .= "Email: ".$email."\n";
    $email_message .= "The message:\n".$_POST['customer_message'];

    // create email headers
    $headers = 'From: '.$email."\r\n".
    'Reply-To: '.$email."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);

    4. There you have it. After someone submits data, it will tell the server to send an email to the email in $email_to. The next proper step would be to use validation and isset() on the data. Not going to talk about that, but just to let you know, validation probably doubles the time needed to make a form.

    -R

  5. #4
    Senior Member Fireproofgfx's Avatar
    Join Date
    Apr 2012
    Location
    Washington
    Posts
    839
    Member #
    31498
    Liked
    171 times
    Here is the code I have been using on a lot of my sites which is along the same lines as previous responses. I figured I would put this up for more examples to look at.

    PHP Code:

    <?php
     
    $name 
    "";
    $email "";
    $message "";
    $msg_to_user "";

    if (
    $_POST['name'] != "") {

    $name $_POST['name'];
    $email $_POST['email'];
    $message $_POST['message'];
    $humancheck $_POST['humancheck'];

    if (!
    $email) {
      
    $msg_to_user '<br /><br /><h4><font color="FF0000">Please type an email address</font></h4>';
    } else if (!
    $message) {
      
    $msg_to_user '<br /><br /><h4><font color="FF0000">Please enter a message</font></h4>';
    } else if (
    $humancheck != "") {
      
    $msg_to_user '<br /><br /><h4><font color="ff0000">ERROR: The Human Check field must be cleared to be sure you are not a spam-bot</font></h4><br />';
    } else {

      
    // emailing to your email address
      
    $to "Youremailaddress.com";
      
    $subject "Contact Form";
      
    $body "From: $name\n E-Mail: $email\n Message:\n $message";
      
    $headers  "From: Your Site Contact Form\r\n";
      
    mail($to$subject$body$headers);

      
    // sending to your cell phone
      
    $to '1234567890@vtext.com';
      
    $subject "Contact Form";
      
    $body substr($message,0,70);
      
    mail($to$subject$body);

      
    $msg_to_user '<br /><br /><h4>Thank you, your message has been sent</h4>';

      
    $name "";
            
    $email "";
      
    $message "";
    }

    }
    ?>
     
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      <span class="style1">Name:<br />
      <input type="text" name="name" value="<?php echo $name?>" >
      <br />
      <br />
      Email:<br />
      <input type="text" name="email" value="<?php echo $email?>" >
      <br />
      <br />
      Message:<br />
      <textarea rows="9" name="message" cols="30"></textarea>
    </span><br />
      <span class="style1">Human Check:</span><br />
    <input name="humancheck" type="text" class="formFields" id="humancheck" value="Please remove all of this text" size="45" maxlength="32" /style="background-color: #eeeeee;" />
      <br />
      <br />
      <input type="submit" value="Submit" name="submit">
      <?php echo $msg_to_user?>
    </form>


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 01:17 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com