请我尝试在换行时将消息显示在新行上,但是我在每行之间得到/r/n
并且还试图让$body .= $_SESSION['username'];
出现在另一行我有尝试使用this example来解决但未成功的代码在
<?php require_once("include/session.php");?>
<?php require_once("include/dataconnect.php");?>
<?php require_once("include/functions.php");?>
<?php include("include/mheader.php");?>
<?php
$submit = $_POST['Notify'];
$message = mysql_real_escape_string(htmlentities(strip_tags($_POST['message'])));
//echo "$message";
//die();
if('POST' === $_SERVER['REQUEST_METHOD'])
{
if (isset($message))
{
//Get Email Address
$emails = mysql_query("SELECT email FROM reusers WHERE username = '{$_SESSION['username']}'")or die(mysql_error());
//$emails = mysql_query("SELECT reusers.email FROM reusers INNER JOIN repplac ON reusers.username = repplac.Uname AND reusers.username = '".$_SESSION['username']."'")or die(mysql_error());
$results = (mysql_fetch_assoc($emails)) or die(mysql_error());
$email= $results['email'];
//echo "$email";
//die();
if(mysql_num_rows($emails) == 0){
exit("No email addresses found for user '{$_SESSION['username']}'");
}
$email = mysql_result($emails, 0);
//echo "$email";
//die();
$body = $_SESSION['username']. "<br>"
. nl2br($_POST['message']);
$to = $email;
$subject = "copy of your notification";
$headers = "From: noti@r.co.uk\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'Bcc:noti@r.co.uk' . "\r\n";
mail($to,$subject,$body,$headers);
}
}
?>
<p>
<form action='notification.php' method='Post' class='rl'>
<div>
<label for='message' class='fixedwidth'>Message</label>
<textarea name="message" rows ="7" cols="40" id="message"></textarea>
</div>
<div class='buttonarea'>
<p>
<input type='submit' name='notify' value='Notify'>
</p>
</div>
</form>
</p>
<?php include("include/footer.php");?>
答案 0 :(得分:1)
由于以更古老的HTML格式发送HTML电子邮件通常更安全,因此我将允许HTML电子邮件内容为HTML 4;所以它不需要格式良好,nl2br()
是可以接受的。
您指定电子邮件的内容为HTML,因此正常的行结尾,\r
,\n
和\r\n
几乎无关紧要。
尝试类似:
$body = $_SESSION['username']. "<br>"
. nl2br($_POST['message']);
那里没有进行健全性检查或验证,但我认为这就是你要做的事情。
----示例代码----
我刚刚对你的代码进行了一些重构,所以我可以更好地了解你正在做什么(这只是个人偏好的问题),然后发表评论来展示我在理智检查和验证方面所取得的成就。
我没有测试任何这个,它只是使用你的代码的一个例子。
<?php
require_once "include/session.php";
require_once "include/dataconnect.php";
require_once "include/functions.php";
require_once "include/mheader.php";
//sanity checks - ensure the form has been posted and that there IS a message
if($_POST && !empty($_POST['message'])) {
//sanity check - ensure there IS a username
$sUsername = !empty($_SESSION['username']) ? $_SESSION['username'] : "";
if($sUsername) {
//check the username against the database?
$resultEmail = mysql_query("SELECT `email` FROM `reusers` WHERE `username` = '{$sUsername}' LIMIT 0, 1")
or die(mysql_error());
//no result - could throw an Exception here
if(mysql_num_rows($resultEmail) == 0) {
die("No email addresses found for user '{$sUsername}'");
}
//email verified against the database
else {
$sEmail = mysql_result($resultEmail, 0);
//create the email
$headers = "From: noti@r.co.uk\r\n"
. 'MIME-Version: 1.0' . "\r\n"
. 'Content-type: text/html; charset=iso-8859-1' . "\r\n"
. 'Bcc:noti@r.co.uk' . "\r\n";
$to = $sEmail; //assuming the email address retrieved from the database has already been mxrr checked etc...
$subject = "copy of your notification";
$body = $sUsername . "<br>"
//remove slashes as this is going in an email, strip tags and convert newlines to "<br>"
// since you're using iso-8859-1 there shouldn't be any oddities unless someone completes
// the form using an Arabic character set (for instance)
. nl2br(strip_tags(stripslashes($_POST['message'])));
//send the email
if(!mail($to, $subject, $body, $headers)) {
die("sendmail error!");
}
}
}
}
?>
答案 1 :(得分:0)
试试这个;
$body = "Username" . "<br>";
$body .= "test1 message for test 1 message for test 1message for test 1message for test 1message for test 1message for test 1message for test 1message for test 1message for test 1message for test 1message for test 1message for test 1message for test 1message for test 1message for test 1message for test 1
test2 message for test 2
test3 message for test 3";
$body = nl2br($body);
以及我得到的内容