Email script problem

Discussion in 'Programming' started by rockymeet, Mar 24, 2010.

  1. #1
    Hi I am currently use this code but , this is not working plz help me.

    we use the variables in the mail() function to send an e-mail:
    <?php
    $to = "someone@example.com";
    $subject = "Test mail";
    $message = "Hello! This is a simple email message.";
    $from = "someonelse@example.com";
    $headers = "From: $from";
    mail($to,$subject,$message,$headers);
    echo "Mail Sent.";
    ?>
    <html>
    <body>

    <?php
    if (isset($_REQUEST['email']))
    //if "email" is filled out, send email
    {
    //send email
    $email = $_REQUEST['email'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    mail( "someone@example.com", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you for using our mail form";
    }
    else
    //if "email" is not filled out, display the form
    {
    echo "<form method='post' action='mailform.php'>
    Email: <input name='email' type='text' /><br />
    Subject: <input name='subject' type='text' /><br />
    Message:<br />
    <textarea name='message' rows='15' cols='40'>
    </textarea><br />
    <input type='submit' />
    </form>";
    }
    ?>

    </body>
    </html>
    Attorney Advertising
     
    rockymeet, Mar 24, 2010 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    worked for me, what's the result of the form submit?
     
    shallowink, Mar 24, 2010 IP
  3. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello..

    To start with, you have to be as accurate as possible when programming:
    echo "<form method='post' action='mailform.php' enctype='multipart/form-data'>

    You are using the post method in your form which is good but then u start using $_REQUEST.. Replace them with $_POST to catch the veriables you are posting.

    As for your condition of sending a mail: isset($_REQUEST['email']) I could fill in a form with email only, so without subject or message while still being sent.. You can get really advanced in this, but to keep it basic:

    $err = "";
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    if((isset($email) && $email!=NULL) && (isset($subject) && $subject!=NULL) && (isset($message) && $message!=NULL)){
    // send your mail
    }
    else{
    $err = "The form could not be sent, please fill in:<br/>";
    if($email==NULL){
    $err .= "- your email<br/>";
    }
    if($subject==NULL){
    $err .= "- a subject<br/>";
    }
    if($message==NULL){
    $err .= "- a message<br/>";
    }
    echo $err;
    }
     
    fex, Mar 24, 2010 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    you only use that enctype for file uploads. the regular x-www-form-urlencoded or default is the correct to use. And its the default so there's no need to even bother with including the enctype.
    http://www.w3.org/TR/html401/interact/forms.html#h-17.3
     
    shallowink, Mar 24, 2010 IP
  5. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    True, but isn't it needed cause php is server side scripting?
     
    fex, Mar 24, 2010 IP
  6. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #6
    I don't think that influences it. The multipart/form-data (from what I've read) opens up two connections (ascii/binary) causing extra overhead. Course that overhead is minor if its on an infrequently used form. To me though, the form-data setting is telling the processing script that its going to get a larger than usual form submission. And it should apply no matter what server side script is handling it (perl,php, asp, whatever).
     
    shallowink, Mar 24, 2010 IP