3

我已经查看了一些解决方案,但都没有奏效。

<?php
    $name = $_POST['name'] . "<br>" ;
    $email = $_POST['email'] . "/r/n";
    $phone = $_POST['phone'] . "/r/n";
    $message = $_POST['message'] . "/r/n";
    $from = 'From: email@place.com';
    $to = 'mail@otherplace.com';
    $subject = 'subject here';

    $body = "From: $name E-Mail: $email Phone: $phone $message"
?>
<?php
    if ($_POST['submit']) {
        if (mail ($to, $subject, $body, $from)) {
            echo '<p>Your message has been sent</p>';
        }else{
            echo '<p>Something went wrong, go back and try again.</p>';
        }
    }
?>

这不仅在一行上显示所有内容,而且还显示 /r/n 和
标签,就好像它们只是文本一样。

4

2 回答 2

13

简单的:

你需要改变你所有/r/n\r\n

现在为了<br>有效,您需要将其添加到您的headers

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

mail()有关更多信息,请参阅 PHP 手册headers()

于 2013-10-30T04:50:41.640 回答
2

<br>不会工作,因为你没有发送 HTML 电子邮件。为什么不简单地在HEREDOC块中构造正文文本,例如

$name = isset($_POST['name']) ? $_POST['name'] : 'Not set';
$email = isset($_POST['email']) ? $_POST['email'] : 'Not set';
$phone = isset($_POST['phone']) ? $_POST['phone'] : 'Not set';
$message = isset($_POST['message']) ? $_POST['message'] : null;

$body = <<<_TXT
From: $name
E-mail: $email
Phone: $phone

$message
_TXT;
于 2013-10-30T04:54:22.190 回答