dcsimg
www.webdeveloper.com
Results 1 to 8 of 8

Thread: PHP Contact process file

  1. #1
    Join Date
    Jun 2011
    Posts
    141

    PHP Contact process file

    Hi guys,
    I have been using the same contact form for all my sites but only recently noticed during tests (and from comments from one of my "clients") that half of the time, emails dont go through.
    I have just put this code in to my new site to test and the contact form processed fine, got me to my success page which then counted down and redirected me to the home page.
    I did this twice and all seemed fine but I still havent had my test emails in my inbox.
    This is the code I use:
    HTML
    Code:
    <form action="processform.php" method="post">
      <label for="name">Name</label><input type="text" id="name" name="name" />
      <label for="email">Email address</label><input type="text" id="email" name="email" />
      <label for="topic">Subject</label><input type="text" id="topic" name="topic" />
      <textarea id="comments" name="comments" rows="5" cols="30"></textarea>
    
      <button type="submit" id="sendmessage">Send</button>
    </form>
    PHP file
    Code:
    <?php
    // Pick up the form data and assign it to variables
    $name = $_POST['name'];
    $email = $_POST['email'];
    $topic = $_POST['topic'];
    $comments = $_POST['comments'];
    
    // Build the email (replace the address in the $to section with your own)
    $to = 'my e-mail address';
    $subject = "New message: $topic";
    $message = "$name said: $comments";
    $headers = "From: $email";
    
    // Send the mail using PHPs mail() function
    mail($to, $subject, $message, $headers);
    
    // Redirect
    header("Location: success.html");
    And before you say it, I did have my e-mail address in the "to" bit, i've changed it for the purpose of this post.

    Cheers, Adey

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    20,084
    Have you checked your spam folder? (I'm not claiming that's necessarily the most likely problem, but it's the easiest one to check first. )
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

  3. #3
    Join Date
    Jun 2011
    Posts
    141
    Yep, first I put my NHS email address in and sent a test message, nothing in inbox of junk mail, and secondly put my hotmail address in. Again, nothing in inbox or junk mail. Really puzzled!

    Adey

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    20,084
    One common problem that could be the issue here is that many servers only allow the From email address to be a valid email on the host. You may need to set it to your email on that server, then put the address supplied by the user in a "Reply-To:" header.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

  5. #5
    Join Date
    Jun 2011
    Posts
    141
    so amend

    Code:
    // Build the email (replace the address in the $to section with your own)
    $to = 'my e-mail address';
    $subject = "New message: $topic";
    $message = "$name said: $comments";
    $headers = "From: $email";
    to

    Code:
    // Build the email (replace the address in the $to section with your own)
    $to = 'my e-mail address';
    $subject = "New message: $topic";
    $message = "$name said: $comments";
    $headers = "From: adey@email.com";
    $headers = "Reply-To: $email";

    Edit: Just tried this, still nothing :-(
    Last edited by djadejones; 06-18-2011 at 01:37 PM. Reason: tried and tested...

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    20,084
    PHP Code:
    // Build the email (replace the address in the $to section with your own)
    $to 'my e-mail address';
    $subject "New message: $topic";
    $message "$name said: $comments";
    $headers "From: adey@email.com";
    $headers "Reply-To: $email"
    You need to concatenate your headers (From and Reply-To), as right now the 2nd is overwriting the first:
    PHP Code:
    // Build the email (replace the address in the $to section with your own)
    $to 'my e-mail address';
    $subject "New message: $topic";
    $message "$name said: $comments";
    $headers "From: adey@email.com" PHP_EOL "Reply-To: $email"
    The PHP_EOL constant is generally the surest way to have them separated by either "\r\n" or "\n" as appropriate depending on what platform you're on.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

  7. #7
    Join Date
    Jun 2011
    Posts
    141
    Have added

    Code:
    $headers = "From: adey@email.com" . PHP_EOL . "Reply-To: $email" \n;
    and all seems to be fine for now, thanks for your help NogDog :-)

  8. #8
    Join Date
    Aug 2012
    Posts
    1

    Cool How to add a thank you bar

    Quote Originally Posted by djadejones View Post
    Hi guys,
    I have been using the same contact form for all my sites but only recently noticed during tests (and from comments from one of my "clients") that half of the time, emails dont go through.
    I have just put this code in to my new site to test and the contact form processed fine, got me to my success page which then counted down and redirected me to the home page.
    I did this twice and all seemed fine but I still havent had my test emails in my inbox.
    This is the code I use:
    HTML
    Code:
    <form action="processform.php" method="post">
      <label for="name">Name</label><input type="text" id="name" name="name" />
      <label for="email">Email address</label><input type="text" id="email" name="email" />
      <label for="topic">Subject</label><input type="text" id="topic" name="topic" />
      <textarea id="comments" name="comments" rows="5" cols="30"></textarea>
    
      <button type="submit" id="sendmessage">Send</button>
    </form>
    PHP file
    Code:
    <?php
    // Pick up the form data and assign it to variables
    $name = $_POST['name'];
    $email = $_POST['email'];
    $topic = $_POST['topic'];
    $comments = $_POST['comments'];
    
    // Build the email (replace the address in the $to section with your own)
    $to = 'my e-mail address';
    $subject = "New message: $topic";
    $message = "$name said: $comments";
    $headers = "From: $email";
    
    // Send the mail using PHPs mail() function
    mail($to, $subject, $message, $headers);
    
    // Redirect
    header("Location: success.html");
    And before you say it, I did have my e-mail address in the "to" bit, i've changed it for the purpose of this post.

    Cheers, Adey
    Please help
    How can i add a Thank You bar to this i try my way but it didn't work

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles