doudi5892 2012-04-03 10:31
浏览 144
已采纳

发送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");?>
  • 写回答

2条回答 默认 最新

  • dongxuan8227 2012-04-03 11:14
    关注

    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!");
          }
        }
      }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码