0

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

我的 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;
        }
    }
}
?>

非常感谢显示奇怪的电子邮件

4

6 回答 6

7

它应该是这样的Sender <HIS@EXAMPLE.COM>

 $headers .= 'From: '.$name.' <'.$email.'>' . "\r\n";
于 2012-08-31T11:59:19.780 回答
2

尝试将标题添加到电子邮件中,就像PHP 邮件手册示例 2中的这样

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

如果您希望它来自带有名称的电子邮件,这将起作用

$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
于 2012-08-31T11:58:01.513 回答
2

将此添加到您的标题中

$headers .= "Reply-To: $replyEmail\r\n";
于 2012-08-31T11:59:33.790 回答
2

From: 标头应包括电子邮件地址和名称,例如

"From:My Display Name<mydisplayname@gmail.com>\r\n"
于 2012-08-31T11:59:50.830 回答
2
<?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.' <sender@example.com>' . "\r\n";
        $mailsent = mail($to, $subject, $messages, $headers);

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

试试这个。只需更改标题。

$headers .= 'From: '.$name.' <sender@example.com>' . "\r\n";
于 2012-08-31T12:05:13.150 回答
0

嗨,这不是一个错误。如果您提供SENDER EMAIL,它将显示发件人的电子邮件地址而不是这个。否则它将采用我们的托管地址。

于 2012-08-31T12:01:46.650 回答