How do I print the sender's info in the "from" field in my email contact PHP script?

Discussion in 'HTML / PHP / JavaScript / CSS' started by lead2crm, Dec 25, 2010.

  1. The following script sends email to me; however, it comes from "[email protected]" instead of the email address entered by the user in the contact form. How do I place their first name, last name and email address in the actual "from" area in outlook?

    Code:
    <?php
    if(isset($_POST['submit'])) {
    
    $to = "[email protected]";
    $subject = "Inquiry";
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $state = $_POST['state'];
    $country = $_POST['country'];
    $company = $_POST['company'];
    $jobrole = $_POST['jobrole'];
    $businessphone = $_POST['businessphone'];
    $buyinghorizon = $_POST['buyinghorizon'];
    $message = $_POST['message'];
    
    $body = "From: $firstname $lastname\n E-Mail: $email\n State: $state\n Country: $country\n Company: $company\n Job Role: $jobrole\n Business Phone: $businessphone\n Buying Horizon: $buyinghorizon\n Message:\n $message";
    
    echo "Data has been submitted to $to!";
    mail($to, $subject, $body);
    
    } else {
    
    echo "blarg!";
    
    }
    ?>
    
     
  2. mjp

    mjp

    By default php uses a "From" address defined in the php.ini file (which for most hosts will be [email protected] or hostname.com). You need to add a headers variable and populate the From address with the user input. Something like this (changes are commented):

    PHP:
    <?php
    if(isset($_POST['submit'])) {

    $to "[email protected]";
    $subject "Inquiry";
    $firstname $_POST['firstname'];
    $lastname $_POST['lastname'];
    $email $_POST['email'];
    $state $_POST['state'];
    $country $_POST['country'];
    $company $_POST['company'];
    $jobrole $_POST['jobrole'];
    $businessphone $_POST['businessphone'];
    $buyinghorizon $_POST['buyinghorizon'];
    $message $_POST['message'];

    // populate From header
    $headers 'From: $email';

    $body "From: $firstname $lastname\n E-Mail: $email\n State: $state\n Country: $country\n Company: $company\n Job Role: $jobrole\n Business Phone: $businessphone\n Buying Horizon: $buyinghorizon\n Message:\n $message";

    echo 
    "Data has been submitted to $to!";

    // add headers
    mail($to$subject$message$headers);

    } else {

    echo 
    "blarg!";

    }
    ?>
    I don't recall off the top of my head whether you need all the header variables or you can use just one like "From." I'm pretty sure you can use one, but you might want to double check the manual.
     
  3. This placed $email into the email headers area, but it did not parse. The email was literally sent from $email and not [email protected]
     
  4. Bruce

    Bruce DiscountASP.NET Staff

    as stated in manual, the email header should look like this

    User <[email protected]>

    ie. $Firstname $lastname <$email>
     
  5. PHP:
    <?php
    if(isset($_POST['submit'])) {

    $to "[email protected]";
    $subject "Inquiry";
    $firstname $_POST['firstname'];
    $lastname $_POST['lastname'];
    $email $_POST['email'];
    $state $_POST['state'];
    $country $_POST['country'];
    $company $_POST['company'];
    $jobrole $_POST['jobrole'];
    $businessphone $_POST['businessphone'];
    $buyinghorizon $_POST['buyinghorizon'];
    $message $_POST['message'];


    // populate From header
    $headers 'From: $firstname $lastname <$email>';

    $body "From: $firstname $lastname\n Email: $email\n State: $state\n Country: $country\n Company: $company\n Job Role: $jobrole\n Business Phone: $businessphone\n Buying Horizon: $buyinghorizon\n Message:\n $message";

    header("Location: http://www.url.com/thanks/");

    // add headers
    mail($to$subject$body$headers);

    } else {

    echo 
    "blarg!";

    }
    ?>
    Bruce,

    The above code has the suggestion you made. This still doesn't allow the fields to parse into the actual email. The "from" section shows exactly what is written in code: $firstname $lastname <$email>
     
  6. mjp

    mjp

    I know I've done this before, let me see if I can dig up the code.
     
  7. MJP--Did you find anything? This is till printing the php code rather than the value of the php code int he email headers
     
  8. mjp

    mjp

    Sorry, it's old code so I was going to check my backups at home, but I left town for a few days and forgot to do it.

    I think I see the problem though. I steered you wrong when I typed that $headers line - it should be double quoted, not single quoted:

    PHP:
    $headers "From: $firstname $lastname <$email>";
    not:

    PHP:
    $headers 'From: $firstname $lastname <$email>';
    php parses a string in double quotes and outputs the contents of a string in single quotes.

    I should have tested that before I posted it. :(
     
  9. That would have been a bit much for free support. I appreciate the thought, though.

    This worked. :)
     

Share This Page