Text Box that will send an email

Discussion in 'Web Design and Development' started by MacFan782040, Sep 8, 2008.

  1. MacFan782040 macrumors 6502a

    MacFan782040

    Joined:
    Dec 1, 2003
    Location:
    Scranton, PA
    #1
    Hey everyone,

    What I am trying to do is create a form on my website that people can type stuff in a text box and it will email it to my address. I've done it before, but I think it was on my Geocities site and it was already setup for me.

    I know there is a javascript out there that will do it, but all the ones I have googled don't seem to work. Do I need something special on my server? If anyone can provide me with a script that would be great.

    Thanks a lot!
     
  2. TEG macrumors 604

    TEG

    Joined:
    Jan 21, 2002
    Location:
    Langley, Washington
    #2
  3. bootedbear macrumors 6502

    bootedbear

    Joined:
    Sep 13, 2004
    Location:
    Austin, TX
    #4
    HTML and JavaScript have no ability to send mail. The best that they can do, as TEG's link shows, is to cause a window in the local email client to open in order to use it to send the message.

    If you want to send the message directly from the page, there must be a server-side component to handle it.

    There are tons of PHP examples out there, and JavaMail is pretty easy to use if your backend is Java powered.
     
  4. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #5
    It's easy. Your form simply sends the test back to the server. A script on the server then stuffs the text into a system command (such as "sendmail") then the script sends the command to be run by using a "system" or "exec" command.
     
  5. MacFan782040 thread starter macrumors 6502a

    MacFan782040

    Joined:
    Dec 1, 2003
    Location:
    Scranton, PA
    #6
    Thanks a lot everyone. I knew there had to be a server-side to it. I got one to work that opens up an email client, but a lot of people don't have theirs set up and stuff like that.

    I use Go Daddy for my site, so I'll check with them to see if they have anything available to use. :eek:

    *Edit*- I used the link provided by jdl8422 and it worked great. Thanks :)
     
  6. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #7
    This page may be of help, it's from GoDaddy's site for their Form-Mailer.
     
  7. MCRunning macrumors 6502

    Joined:
    Sep 8, 2008
    #9
    Here is something quick I just wrote up:

    contact.php
    PHP:
      <table width="100%" cellpadding="1" cellspacing="1">
      <
    tr>
      <
    td width="600" valign="top">
      <
    h3>Contact WebsiteName </h3>
      <
    p>If you have any commentsquestions, or concernsplease use the form below.</p>
      <
    form method="post" action="sendmail.php">
      <
    table width="500">
      <
    tr>
      <
    td valign="top">Name:</td><td><input type="text" size="30" name="name" /></td>
      </
    tr>
      <
    tr>
      <
    td valign="top">E-mail:</td><td><input type="text" size="30" name="email" /></td>
      </
    tr>
      <
    tr>
      <
    td valign="top">2:</td><td><input type="text" size="30" name="verify" />(like youwe don't like spam)</td>
      </tr>
      <tr>
      <td valign="top">Comments:</td>
      <td><textarea cols="50" rows="8" name="comments"></textarea></td>
      </tr>
      <tr>
      <td></td>
      <td><input type="submit" value="Submit" /></td>
      </tr>
      </table>
      </form>
    </td>

    sendmail.php
    PHP:
    <?
    //send us your mail
    if($verify == 4)
    {
    mail('email@website.com','subject',$comments,'From: '.$name.' <'.$email.'>');
    header'Location: website' );
    }
    else
    {
     
    header'Location: website' );
    }
    ?>
    Messy, but should get the job done.

    MC
     
  8. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #10
    Table layouts. Really? The code also isn't valid or semantically correct. I know you're trying to give a short example, but the code does next to no validation. If you were to actually use that code you would have your inbox spammed big time and likely would become someones spambot and you'd be legally responsible. That PHP code should not be used as is, it could make for a starting point though.

    Until a developer is comfortable with data validation and the like, I recommend sticking with scripts that have been developed well and take these extra things into consideration for you so you don't get yourself into spam hell.
     
  9. MacFan782040 thread starter macrumors 6502a

    MacFan782040

    Joined:
    Dec 1, 2003
    Location:
    Scranton, PA
    #11
    All of you are awesome. Thanks for the help.

    I have 1 more for ya... ;)

    Basically I run my organization's website. We are required to do library hours, and usually we log them in a binder at the library, but we wanted to see if we could do it instead on our website.

    Is there some kind of database script that will allow about 50 people or so to choose their name, and then add hours, and it will update a table with everyone's names and hours?

    Thanks again!
     
  10. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #12
    You'll want something like this Time Management script. I've come across various scripts that do time management and there are good free ones, but I have not used any personally so won't make any specific recommendations. I'll simply say Google search.
     
  11. MCRunning macrumors 6502

    Joined:
    Sep 8, 2008
    #13
    Better? He can modify the code to his own needs. All he asked for was a text box that can send whatever to his email and that is what I provided.

    PHP:
    <?php

    // Check for form submission:
    if (isset($_POST['submitted'])) {

        
    // Minimal form validation:
        
    if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comments']) ) {
        
            
    // Create the body:
            
    $body "Name: {$_POST['name']}\n\nComments: {$_POST['comments']}";
            
            
    // Make it no longer than 100 characters long:
            
    $body wordwrap($body100);
        
            
    // Send the email:
            
    mail('your_email@example.com''Contact Form Submission'$body"From: {$_POST['email']}");
            
            
    // Print a message:
            
    echo '<p><em>Thank you for contacting me. I will reply some day.</em></p>';
            
            
    // Clear $_POST (so that the form's not sticky):
            
    $_POST = array();
        
        } else {
            echo 
    '<p style="font-weight: bold; color: #C00">Please fill out the form completely.</p>';
        }
        
    // End of main isset() IF.

    // Create the HTML form:
    ?>
    <p>Please fill out this form to contact me.</p>
    <form action="email.php" method="post">
        <p>Name: <input type="text" name="name" size="30" maxlength="60" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" /></p>
        <p>Email Address: <input type="text" name="email" size="30" maxlength="80" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>
        <p>Comments: <textarea name="comments" rows="5" cols="30"><?php if (isset($_POST['comments'])) echo $_POST['comments']; ?></textarea></p>
        <p><input type="submit" name="submit" value="Send!" /></p>
        <input type="hidden" name="submitted" value="TRUE" />
    </form>
    </body>
    </html>
    MC
     
  12. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #14
    If the OP is asking about sending emails from a page, do you really believe he'll know about PHP validation? The new HTML you provided looks a lot better without the tables. The minimal validation you're doing is certainly a start, but could still easily be turned into a spambot. I'm not saying you need to post code that is immensely validated or anything. I just like it to be clear to the OP that it's a minimal setup and that leaving the code as is will very likely leave them open to having their site turned into a spambot. This is also why I advise people to use pre-built scripts unless they're developers themselves and understand these potential problems. Essentially just making sure the OP knows there is a disclaimer with the code.

    This wasn't attack on you MCRunning, I just want to make sure the OP doesn't think he can copy and paste the code you provide as is without further consideration, and to also explain why he shouldn't. Your post simply opened up that opportunity so don't take what I've said personally because it's not meant as such.

    For those interested in using PHP for form validation like you would do for a contact form you can find some good reading at PHP Security Consortium to get you thinking about security.

    Others readings:
     
  13. MCRunning macrumors 6502

    Joined:
    Sep 8, 2008
    #15
    I totally see where you are coming from angel, but at the same time it's like when you hire someone to do your work for you. If you don't tell them exactly what you want, you will get exactly what you paid for.

    Say he hired a programmer to do this job for him and asked for a text box that when a user submitted information it emailed him whatever was in the text box. The programmer will give him exactly that, then later he realizes he is getting spammed and needs validation. He will go back to the programmer and say why didn't you include validation?! And of course the programmer will say "you didn't ask for validation".

    Hopefully a lesson was learned from this :D

    MC
     
  14. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #16
    Yea, get a better programmer that keeps the customer's lack of knowledge in mind ;)

    Kidding aside though, customers will never be able to tell a programmer exactly what they want/need. If they knew that level of detail they'd likely be able to do it them self. I work in an area where even amongst professionals who work together on a regular basis, but with differing backgrounds, have a lot of trouble talking about certain things because they come from different areas and perspectives.

    A good programmer will know what follow up questions to do with a customer (through experience) and what things to provide that are not asked for, but are needed. I mean when you want an oil change for your car you don't tell the mechanic that the old oil removed as well. He'd likely upset at you telling him his job and for making stupid statements.

    OK, I'm done talking off topic. Sorry to OP for getting so far off from topic. Let us know if you need anything further.
     
  15. MCRunning macrumors 6502

    Joined:
    Sep 8, 2008
    #17
    PHP:
    <?php
    // Check for form submission:
    if (isset($_POST['submitted'])) {
        function 
    spam_scrubber($value) {
            
            
    $very_bad = array('to:''cc:''bcc:''content-type:''mime-version:''multipart-mixed:''content-transfer-encoding:');
            
            foreach (
    $very_bad as $v) {
                if (
    stripos($value$v) !== false) return '';
            }
            
            
    // Replace any newline characters with spaces:
            
    $value str_replace(array( "\r""\n""%0a""%0d"), ' '$value);
            
            
    // Return the value:
            
    return trim($value);
        
        } 
    // End of spam_scrubber() function.
        
        // Clean the form data:
        
    $scrubbed array_map('spam_scrubber'$_POST);

        
    // Minimal form validation:
        
    if (!empty($scrubbed['name']) && !empty($scrubbed['email']) && !empty($scrubbed['comments']) ) {
        
            
    // Create the body:
            
    $body "Name: {$scrubbed['name']}\n\nComments: {$scrubbed['comments']}";
            
    $body wordwrap($body100);
        
            
    // Send the email:
            
    mail('your_email@example.com''Contact Form Submission'$body"From: {$scrubbed['email']}");
            
            
    // Print a message:
            
    echo '<p><em>Thank you for contacting me. I will reply some day.</em></p>';
            
            
    // Clear $_POST (so that the form's not sticky):
            
    $_POST = array();
        
        } else {
            echo 
    '<p style="font-weight: bold; color: #C00">Please fill out the form completely.</p>';
        }
        
    // End of main isset() IF.
    ?>
    <p>Please fill out this form to contact me.</p>
    <form action="email.php" method="post">
        <p>Name: <input type="text" name="name" size="30" maxlength="60" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" /></p>
        <p>Email Address: <input type="text" name="email" size="30" maxlength="80" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>
        <p>Comments: <textarea name="comments" rows="5" cols="30"><?php if (isset($_POST['comments'])) echo $_POST['comments']; ?></textarea></p>
        <p><input type="submit" name="submit" value="Send!" /></p>
        <input type="hidden" name="submitted" value="TRUE" />
    </form>
    </body>
    </html>
    More validation yay

    MC
     
  16. MacFan782040 thread starter macrumors 6502a

    MacFan782040

    Joined:
    Dec 1, 2003
    Location:
    Scranton, PA
    #18
    A huge thanks to all of you! :) I downloaded the Time Management thing from angelwatt but I don't really have a clue how to customize it.. I'm more familiar with HTML.
     

Share This Page