简体   繁体   English

在我的PHP电子邮件表单中创建换行符

[英]Create a line break in my PHP email form

\\I hope someone can help. \\我希望有人能帮帮忙。 I am a bit of a dunce with php but I managed to create a booking form for my bed and breakfast using a mail.php page called up from the html form. 我对php有点a,但是我设法使用从html表单调出的mail.php页面为我的床和早餐创建了预订表。

The body goes like this: 身体是这样的:

$subject = "Booking Request"; 
$fields = array(); 

$fields{"title"} = "Title";
$fields{"name"} = "Name";
$fields{"email"} = "email";
$fields{"telephone"} = "telephone";
$fields{"mobile"} = "mobile";
$fields{"arrival_day"} = "arrival day";
$fields{"departure_day"} = "departure day";
$fields{"adults"} = "#  of adults";
$fields{"children"} = "# of children";
$fields{"questions"} = "any questions?";

It works but everything comes out on one line like this 它可以工作,但一切都像这样一行

Title: Ms Name: Joe Blogs email: joe@blogs.com.au telephone: 02 12345697 mobile: 01234958arrival day: 10/06/12 departure day: 20/06/12 # of adults: 8 # of children: 4 any questions? 职称:女士姓名:Joe Blogs电子邮件:joe@blogs.com.au电话:02 12345697手机:01234958到达日期:10/06/12出发日期:20/06/12成人人数:8儿童人数:4任何问题?

but I want it to look like this: 但我希望它看起来像这样:

Title: Ms 职称:女士
Name: Joe Blogs 姓名:Joe Blogs
email: joe@blogs.com.au 电子邮件:joe@blogs.com.au
etc. 等等

I've tried \\n , \\r and <br/> in all sorts of places but I'm just stabbing in the dark... can what i have be fixed or do I need to use an entirely different code? 我在各种地方都尝试过\\n\\r<br/> ,但是我只是在黑暗中刺伤……我可以解决问题还是需要使用完全不同的代码?

You should use \\n as an end line. 您应该使用\\n作为结束行。

And please change {} to [] : 并将{}更改为[]

$fields["title"] = "Title";
$fields["name"] = "Name";
$fields["email"] = "email";
$fields["telephone"] = "telephone";
...

I used to use "\\n": 我曾经使用“ \\ n”:

$text = 'line1\n line 2\n line 3';

The result were: 结果是:

line1\n
line2\n
line3

And i was not satisfied so i found PHP_EOL 而且我不满意,所以我发现了PHP_EOL

$text = 'line 1' . PHP_EOL . 'line 2' . PHP_EOL . 'line 3';

The result will be: 结果将是:

line 1
line 2
line 3

"<br>" is a viable solution but you're sending "HTML" formatted email. "<br>"是可行的解决方案,但是您正在发送“ HTML”格式的电子邮件。

使用nl2br();

$message = nl2br($name . " " . " wrote this:" . "\n" . $_POST['message'] . $_POST['phone'] . $_POST['email']  . "\n" . $_POST['radio']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM