Xroad
Xroad

Reputation: 425

PHP processing form : unknown sender

I created a form with a processing PHP file. Everything works fine. I receive the mail but it's from "Unknow sender" in Gmail. Why please ?

I would like to see in my email box the name and the firstname of the person who fills the form. What's wrong in my code ?

<?php


if(isset($_POST) && isset($_POST['form3_firstname']) && isset($_POST['form3_name']) && isset($_POST['form3_email']) && isset($_POST['form3_telephone']) && isset($_POST['form3_message'])) {
    extract($_POST);
    if(!empty($form3_firstname) && !empty($form3_name) && !empty($form3_email) && !empty($form3_telephone) && !empty($form3_message)) {

        $to = '[email protected]'; // My real email

        $subject = 'Contact from the site';

        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
        $headers .= 'From:' .$form3_firstname. " " .$form3_name. "\r\n";
        $headers .= 'Reply-To:'.$form3_email. "\r\n";

        $message = '<html><body>';
        $message .= '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>';
        $message .= '<table>';
        $message .= '<tr><td colspan="2"><p>MESSAGE</p></td></tr>';
        $message .= '<tr><td>Firstname :</td><td>'.$form3_firstname.'</td></tr>';
        $message .= '<tr><td>Name :</td><td>'.$form3_name.'</td></tr>';
        $message .= '<tr><td>Email :</td><td>'.$form3_email.'</td></tr>';
        $message .= '<tr><td>Telephone :</td><td>'.$form3_telephone.'</td></tr>';
        $message .= '<tr><td>Message :</td<td>'.stripslashes($form3_message).'</td></tr>';
        $message .= '</table>';
        $message .= '</body></html>';


        if(mail($to, $subject, $message, $headers)){
            echo "Form sent";
        } else {
            echo "Form not sent";
        }

    } else {
        echo "You have not filled in the field";
    }
}


?>

Upvotes: 1

Views: 1594

Answers (3)

Replace the $form3_name with $form3_email

$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\r\n";
                                            ^^^^^^^^^^^^ //<----- Here

That's a name , not an email address , and that's the reason you get that error.

Also, you need to wrap them in tags <>

The right way..

$headers .= 'From:' .$form3_firstname. " ".'<'.$form3_email.'>'."\r\n";

Upvotes: 2

Nishant Solanki
Nishant Solanki

Reputation: 2128

replace "\r\n" with "\n" and your problem will be solved... and also put return-path in your headers...

    $headers = 'MIME-Version: 1.0' . "\n";
    $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
    $headers .= 'From: \''.$form3_firstname.'\' <'.$form3_firstname.'>\r\nReturn-Path: <'.$form3_firstname.'>
    $headers .= 'From:' .$form3_firstname. " " .$form3_name. "\n";
    $headers .= 'Reply-To:'.$form3_email. "\n";

please let me know if you want furtther guidance...

Upvotes: 1

Roemer
Roemer

Reputation: 1271

Because you do not supply an emailaddress in your "From:" header. There always needs to be an emailaddress.

Try something like: $headers .= "From: $form3_firstname $form3_name <$form_email>\r\n";

Mind you, you may have to test and/or escape your form variables; for instance, check that there is no newline in there, otherwise your form might be abused for spamming.

Upvotes: 0

Related Questions