如何将段落放入PHP联系人表格中?

时间:2019-04-03 13:31:00

标签: php html email contact-form

我用PHP编写了一个联系表单,我想在要发送的消息中包括段落,以便用户不得不写某些段落,而不仅仅是文字块。

我试图将message分成多个textareas,但是我不知道如何调整PHP代码,以便这些textareas可以在{ {1}}的顺序正确。每个message都应该呈现一个段落。

这是我的PHP:

textarea

这是我的HTML:

<?php 
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: mywebsite.com'; 
    $to = 'myemailadress@adress.com'; 
    $subject = 'subject line';
    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html\r\n";
    $headers .= 'From: '. $email. "\r\n" .
    $headers .= "Reply-To: ". $email. "\r\n";
    $headers .= "X-Mailer: PHP/" . phpversion();

    $status = mail($to, $subject, $message, $headers);

    if ($status) { 
        echo '<p style="color: white">Your Message was sent!</p>';
    } else { 
        echo '<p style="color: white">Something went wrong. Please try again.</p>'; 
    }
?>

2 个答案:

答案 0 :(得分:0)

我添加了:

$message = nl2br($message); // Inserts HTML line breaks <br />

这应该插入HTML换行符。

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: mywebsite.com';
$to = 'myemailadress@adress.com';
$subject = 'subject line';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= 'From: ' . $email . "\r\n" .
$headers .= 'Reply-To: ' . $email . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();

$message = nl2br($message); // <-- added to Inserts HTML line breaks.

$status = mail($to, $subject, nl2br($message), $headers);

if ($status) {
    echo '<p style="color: white">Your Message was sent!</p>';
} else {
    echo '<p style="color: white">Something went wrong. Please try again.</p>';
}

答案 1 :(得分:0)

https://www.php.net/manual/en/function.nl2br.php

使用nl2br时,可以将“ \ n”从文本区域更改为“
”。 您可以发送内容类型:文本/普通邮件到,而不必将换行符转换为
标记。

我在您的代码中看不到多个文本区域,所以我想nl2br是您要查找的功能。