PHP 断行 ".\n"在电子邮件正文中不起作用。线条正在消失

标签 php html email variables line-breaks

如何在 $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    = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c2135212d25200c2b212d2520622f2321" rel="noreferrer noopener nofollow">[email protected]</a>"; //recepient
    $from_email         = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80edf9ede1e9ecb2c0e7ede1e9ecaee3efed" rel="noreferrer noopener nofollow">[email protected]</a>"; //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 */       
    }
}
?>

最佳答案

您也在对行结尾进行 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";

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

关于PHP 断行 ".\n"在电子邮件正文中不起作用。线条正在消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48251484/

相关文章:

php - localhost/live - 通过 HTTP_HOST 检测

php - 使用 php 驱动程序将查询从 MySql 查询转换为 mongoDB 查询

javascript - 如何将ajax代码转换为php

javascript - 单击可转到实时动画中的下一个位置

ruby-on-rails - 通过 SendGrid 使用 Ruby on Rails 发送带附件的电子邮件

javascript - 使用 Express.js/Node.js 的电子邮件管道

php - LEFT JOIN - 排序和分组

c# - 删除 asp 按钮背景?

html - 在跨度内右对齐数字的最简单方法。想不通

php - 使用 mailchimp API V3 发送单个邮件