PHP 换行符“.\n”在电子邮件正文中不起作用。线条正在消失

PHP break line ".\n" isn't working in email body. Lines are dissapearing

提问人:in2d 提问时间:1/15/2018 最后编辑:in2d 更新时间:1/15/2018 访问量:414

问:

我怎样才能在 和 之后断线?现在它只显示为输出,但是当我删除“”时,它们都集中在一行上。$sender_name$sender_number$sender_name\n

    $body .= chunk_split(base64_encode($sender_name."\n\r"));
    $body .= chunk_split(base64_encode($sender_number."\n\r"));
    $body .= chunk_split(base64_encode($sender_message."\n\r"));

整个PHP代码

<?php
if($_POST && isset($_FILES['file']))
{
    $recipient_email    = "[email protected]"; //recepient
    $from_email         = "[email protected]"; //from email using site domain.
    $subject            = "Attachment email from your website!"; //email subject line

    $sender_name = filter_var($_POST["s_name"], FILTER_SANITIZE_STRING); //capture sender name
    $sender_number = filter_var($_POST["s_number"], FILTER_SANITIZE_STRING); //capture sender number
    $sender_email = filter_var($_POST["s_email"], FILTER_SANITIZE_STRING); //capture sender email
    $sender_message = filter_var($_POST["s_message"], FILTER_SANITIZE_STRING); //capture message
    $attachments = $_FILES['file'];

    //php validation
    if(strlen($sender_name)<4){
        die('Name is too short or empty');
    }
    if(strlen($sender_number)<4){
        die('Number is too short or empty');
    }
    if (!filter_var($sender_email, FILTER_VALIDATE_EMAIL)) {
      die('Invalid email');
    }
    if(strlen($sender_message)<4){
        die('Too short message! Please enter something');
    }

    $file_count = count($attachments['name']); //count total files attached
    $boundary = md5(""); 

    if($file_count > 0){ //if attachment exists
        //header
        $headers = "MIME-Version: 1.0\r\n"; 
        $headers .= "From:".$from_email."\r\n"; 
        $headers .= "Reply-To: ".$sender_email."" . "\r\n";
        $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 

        //message text
        $body = "--$boundary\r\n";
        $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
          $body .= chunk_split(base64_encode($sender_name)) . "<br />";
  $body .= chunk_split(base64_encode($sender_number)) . "<br />";
  $body .= chunk_split(base64_encode($sender_message)) . "<br />";

        //attachments
        for ($x = 0; $x < $file_count; $x++){       
            if(!empty($attachments['name'][$x])){

                if($attachments['error'][$x]>0) //exit script and output error if we encounter any
                {
                    $mymsg = array( 
                    1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini", 
                    2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", 
                    3=>"The uploaded file was only partially uploaded", 
                    4=>"No file was uploaded", 
                    6=>"Missing a temporary folder" ); 
                    die($mymsg[$attachments['error'][$x]]); 
                }

                //get file info
                $file_name = $attachments['name'][$x];
                $file_size = $attachments['size'][$x];
                $file_type = $attachments['type'][$x];

                //read file 
                $handle = fopen($attachments['tmp_name'][$x], "r");
                $content = fread($handle, $file_size);
                fclose($handle);
                $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)

                $body .= "--$boundary\r\n";
                $body .="Content-Type: $file_type; name=" . $file_name ."\r\n";
                $body .="Content-Disposition: attachment; filename=" . $file_name ."\r\n";
                $body .="Content-Transfer-Encoding: base64\r\n";
                $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
                $body .= $encoded_content; 
            }
        }

    }else{ //send plain email otherwise
       $headers = "From:".$from_email."\r\n".
        "Reply-To: ".$sender_email. "\n" .
        "X-Mailer: PHP/" . phpversion();
        $body = $sender_name. "\n";
        $body = $sender_number. "\n";
        $body = $sender_message. "\n";
    }

     $sentMail = @mail($recipient_email, $subject, $body, $headers);
    if($sentMail) //output success or failure messages
    {       
         header("Location: index.php"); /* Redirect browser */
         exit();
    }else{

        header("Location: index.php"); /* Redirect browser */       
    }
}
?>
php html 电子邮件 变量 行符

评论

2赞 Mahesh Hegde 1/15/2018
尝试 <br> 而不是 \n

答:

2赞 user3647971 1/15/2018 #1

您也在对行尾进行 base64 编码

因此,实际输出不会有\n\r 试试这个:

$string .= $sender_name."\r\n";
$string .= $sender_number."\r\n";
$string .= $sender_message."\r\n";
$body .= base64_encode(chunk_split($string)); // I think since your transfer encoding is base64, it will not respect the line endings added by the chunk_split not sure tho so be sure to encode it all with base64

编辑: 给定您的代码:

$body = $sender_name. "\n";
$body = $sender_number. "\n";
$body = $sender_message. "\n";

在每一行中,您不是在附加字符串,而是为其分配一个新值;use 代替赋值运算符.=

评论

0赞 in2d 1/15/2018
电子邮件显示为空白。
0赞 user3647971 1/15/2018
对不起,您可能希望改用 .=
0赞 in2d 1/15/2018
你是对的。代码还存在另一个问题。多亏了你,才解决了这一切。
2赞 azjezz 1/15/2018 #2

你也是encondig “\n\r”,确保把换行符放在最后,

  $body .= chunk_split(base64_encode($sender_name)) . PHP_EOL;
  $body .= chunk_split(base64_encode($sender_number)) . PHP_EOL;
  $body .= chunk_split(base64_encode($sender_message)) . PHP_EOL;

如果您以 HTML 格式发送电子邮件,请替换为:PHP_EOL<br />

  $body .= chunk_split(base64_encode($sender_name)) . "<br />";
  $body .= chunk_split(base64_encode($sender_number)) . "<br />";
  $body .= chunk_split(base64_encode($sender_message)) . "<br />";

评论

0赞 in2d 1/15/2018
对我不起作用。仍然只有 $sender_name 出现在我的电子邮件中。尝试了两种解决方案,结果都是一样的。
1赞 azjezz 1/15/2018
那么我认为问题不是换行符,你的问题说不起作用,请详细描述你的问题\n
1赞 azjezz 1/15/2018
您能告诉我们您在电子邮件中收到的内容吗?您是如何发送电子邮件的,以便我们提供帮助
0赞 in2d 1/15/2018
我在 HTML 的名称字段中收到 enter 中的文本。但是这个数字和信息消失了。我正在使用自己的域发送电子邮件。这意味着我使用我的域运营商SMTP服务器,我认为。我将编辑我的问题并发布整个 php 代码。
0赞 in2d 1/15/2018
找到了解决方案。谢谢!