Linards Berzins
Linards Berzins

Reputation: 83

PHP email submit form receiver not receiving the correct sendr email address

Im trying to figure out the problem with the PHP code for submit form, im doing for my friend. It is sending the emails through, but the problem is that the receiver gets a very odd email address. I am attaching an image to have a closer look.

My PHP code is:

<?php 
    $error = false;
    $sent = false;

    if(isset($_Post['name'])) {
        if(empty($_Post['name']) || empty($_Post['email']) ||  empty($_Post['comments'])) {
            $error = true;
        } else {

        $to = "[email protected]";

        $name = trim($_Post['name']);
        $email = trim($_Post['email']);
        $comments = trim($_Post['comments']);

        $subject = "Contact Form";

        $messages =  "Name: $name \r\n Email: $email \r\n Comments: $comments";

        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= "From:" . $name . "\r\n";
        $mailsent = mail($to, $subject, $messages, $headers);

        if($mailsent) {
            $sent = true;
        }
    }
}
?>

Many thanks odd email displayed

Upvotes: 0

Views: 583

Answers (6)

Mihai Iorga
Mihai Iorga

Reputation: 39724

It should be like this Sender <[email protected]>:

 $headers .= 'From: '.$name.' <'.$email.'>' . "\r\n";

Upvotes: 7

Mansoorkhan Cherupuzha
Mansoorkhan Cherupuzha

Reputation: 1761

<?php 
    $error = false;
    $sent = false;

    if(isset($_Post['name'])) {
        if(empty($_Post['name']) || empty($_Post['email']) ||  empty($_Post['comments'])) {
            $error = true;
        } else {

        $to = "[email protected]";

        $name = trim($_Post['name']);
        $email = trim($_Post['email']);
        $comments = trim($_Post['comments']);

        $subject = "Contact Form";

        $messages =  "Name: $name \r\n Email: $email \r\n Comments: $comments";

        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: '.$name.' <[email protected]>' . "\r\n";
        $mailsent = mail($to, $subject, $messages, $headers);

        if($mailsent) {
            $sent = true;
        }
    }
}
?>

Try this. just change the header.

$headers .= 'From: '.$name.' <[email protected]>' . "\r\n";

Upvotes: 2

Basith
Basith

Reputation: 1075

Hi This was not an error.. If you provide SENDER EMAIL then it will display the senders email address instead of this.. Otherwise it will take our hosting address.

Upvotes: 0

Mark
Mark

Reputation: 3055

The From: header should include an email address as well as the name, something like

"From:My Display Name<[email protected]>\r\n"

Upvotes: 2

coolmine
coolmine

Reputation: 4463

Add this to your headers

$headers .= "Reply-To: $replyEmail\r\n";

Upvotes: 2

Drazion
Drazion

Reputation: 362

Try adding the headers to the email, like this from PHP mail Manual example 2

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

If you want it to be from an email with a name, this would work

$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";

Upvotes: 2

Related Questions