Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

New line in php not working

28 views
Skip to first unread message

wart2ww

unread,
Jan 28, 2017, 9:24:42 PM1/28/17
to
The following code all works fine in a <?php ?> block except the \n is not working. All the fields in the in the email are on one line. I have done this before and it seemed to work but not now. What have I done wrong?

foreach ($_POST as $key => $value) {

if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n"; }
}

Thank you for your help.

J.O. Aho

unread,
Jan 29, 2017, 3:30:11 AM1/29/17
to
On 01/29/17 03:24, wart2ww wrote:
> The following code all works fine in a <?php ?> block except the \n is not working.
> All the fields in the in the email are on one line.

This has nothing to do with "\n" not working, this is the way the mail
client handles the "\n", for example the terrible mail client outlook
will show all in one line, you will need to have "\t\n\n" for it to
handle this correctly, while the nice Thunderbird will show it as
intended as it is with "\n".


> I have done this before and it seemed to work but not now. What have I done wrong?
>
> foreach ($_POST as $key => $value) {
>
> if (isset($fields[$key])) {
> $emailText .= "$fields[$key]: $value\n"; }
> }

For more readability of your code, you should stick with a coding
standard and make variables in a string to stand out


foreach ($_POST as $key => $value) {

if (isset($fields[$key])) {
$emailText .= "${fields[$key]}: ${value}\n";
}
}

or



foreach ($_POST as $key => $value) {

if (isset($fields[$key])) {
$emailText .= $fields[$key] .": ". $value ."\n";
}
}


> Thank you for your help.


np.

--

//Aho
Message has been deleted
Message has been deleted

wart2ww

unread,
Jan 29, 2017, 9:04:46 PM1/29/17
to
On Sunday, January 29, 2017 at 6:10:24 AM UTC-8, Thomas 'PointedEars' Lahn wrote:
> J.O. Aho wrote:
>
> > On 01/29/17 03:24, wart2ww wrote:
> >> The following code all works fine in a <?php ?> block except the \n is
> >> not working. All the fields in the in the email are on one line.
> >
> > This has nothing to do with "\n" not working, this is the way the mail
> > client handles the "\n",
>
> Utter nonsense. Obviously this code is generating *header fields*. They
> are to be separated by CRLF and nothing else.
>
> But then again, what kind of an understanding of Internet standards can one
> expect from a person who, despite several hints, keeps abusing dedicated
> Internet domains?
>
> --
> PointedEars
> Zend Certified PHP Engineer <http://www.zend.com/en/yellow-pages/ZEND024953>
> <https://github.com/PointedEars> | <http://PointedEars.de/wsvn>
> Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

I have a question about your response. When you talk about 'keeps abusing dedicated Internet domains are you referring to me, the author of the original post? I am not sure which one of us you are chastising.

Jerry Stuckle

unread,
Jan 29, 2017, 9:14:26 PM1/29/17
to
OK, I may have screwed up here. I thought you were talking about the
body of the message. Are these header fields you are setting? If so, I
agree with the others - if you're using Linux, you need to use \r\n.
(If you're on Windows, \n should already equate to \r\n).

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================

wart2ww

unread,
Jan 29, 2017, 9:56:04 PM1/29/17
to
The code is for a confirmation email message after a form is submitted. It is designed for Windows environment.

Jerry Stuckle

unread,
Jan 29, 2017, 10:08:30 PM1/29/17
to
On 1/29/2017 9:56 PM, wart2ww wrote:
> On Sunday, January 29, 2017 at 6:14:26 PM UTC-8, Jerry Stuckle wrote:
>> On 1/28/2017 9:24 PM, wart2ww wrote:
>>> The following code all works fine in a <?php ?> block except the \n is not working. All the fields in the in the email are on one line. I have done this before and it seemed to work but not now. What have I done wrong?
>>>
>>> foreach ($_POST as $key => $value) {
>>>
>>> if (isset($fields[$key])) {
>>> $emailText .= "$fields[$key]: $value\n"; }
>>> }
>>>
>>> Thank you for your help.
>>>
>>
>> OK, I may have screwed up here. I thought you were talking about the
>> body of the message. Are these header fields you are setting? If so, I
>> agree with the others - if you're using Linux, you need to use \r\n.
>> (If you're on Windows, \n should already equate to \r\n).
>>
>
> The code is for a confirmation email message after a form is submitted. It is designed for Windows environment.
>

If you're running on Windows, then "\n" should already be converted to
crlf ("\r\n" in Linux). But I'm still not sure if you're in the header
or the body of the message.

Also, what is the message encoding? Is it plain text, html, or what?

wart2ww

unread,
Jan 29, 2017, 10:40:57 PM1/29/17
to
The code is written and saved as a php extension. The code is written as html in the body, not the header.

Jerry Stuckle

unread,
Jan 30, 2017, 7:41:13 AM1/30/17
to
Ah, this is written as HTML - that is a key factor here. HTML ignores
white space in email, just as it does on a web page. Look at the source
of your email - you will see the line break there.

If you want a break in HTML, you have to use an HTML code for it, i.e.
<br> for a line break or <p>...</p> for a paragraph break.

Angel

unread,
Jun 30, 2022, 10:43:01 AM6/30/22
to
Reprogram then php, so that it will work.


******************************
Angel
☏ ------> 372 53900660
0 new messages