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

问题描述:

我正在尝试找出用于提交表单的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.

我的PHP代码是:

<?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 \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;
        }
    }
}
?>

非常感谢

发件人应该是这样的发件人< HIS @ EXAMPLE.COM>

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