doukanwen4114 2018-01-14 16:07
浏览 123
已采纳

PHP中断行“。 ”在电子邮件正文中不起作用。 线条正在消失

How can i break line after $sender_name and $sender_number? Right now it only shows $sender_name as output but when i remove " " they all are together on one line.

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

Whole PHP code

<?php
if($_POST && isset($_FILES['file']))
{
    $recipient_email    = "mymail@gmail.com"; //recepient
    $from_email         = "mymail2@gmail.com"; //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
"; 
        $headers .= "From:".$from_email."
"; 
        $headers .= "Reply-To: ".$sender_email."" . "
";
        $headers .= "Content-Type: multipart/mixed; boundary = $boundary

"; 

        //message text
        $body = "--$boundary
";
        $body .= "Content-Type: text/plain; charset=ISO-8859-1
";
        $body .= "Content-Transfer-Encoding: base64

"; 
          $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
";
                $body .="Content-Type: $file_type; name=" . $file_name ."
";
                $body .="Content-Disposition: attachment; filename=" . $file_name ."
";
                $body .="Content-Transfer-Encoding: base64
";
                $body .="X-Attachment-Id: ".rand(1000,99999)."

"; 
                $body .= $encoded_content; 
            }
        }

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

     $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 */       
    }
}
?>
  • 写回答

2条回答 默认 最新

  • dtcyv3985 2018-01-14 16:08
    关注

    You are base64encoding the line endings aswell

    So the actual output would not have Try this:

    $string .= $sender_name."
    ";
    $string .= $sender_number."
    ";
    $string .= $sender_message."
    ";
    $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
    

    EDIT: Given your code:

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

    On each of these lines you are not appending the string but assigning it a new value; use .= instead of assignment operator

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python