发送php邮件的换行问题

发送php邮件的换行问题

问题描述:

please i am trying to make the message show on newline as the customer types it, but i am getting /r/n between each line and also trying to make the $body .= $_SESSION['username']; appear on a separate line i have tried to use this example to solve but has not been successful the code is below

   <?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
";  
$headers  .= 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
$headers .= 'Bcc:noti@r.co.uk' . "
";
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");?>

Since it's generally safer to send HTML emails in a more archaic form of HTML I'm going to allow the HTML email content to be HTML 4; so it doesn't need to be XML well formed and nl2br() is acceptable.

You're specifying that the content of your email is HTML so normal line endings, , and are pretty much irrelevant.

Try something like:

$body = $_SESSION['username']. "<br>"
         . nl2br($_POST['message']);

There's no sanity checks or validation in there but I think that's what you're trying to get it to do.

---- EXAMPLE CODE ----

I've just refactored your code somewhat so I could better see what you're doing (it's just a matter of personal preference) and put comments in to show what I'm getting at with regards to sanity checks and validation.

I've not tested any of this, it's pretty much just an example using your code.

<?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
"
            . 'MIME-Version: 1.0' . "
"
            . 'Content-type: text/html; charset=iso-8859-1' . "
"
            . 'Bcc:noti@r.co.uk' . "
";

      $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!");
      }
    }
  }
}
?>

try this;

$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);

and here what i got enter image description here