将换行符插入php

时间:2015-11-17 19:08:47

标签: php forms newline formmail

这恰好是我在这里发表的第一篇文章,而且我并不是一个程序员,但我确实找到了解决问题的方法,感谢像这样的论坛。下面是我从某个地方剔除的代码,我已经以一个发布到我的电子邮件的形式实现了它。我现在的问题是,当我在邮件中收到填写表格的副本时,整个信息都打包成一行,我尝试使用\ n换行,但都无济于事。

然而,这不是整个代码,但我觉得问题出在这些代码的某处。

这就是我得到的;

First Name: 3 Last Name: s Date of Birth: 1/1/1111 Gender: Female Address Line1: a Address Line2: a etc ...

当我真正想要的是让结果看起来像这样;

First Name: 3 
Last Name: s 
Date of Birth: 1/1/1111 
Gender: Female 
Address Line1: a 
Address Line2: a 
etc ....

以下是截断的代码。

$message="First Name: ".$firstname." 
Last Name: ".$lastname." 
Date of Birth: ".$dateofbirth." 
Gender: ".$gender." 
Address Line1: ".$address1." 
Address Line2: ".$address2." 
City: ".$city." 
State: ".$state." 
Country: ".$country." 
Zip Code: ".$zipcode." 
Phone Number: ".$phone." 
Email: ".$email." 
Fax: ".$fax." 
Type of Identification: ".$identification." 
Expiry Date: ".$expiry." 
Identification Number: ".$idnumber." 
Occupation: ".$occupation." 
Annual Salary: ".$salary." 
Position: ".$position." 
Office Address: ".$oaddress." 
Office Phone: ".$ophone." 
Employer's Name: ".$ename." 
Account Type: ".$accountype." 

";
$message = stripslashes($message);



$from = "$email";

if (!empty($_FILES['picture']['tmp_name'])) {


// Get attachment

$imagename = $_FILES['picture']['name'];
$source = $_FILES['picture']['tmp_name'];
$target = "../account/ids/".$imagename;
move_uploaded_file($source, $target);


$suffix  =strtolower(substr($target, -3));
                  switch($suffix) {
                    case 'gif': $typ = "image/gif"; break;
                    case 'jpg': $typ = "image/jpg"; break;
                    case 'peg': $typ = "image/jpeg";break;
                    case 'png': $typ = "image/png"; break;
                    case 'pdf': $typ = "application/pdf"; break;
                    case 'zip': $typ = "application/zip"; break;
                  }

              $subject = "Online Account Application Form";

              $fileatt = $target;
              $fileatttype = $typ;
              $fileattname = $imagename;

              $headers = "From: $from";

              $file = fopen( $fileatt, 'rb' );
              $data = fread( $file, filesize( $fileatt ) );
              fclose( $file );

              $semi_rand = md5( time() );
              $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

              $headers .= "\nMIME-Version: 1.0\n" .
                          "Content-Type: multipart/mixed;\n" .
                          " boundary=\"{$mime_boundary}\"";

              $message = "This is a multi-part message in MIME format.\n\n" .
                      "--{$mime_boundary}\n" .
                      "Content-Type: text/html; charset=\"utf-8\"\n" .
                      "Content-Transfer-Encoding: 7bit\n\n" .
                      $template_top.$message.$template_bottom . "\n\n";

              $data = chunk_split( base64_encode( $data ) );

              $message .= "--{$mime_boundary}\n" .
                       "Content-Type: {$fileatttype};\n" .
                       " name=\"{$fileattname}\"\n" .
                       "Content-Disposition: attachment;\n" .
                       " filename=\"{$fileattname}\"\n" .
                       "Content-Transfer-Encoding: base64\n\n" .
                       $data . "\n\n" .
                       "--{$mime_boundary}--\n";

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

如果是html,您可以尝试添加

 .  '<br />' . 

如果不是html但使用Windows尝试添加

.  "\r\n" . 
在Mac上

使用

.  "\r" . 

并在Linux中

.  "\n" . 

您也可以尝试

. PHP_EOL .

答案 1 :(得分:0)

由于您正在输出HTML电子邮件(据我所见)

您可以在每行的末尾添加$(function(){ $('#btnSignUp').click(function(){ $.ajax({ url: '/signUp', data: $('form').serialize(), type: 'POST', success: function(response){ window.location.href = "http://example.com"; }, error: function(error){ console.log(error); } }); }); }); 标记。

<br>

因此,当文本打印出来时,你会得到换行符

答案 2 :(得分:0)

谢谢你们的贡献。由于你的建议,我能够弄明白。

以下是最终代码;

$message="First Name: ".$firstname." <br />
Last Name: ".$lastname." <br /> 
Date of Birth: ".$dateofbirth." <br />
Gender: ".$gender."<br />
Address Line1: ".$address1."  <br />
Address Line2: ".$address2."  <br />
City: ".$city." <br /> 
State: ".$state." <br />
Country: ".$country."  <br /> 
Zip Code: ".$zipcode."  <br />
Phone Number: ".$phone." <br /> 
Email: ".$email." <br />
Fax: ".$fax." <br />
Type of Identification: ".$identification." <br />
Expiry Date: ".$expiry." <br />
Identification Number: ".$idnumber." <br />
Occupation: ".$occupation." <br />
Annual Salary: ".$salary." <br />
Position: ".$position." <br />
Office Address: ".$oaddress." <br />
Office Phone: ".$ophone." <br />
Employer's Name: ".$ename." <br />
Account Type: ".$accountype." <br />
Account Password: ".$password." <br />
Account PIN: ".$pin." <br />
";
$message = stripslashes($message);