简体   繁体   中英

PHP mail reply to sender instead of server email

Can anyone point out where am I getting this form wrong? Instead of being able to reply to the " email " from the sender I get the server email.

Here is the php code:

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: business.com'; 
    $to = 'anyone@gmail.com'; 
    $subject = 'A new Message from your website business.com';
    $human = $_POST['human'];
    $headers = 'From: info@business.com' . "\r\n" .
    'Reply-To: ' . $email . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
    if ($name != '' && $email != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
    } else {
        echo '<p>You need to fill in all required fields!!</p>';
    }
}
?>

and here is the html

<form method="post" action="contact.php" target="contactIframe" name="contact">
    <label>Name</label>
    <input name="name" placeholder="Type Here">
    <label>Email</label>
    <input name="email" type="email" placeholder="Type Here">
    <label>Message</label>
    <textarea name="message" placeholder="Type Here"></textarea></br>
    <label>*What is 2+2? (Anti-spam)</label>
    <input name="human" placeholder="Type Here"></br></br>
    <input id="submit" name="submit" type="submit" value="Submit">
    <input type="button" name="reset_form" value="Clear" onclick="this.form.reset();">
</form>

Thanks everyone

You mail function is does not have $headers as additional headers. The code should be...

 if ($human == '4') {                 
    if (mail ($to, $subject, $body, $headers)) { 
       echo '<p>Your message has been sent!</p>';
    }
 }

ref : PHP Mail Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM