SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php mail sent but not received in email

    Hi guys,

    I'm a bit of a novice when it comes to php. I have recently built a "Contact Us" form and I am having difficulties getting the results sent to the desired email address.

    According to the Mail Log, the message is being sent, but never received. Below is the code for the php script for the webform:

    PHP Code:
    <?php

    /* Subject and Email Variables */

        
    $emailSubject 'Little Treasures Contact Form';
        
    $webMaster 'info@littletreasures-shop.co.uk';
        
    /* Message Info */

        
    $NameField $_POST['Name'];
        
    $PhoneField $_POST['Phone'];
        
    $EmailField $_POST['Email'];
        
    $SubjectField $_POST['Subject'];
        
    $MessageField $_POST['Message'];
        
        
    $body = <<<EOD
    <br><hr><br>
    Name: 
    $Name <br>
    Phone: 
    $Phone <br>
    Email: 
    $Email <br>
    Subject: 
    $Subject <br>
    Message: 
    $Message <br>
    EOD;

        
    $headers "From: $Email\n";
        
    $headers .= "Content-type: text/html\n";

    if(
    mail($webMaster$emailSubject$body$headers))
    {
    echo 
    header("Location: http://littletreasures-shop.co.uk/Contact/messagesent/true.html");
    }
    else
    {
    echo 
    header("Location: http://littletreasures-shop.co.uk/Contact/messagesent/false.html");
    }

    ?>
    Any input would be highly appreciated!

    Thank you in advance,

    Ryan.
    Last edited by cpradio; Feb 12, 2013 at 08:06. Reason: Wrapped code in [php] tags

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,118
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    Hi Ryski. Welcome to the forums.

    Do you mean that you receive an email, but it's blank? If so, the issue is simple. Instead of this:

    Code:
    Name: $Name <br>
    Phone: $Phone <br>
    Email: $Email <br>
    Subject: $Subject <br>
    Message: $Message <br>
    you need this:

    Code:
    Name: $NameField <br>
    Phone: $PhoneField <br>
    Email: $EmailField <br>
    Subject: $SubjectField <br>
    Message: $MessageField <br>
    You don't actually have any variables called "Name", "Phone" etc.

    Also change this:

    Code:
    $headers = "From: $Email\n";
    to

    Code:
    $headers = "From: $EmailField\n";
    See if that helps.
    Last edited by cpradio; Feb 12, 2013 at 08:06. Reason: corrected typo ;)
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  3. #3
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi ralph,

    Thank you for the response. It's not that the email is blank, the email just doesn't land in the inbox/spam folder at all, but the server log shows that it has been sent. I've tried using different target emails (gmail, yahoo, hotmail, etc.) it appears that the email has been sent. However, it never reaches its destination.

    Tried the suggested adjustments with the same results. it's amazing how something so simple can be so frustrating!

  4. #4
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,118
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    You might need to give us some more information. For example, show us the HTML code for your form, and explain where this PHP is in relation to that form.
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  5. #5
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the code for the form is:

    HTML Code:
    <form action="contact.php" method="post" id="contact">
                <label for="Name2"><strong> Name </strong><br/>
                </label>
                <input name="Name" type="text" id="Name2" size="45">
                <label for="Phone"><strong><br/>
                  Phone Number<br/>
                </strong></label>
                <input name="Phone" type="text" id="Phone" size="45">
                <strong>
                  <label for="Email"><br/>
                    Email<br/>
                  </label>
                </strong>
                <input name="Email" type="text" id="Email" size="45">
                <label for="Subject"><strong><br/>
                  Subject<br/>
                </strong></label>
                <input name="Subject" type="text" id="Subject" size="45">
                <label for="Message"><strong><br/>
                  Message<br/>
                </strong></label>
                <textarea name="Message" id="Message" cols="35" rows="3"></textarea>
                <br/>
                <input type="submit" name="Send" id="Send" value="Submit">
                <input type="reset" name="Reset" id="Reset" value="Reset">
              </form>
    and the php script is in the same directory as the index for the contact page.
    Last edited by cpradio; Feb 12, 2013 at 08:08. Reason: Added [html] tags

  6. #6
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,118
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    As an experiment, try replacing the contents of your contact.php file with this code:

    PHP Code:
    <?php

    $your_email 
    "info@littletreasures-shop.co.uk"// email address to which the form data will be sent
    $subject "Little Treasures Contact Form"// subject of the email that is sent

    if (!isset($_POST['Send'])) {
        
    header"Location: http://littletreasures-shop.co.uk/Contact/index.html" );
      }

    if (isset(
    $_POST["Send"])) {
        
    $nam $_POST["Name"];
        
    $pho $_POST["Phone"];
        
    $ema trim($_POST["Email"]);
        
    $sub $_POST["Subject"];
        
    $com $_POST["Message"];

        if (
    get_magic_quotes_gpc()) { 
        
    $nam stripslashes($nam);
        
    $ema stripslashes($ema);
        
    $pho stripslashes($pho);
        
    $sub stripslashes($sub);
        
    $com stripslashes($com);
        }

    $error_msg=array(); 

    if (empty(
    $nam) || !preg_match("~^[a-z\-'\s]{1,60}$~i"$nam)) { 
    $error_msg[] = "The name field must contain only letters, spaces, dashes ( - ) and single quotes ( ' )";
    }

    if (empty(
    $ema) || !filter_var($emaFILTER_VALIDATE_EMAIL)) {
        
    $error_msg[] = "Your email must have a valid format, such as name@mailhost.com";
    }

    if (empty(
    $pho) && !preg_match("/^[A-z\/0-9\s\(\)]{1,60}$/"$pho)) { 
    $error_msg[]="The phone field can contain only digits, spaces and parentheses";
    }

    if (empty(
    $sub) || !preg_match("~^[a-z\-'\s]{1,60}$~i"$sub)) { 
    $error_msg[] = "The subject field must contain only letters, spaces, dashes ( - ) and single quotes ( ' )";
    }

    $limit 1000;

    if (empty(
    $com) || !preg_match("/^[0-9A-Za-z\/-\s'\(\)!\?\.,]+$/"$com) || (strlen($com) > $limit)) { 
    $error_msg[] = "The Comments field must contain only letters, digits, spaces and basic punctuation (&nbsp;'&nbsp;-&nbsp;,&nbsp;.&nbsp;), and has a limit of 1000 characters";
    }

    // Assuming there's an error, refresh the page with error list and repeat the form

    if ($error_msg) {
    echo 
    '<!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Error</title>
    <style>
        body {background: #f7f7f7; font: 100%/1.375 georgia, serif;padding: 20px 40px;}
        div {margin-bottom: 10px;}
        .content {width: 40%; margin: 0 auto;}
        h1 {margin: 0 0 20px 0; font-size: 175%; font-family: calibri, arial, sans-serif;}
        fieldset {border: 0; padding: 0; margin: 0;}
        legend {position: absolute; left:-9999px}
        label {margin-bottom: 2px;}
        input[type="text"], input[type="email"], textarea {font-size: 0.75em; width: 98%; font-family: arial; border: 1px solid #ebebeb; padding: 4px; display: block;}
        input[type="radio"] {margin: 0 5px 0 0;}
        textarea {overflow: auto;}
        .hide {display: none;}
        .err {color: red; font-size: 0.875em; margin: 1em 0; padding: 0 2em;}
    </style>
    </head>
    <body>
        <div class="content">
            <h1>O dear!</h1>
            <p>Unfortunately, your message could not be sent. The form as you filled it out is displayed below. Please make sure each field is completed, and please also address any issues listed below:</p>
            <ul class="err">'
    ;
    foreach (
    $error_msg as $err) {
    echo 
    '<li>'.$err.'</li>';
    }
    echo 
    '</ul>
        <form method="post" action="'
    $_SERVER['PHP_SELF'], '">
            <fieldset> 
                <legend>Contact Us</legend>
                    <div>
                        <label for="name">Name</label>
                        <input name="Name" type="text" size="40" maxlength="60" id="name" value="'
    ; if (isset($_POST["Name"])) {echo $nam;}; echo '">
                    </div>
                    <div>
                        <label for="email">Email Address</label>
                        <input name="Email" type="email" size="40" maxlength="60" id="email" value="'
    ; if (isset($_POST["Email"])) {echo $ema;}; echo '">
                    </div>
                    <div>
                        <label for="phone">Telephone</label>
                        <input name="Phone" type="text" size="40" maxlength="60" id="phone" value="'
    ; if (isset($_POST["Phone"])) {echo $pho;}; echo '">
                    </div>
                    <div>
                        <label for="subject">Subject</label>
                        <input name="Subject" type="text" size="40" maxlength="60" id="subject" value="'
    ; if (isset($_POST["Subject"])) {echo $sub;}; echo '">
                    </div>
                    <div>
                        <label for="comm">Comments</label>
                        <textarea name="Message" rows="10" cols="50" id="comm">'
    ; if (isset($_POST["Message"])) {echo $com;}; echo '</textarea>
                    </div>
                    <div>
                        <input type="submit" name="Send" value="Send">
                    </div>
            </fieldset>
        </form>
    </body>
    </html>'
    ;
    exit();


    $email_body 
        
    "Name: $nam\n\n" .
        
    "Email: $ema\n\n" .
        
    "Telephone: $pho\n\n" .
        
    "Subject: $sub\n\n" .
        
    "MESSAGE:\n\n" .
        
    "$com

    // Assuming there's no error, send the email and redirect to Thank You page

    if (isset($_REQUEST['MESSAGE']) && !$error_msg) {
    mail ($your_email$subject$email_body"From: $nam <$ema>" "\r\n" "Reply-To: $nam <$ema>");
    header ("Location: http://littletreasures-shop.co.uk/Contact/messagesent/true.html");
    exit();
    }  
    }
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  7. #7
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, tried that. Now the email doesn't appear in the Sendmail log and it displays a blank page when you click submit. Still no email in the inbox/junk folder either. Could it be a server side issue?

    EDIT: the site is hosted on serversfree.com

  8. #8
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,118
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    Hm, sounds like a dud server to me. Does it support php?
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  9. #9
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,118
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    Ah man, I'm such an eejit: I made one mistake at the end of that code in post #6. If you correct the error in red below, I reckon things should work fine. In the final lines of the code, change this:

    Code:
    if (isset($_REQUEST['MESSAGE']) && !$error_msg) {
    mail ($your_email, $subject, $email_body, "From: $nam <$ema>" . "\r\n" . "Reply-To: $nam <$ema>");
    header ("Location: http://littletreasures-shop.co.uk/Contact/messagesent/true.html");
    exit();
    }  
    }
    to this:

    Code:
    if (isset($_REQUEST['Message']) && !$error_msg) {
    mail ($your_email, $subject, $email_body, "From: $nam <$ema>" . "\r\n" . "Reply-To: $nam <$ema>");
    header ("Location: http://littletreasures-shop.co.uk/Contact/messagesent/true.html");
    exit();
    }  
    }
    And forget what I said about the hosting. The serversfree.com website clearly states that PHP is supported.
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  10. #10
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works perfectly! You sir, are an absolute legend!

    Thank you very much for your help.

  11. #11
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,118
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    Ah, great. Glad to hear that.

    It also has security protection now. One thing you might want to change is the error page. If you just hit submit without filling in the fields, you get a pretty plain Error page. This could be styled to look like the rest of your site. (Just thought I'd better point that out.) If you need help with that part of it, just shout.
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."


Tags for this Thread

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
  •