Results 1 to 7 of 7

Thread: Php contact form sent to e-mail

  1. #1
    Join Date
    Jul 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Cool Php contact form sent to e-mail

    Hi,
    I have made a contact form for my web site and when
    it is sent it should go to my e-mail. The form works, just not consistantly.
    Can you take a look at it and tell me what needs to be changed?
    I don't get how it can work sometimes and then not other times.
    Thanks,


    Code:
    if (isset($_REQUEST['txteMail']))
       {//if "email" is filled out, proceed
     
      //check if the email address is invalid
       $mailcheck = spamcheck($_REQUEST['txteMail']);
     
    
       if ($mailcheck==FALSE)
         {
         echo "Invalid input";
         }
       else
     { //send email
    $to = 'someone@cox.net' . ',' ; // note the comma
    $to .= 'someone@yahoo.com';
    $subject =($_REQUEST['txtSubject']);
    $message =($_REQUEST['txtName']) . "\r  ";
    $message .=($_REQUEST['txtLName']) . "\r\n";
    $message .=($_REQUEST['txtMsg']) . "\r\n";
    $message .= "Checked Boxes: " . $products_msg ."\n";
    $headers =($_REQUEST['txteMail']);
    //mail($to, $subject, $message, $headers);
     mail($to, $subject, $message, $headers ); 
    }
    Last edited by djr33; 08-03-2011 at 08:18 PM.

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    It could be a problem with your SMTP server. Also, can you post your HTML form that sends the data to this script?

    Maybe there is something there that is tripping up the script.

  3. #3
    Join Date
    Nov 2008
    Posts
    58
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default

    Add a 'From' header to $headers
    Also see the link below:
    Why is my PHP script not sending emails?

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Make a simpler email test page, removing everything that is extra. Use one email address, not two, and don't use variables-- just add test content.
    Then test that page to see if it also does not always work. If it does always work, then something is wrong with this current page. If it does not always work, then something is wrong with your server.

    The only thing that looks at all strange to me is the two email addresses, but that is probably irrelevant because if it works sometimes, then that can't be wrong.


    Have you checked your spam folder? Automated emails like that often get placed in spam by email filters.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jul 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    This is the code at the beginning of the page : (and the rest of the form) I have added in a write to text file so that if the e-mail doesn't work I can still check if the form was filled out. I appreciate you looking at this.
    [CODE]
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
    session_start();
    ob_start();
    }
    function spamcheck($field)
    {
    //filter_var() sanitizes the e-mail
    //address using FILTER_SANITIZE_EMAIL
    $field=filter_var($field, FILTER_SANITIZE_EMAIL);

    //filter_var() validates the e-mail
    //address using FILTER_VALIDATE_EMAIL
    if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
    else
    {
    return FALSE;
    }
    }[CODE]



    [CODE]
    //this code puts the checkbox values into a variable that prints out in an email or form
    if (isset($_POST["products"]))
    {
    foreach($_POST['products'] as $value) {
    $products_msg .= "$value, ";
    }
    // prepare email body text for the checkboxes
    $Body .= "products: ";
    $Body .= $products_msg;
    }

    if (isset($_REQUEST['txteMail']))
    {//if "email" is filled out, proceed

    //check if the email address is invalid
    $mailcheck = spamcheck($_REQUEST['txteMail']);


    if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
    else
    { //send email
    $to = 'Someone@cox.net' . ',' ; // note the comma
    $to .= 'someone@yahoo.com';
    $subject =($_REQUEST['txtSubject']);
    $message =($_REQUEST['txtName']) . "\r ";
    $message .=($_REQUEST['txtLName']) . "\r\n";
    $message .=($_REQUEST['txtMsg']) . "\r\n";
    $message .= "Checked Boxes: " . $products_msg ."\n";
    $headers =($_REQUEST['txteMail']);
    //mail($to, $subject, $message, $headers);
    mail($to, $subject, $message, $headers );
    }
    // print to text file for back up
    //load variables
    $filename = "myForm.txt";
    $fp = fopen($filename,"a") or exit("unable to open file");
    $counter = $_SESSION['count'];
    $counter ++;
    $fp = fopen($filename,"a") or exit("unable to open file");
    $date = date("l, F j, Y") . " ";
    $time = date("h:i A") . "\r\n";
    fwrite($fp, "<br />" . $date);
    fwrite($fp, $time);
    fwrite($fp, "<p>Name: " . $_POST["txtName"]) . "\r\n";
    fwrite($fp, " " . $_POST["txtLName"]) . "\r\n </p>";
    fwrite($fp, "<p>Subject: " . $_POST["txtSubject"]) . "\r\n </p>";
    fwrite($fp, "<p>E-Mail: " . $_POST["txteMail"]) . "\r\n </p>";
    fwrite($fp, "<p>Inquery: " . $_POST["txtMsg"]) . "\r\n </p>";
    fwrite($fp, "<p>Checked Boxes: " . $products_msg ) . "\r\n </p>";
    fclose($fp);
    $url = "http://dianesepanski.com/form.php";
    header("Location: $url");
    } [CODE]


    <div id="mainContent" style="color:#666666;">
    <form action="contact.php" method="post" >
    <h2>Contact Form</h2>
    <p>Please fill out the form below.
    Questions, comments or to be added to my e-mail list.<br />
    Updates, newsletter, products and more!</p>
    <div id ="notifyMe" style="float:right;padding-right:28%;text-align:left;">
    <h3 style="margin-left:2%;">Notify me when: </h3>

    <p><input type="checkbox" name="products[]" value="NewProduct" /> New Product is listed for sale<br />
    <input type="checkbox" name="products[]" value="ShowUpdate" />When Show Info is Updated<br />
    <input type="checkbox" name="products[]" value="Newsletter" /> Add Me to Your Newsletter<br />
    <input type="checkbox" name="products[]" value="Blog" />When Blogs Are Updated<br />
    <input type="checkbox" name="products[]" value="ClassPost" />When Classes are Posted<br />
    <input type="checkbox" name="products[]" value="All" /> All of the Above<br />
    </p></div>

    <div id="formInfo" style="padding-left:25%;text-align:left;"><p>
    <label>
    <input type="text" name="txtName" id="txtName" />
    First Name </label>
    </p>
    <p>
    <label>
    <input type="text" name="txtLName" id="txtLName" />Last Name
    </label>
    </p>
    <p>
    <label>
    <input type="text" name="txteMail" id="txteMail" />E-mail </label></p>
    <p>
    <label>
    <input type="text" name="txtSubject" id="txtSubject" />Subject
    </label>
    </p>

    <p> <textarea name="txtMsg" cols="50" rows="6"></textarea>
    Message</p>
    <p> <input name="Submit" type="submit" value="submit" /><input name="Reset" type="reset" value="Reset" /></p>

    </div>
    </form>

  6. #6
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    As Daniel said, make a simpler email page and debug it from there.

    Also, submit your form in different circumstances (like some fields checked, some fields entered, etc). That might affect what is causing the form to send (or not send at all).

  7. The Following User Says Thank You to JShor For This Useful Post:

    shika (08-05-2011)

  8. #7
    Join Date
    Jul 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the advice.
    I have tried adding the "From" to the headers. and It
    sends after a few minutes.
    I will start debugging now and see if it will send any quicker.

Bookmarks

Posting Permissions

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