Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need super simple method to send a form to an email address

    On my own site, I use a very long pearl script to send the forms on my site to my email. It works fine. However, I am redesigning a site for an organization who want a form. I don't want to use the long complicated script since they are not web saavy, plus I don't know if their hosts server can deal with it. And so far, it's not working correctly

    So I need something super simple. PHP, CGI, whatever. I know it can be done in PHP, but I don't know how. It just needs to send the form to their email address. I would use <action="mailto: poo@pee.com" etc>, but that prompts the users email client to open, which I don't want.

    So if anyone has something short and sweet (that works) to send a form to an email address, that would be great.

    I found one at http://www.3rdcoast.com but it only grabs the first item in the fied and nothing else. Help please! This needs to be done very soon.

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    $email "$_REQUEST['email']"//get the email address of person filling out the form
    $to "someone@somedomain.com"//email address to send to
    $subject "$_REQUEST['subject']"// get the subject or predetermin one
    $headers  'MIME-Version: 1.0' "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n"
    $body "$_REQUEST['message'];"
    mail($to$subject$body$headers);
    if 
    mail($to$subject$body$headers)
    {
    echo 
    "mail sent";
    }
    else
    {
    echo 
    "mail not sent";

    need any help customising email me or add me on msn or something
    Last edited by rafiki; 03-05-2007 at 08:23 PM.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Doesn't get much simpler than this ...

    You need to use the variables you have defined in your form.
    This is just an example. No security here either. But always
    define the subject in your code. Don't let the visitor define the subject.

    PHP Code:
    <?php 

    // define the variables exactly as you have
    // named them in your form.  This is just an example here:
    $user_name $_POST['user_name'];
    $user_email $_POST['user_email'];
    $user_subject $_POST['user_subject'];
    $user_comment $_POST['user_comment'];

    //Who to send the email to (your email address):
    $recipient "youremail@aol.com";

    //Email subject line ... hard-coded:
    $esubject "email from website";

    //Where to go after the email has been sent:
    $redirect="index.html";

    $emessage="
    $user_subject
    --------------------------------
    Name: $user_name
    Email: $user_email
    --------------------------------
    Comment:\n $user_comment
    --------------------------------

    "
    ;

    $extraheaders "From: $user_email\nReply-To: $user_email\n\n";
    if (! 
    mail($recipient$esubject$emessage$extraheaders))
    echo 
    "Mail did not send for some reason.";

    header ("location: $redirect");

    ?>

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by mlseim View Post
    PHP Code:
    *snip*
    if (! 
    mail($recipient$esubject$emessage$extraheaders))
    echo 
    "Mail did not send for some reason.";

    header ("location: $redirect");
    *
    snip
    If you use that code and sending does actually fail it would not forward and would give you an error about headers already being sent. You need to either die() with the error, forward back to the previous page with a variable that will tell that page to show an error, or use put the header into an "else".
    PHP Code:
    if (!mail($recipient$esubject$emessage$extraheaders))
    {
        echo 
    "Mail did not send for some reason.";
    }
    else
    {
        
    header ("location: $redirect");


  • #5
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Quote Originally Posted by Inigoesdr View Post
    If you use that code and sending does actually fail it would not forward and would give you an error about headers already being sent. You need to either die() with the error, forward back to the previous page with a variable that will tell that page to show an error, or use put the header into an "else".
    PHP Code:
    if (!mail($recipient$esubject$emessage$extraheaders))
    {
        echo 
    "Mail did not send for some reason.";
    }
    else
    {
        
    header ("location: $redirect");

    i already had that in the first post

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by rafiki View Post
    i already had that in the first post
    Ok, but I wasn't responding to your post. Oh, you have two instances of mail() and you forgot the parenthesis for your if() switch.

  • #7
    New Coder
    Join Date
    Dec 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is the PHP script I have so far that causes a 500 Internal Server Error:

    PHP Code:
    <?
    if ($_POST["Submit"]){

    if (
    $_POST["Name"] and $_POST["Address"] and $_POST["Apt_Suite"] and $_POST["City"] and $_POST["Province"] and $_POST["Postal_Code"] and $_POST["Phone"] and $_POST["Email"] and $_POST["Publication_Title"] and $_POST["#_Copies_English"] and $_POST["#_Copies_French"] and $_POST["Details"]){

    mail (
    "eday2010@gmail.com",
    "Publication Order Request (EN)",
    "
    Name:     "
    .$_POST['Name']."
    Address:     "
    .$_POST['Address']."
    Apt/Suite:     "
    .$_POST['Apt_Suite']."
    City:     "
    .$_POST['City']."
    Province or Territory:     "
    .$_POST['Province']."
    Postal Code:     "
    .$_POST['Postal_Code']."
    Phone:  "
    .$_POST['Phone']."
    E-mail:   "
    .$_POST['Email']."
    Publication Title:  "
    .$_POST['Publication_Title']."
    # of English Copies:  "
    .$_POST['#_Copies_English']."
    # of French Copies:  "
    .$_POST['#_Copies_French']."
    Details/Comments:  "
    .$_POST['Details']."
    "
    ,
    "From: ".$_POST['Name']." <".$_POST['Email'].">");

    header("Location: http://www.wnstudios.ca/en/thankyou2.html"); 
    exit(); 
    }
    ?>
    and here is the page with the form on it: http://www.wnstudios.ca/en/order-commandez.html

  • #8
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    you need to escape some""'s with \"$something\" otherwise the string stops i think
    i got the 500 and
    More information about this error may be available in the server error log.

    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

    check server logs

  • #9
    New Coder
    Join Date
    Dec 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I can't find anything in the error logs. The last errors logged are from the 28th of February. They seem to log once a month. The permission is set correctly on the file, so I am not sure what is wrong.

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Look for a file called "error_log" in the same folder as the file. You're also missing a } at the end of your code, which I'm guessing is just because you didn't post the whole page and missed it. But that shouldn't be causing a 500 error, is this part of a page with more code? Try setting error_reporting(E_ALL); at the start of your script too.
    Last edited by Inigoesdr; 03-06-2007 at 06:30 PM.

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    eday ...

    I suspect you uploaded that script into your "cgi-bin" directory.

    It needs to be in the same directory as your other web pages,
    not the cgi-bin directory. That's for Perl scripts not PHP scripts.

  • #12
    New Coder
    Join Date
    Dec 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't get an error with this script being in the same directory as the form page, but it doesn't load the thank you page. it goes to itself and shows a blank page.

  • #13
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,043
    Thanks
    2
    Thanked 316 Times in 308 Posts
    The code you posted is missing at least one }. Depending on your error_reporting setting, this will either output a fatal parse error message or a blank page. Go back and match up all of your { and }.

    Edit: I guess I will be the "security cop" and tell you to not put any unchecked input from the visitor into the header field of the mail(...) function -
    PHP Code:
    "From: ".$_POST['Name']." <".$_POST['Email'].">"); 
    Doing this will allow a spammer to send email to anyone he wants through your mail server. This will both hurt the reputation of your domain/site and get your mail server on spam databases or banned by the major ISP's, like msn/hotmail, yahoo, gmail...

    Either test the variables that are being put into the header field to make sure they contain only acceptable values and/or don't put them in the header field at all (place them in the message body and make the From: address your own, sending the email from yourself to yourself.)
    Last edited by CFMaBiSmAd; 03-09-2007 at 04:49 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #14
    New Coder
    Join Date
    Dec 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I did match up the curly brackets and put in the missing end one. It still only went to a blank page.

  • #15
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,043
    Thanks
    2
    Thanked 316 Times in 308 Posts
    The following is your submit button from the form -
    Code:
    <input type="submit" name="submitbutton" id="submitbutton2" value="Submit" align="left" />
    The following is from your form processing code -
    Code:
    if ($_POST["Submit"]){
    There is no match between the name of the variable that is set by the form and what is tested in the code...
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •