PHP电子邮件提交表单接收者未收到正确的发件人电子邮件地址

问题描述:

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 = "linardsberzins@gmail.com";

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

        $subject = "Contact Form";

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

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

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

Many thanks odd email displayed

我试图找出提交表单的PHP代码的问题,即时为我的朋友做。 它正在发送电子邮件,但问题是接收者得到一个非常奇怪的电子邮件地址。 我附上一张图片仔细看看。 p>

我的PHP代码是: p>

 &lt;?php 
 $ error = false; 
 $ sent = false; 
  
 if(isset($ _ Post ['name'])){
 if(empty($ _ Post ['name'])|| empty($ _ Post ['email'])|| empty($ _ Post ['  comments'])){
 $ error = true; 
} else {
 
 $ to =“linardsberzins@gmail.com”; 
 
 $ name = trim($ _ Post ['name'])  ; 
 $ email = trim($ _ Post ['email']); 
 $ comments = trim($ _ Post ['comments']); 
 
 $ subject =“联系表格”; 
 
 $  messages =“姓名:$ name 
 
电子邮件:$ email 
 
评论:$ comments”; 
 
 $ headers ='MIME-Version:1.0'。  “
 
”; 
 $ headers。='Content-type:text / html;  charset = iso-8859-1'。  “
 
”; 
 $ headers。=“From:”。  $ name。  “
 
”; 
 $ mailsent = mail($ to,$ subject,$ messages,$ headers); 
 
 if if($ mailsent){
 $ sent = true; 
} 
}  
} 
?&gt; 
  code>  pre> 
 
 

非常感谢 p> div>

It should be like this Sender <HIS@EXAMPLE.COM>:

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

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

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "
" .
    'Reply-To: webmaster@example.com' . "
" .
    '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 <birthday@example.com>' . "
";

Add this to your headers

$headers .= "Reply-To: $replyEmail
";

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

"From:My Display Name<mydisplayname@gmail.com>
"

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.

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

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

        $to = "linardsberzins@gmail.com";

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

        $subject = "Contact Form";

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

        $headers  = 'MIME-Version: 1.0' . "
";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
        $headers .= 'From: '.$name.' <sender@example.com>' . "
";
        $mailsent = mail($to, $subject, $messages, $headers);

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

Try this. just change the header.

$headers .= 'From: '.$name.' <sender@example.com>' . "
";