SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Contact form header trouble

    Hello everyone,

    I wrote a PHP contact script with form validation in it and everything, and it works. The thing is, I want to be able to hit reply from the message that was sent to me. I'm not sure how to make the email look like it was sent by the person who wrote the message. I assume this would be done with header definitions, but I'm not sure how to do this. Can anyone help me out? I just need a rough idea.

    PHP Code:
    <?php

    /* GET VARIABLES */

    $name trim($_POST['name']);
    $address trim($_POST['email']);
    $number trim($_POST['phone']);
    $subject trim($_POST['subject']);
    $message trim($_POST['message']);
    $to "me@onlyme.com";

    /* TESTS */

    if ($name == "your full name") {
        print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact.php/?s=2#form\">";
    } else if (
    $address == "your email address") {
        print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact.php/?s=2#form\">";
    } else if (
    $number == "phone number with area code") {
        print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact.php/?s=2#form\">";
    } else if (
    $subject == "what is this all about?") {
        print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact.php/?s=2#form\">";
    } else if (
    $to == "questions? comments? need a website? do tell us!") {
        print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact.php/?s=2#form\">";
    } else {

            
    /* CREATE MESSAGE */
            
            
    $email "";
            
    $email .= "From: ";
            
    $email .= $name " <" $address ">";
            
    $email .= "\r\n";
            
    $email .= "Phone: ";
            
    $email .= $number;
            
    $email .= "\r\n\r\n";;
            
    $email .= $message;
            
            
    /* SEND */
            
            
    mail($to$subject$email);
            
            
    /* REDIRECT */

            
    if (mail){
              print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact.php/?s=1#form\">";
            }
            else{
              print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=contact.php/?s=2#form\">";
            }
    }

    ?>

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, you already do that, it's From: email@example.com header.

    On a side note, please do the user input validation.
    Saul

  3. #3
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried modifying it some. This I thought should theoretically work, but it didn't. I can't figure out why.

    PHP Code:
    /* HEADERS */
            
    $headers 'From: $address' "\r\n" .
    'Reply-To: $address' "\r\n" .
    'X-Mailer: PHP/' phpversion();
            
    /* SEND */
            
    mail($to$subject$email$headers); 
    Help, please?

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hm, do post the form itself; the code seems fine (despite the lack of validation).
    Saul

  5. #5
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's no need to post the form itself, all the variables are pulling correctly. The email is coming to me as from "$address"@cl00.gs00.gridserver.com. (I changed the numbers slightly). But everything else is coming correctly, so the header code is written wrong.

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Oh right, didn't notice before, you're using single quotes and the variable within. Anything between single quotes is not parsed and is used as it is. You want to use double quotes there or break the string:

    Code php:
    $headers = 'From: '.$address. "\r\n" .
    'Reply-To: '.$address . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
    Saul


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
  •