the following mail does not produce line breaks with double quotes - php

I am using another version of the mail function and trying to get \n\n to work but it is not producing a separate line as intented.
Here is my code:
// email body content
$htmlContent = "<h1>Thank-you for purchasing your Level 1 Monthly Subscriptionplan!</h1>
<p>Dear $name \n\nThank you for purchasing your Level 1 Monthly Subscriptionplan for $price. \n\nPlease make sure that you check your student's general information to view your details carefully and note the expiry date for your subscriptionplan.\n\nFrom,\n\nPianoCourse101</p>";

\n will create a newline break in the code, but not within an HTML paragraph. You can either place all paragraphs within their own <p></p> tags or you can force an HTML line break using <br /> where you have the \n\n\ code

In html mails, you should use <br /> to add a line break.
All html parsers will ignore \n line breaks in the code.

Related

What does exactly mean by 'Line feeds' in HTML and PHP? How do they are added in HTML and PHP code?

I was reading PHP Manual and I come across following text paragraph :
Line feeds have little meaning in HTML, however it is still a good
idea to make your HTML look nice and clean by putting line feeds in. A
linefeed that follows immediately after a closing ?> will be removed
by PHP. This can be extremely useful when you are putting in many
blocks of PHP or include files containing PHP that aren't supposed to
output anything. At the same time it can be a bit confusing. You can
put a space after the closing ?> to force a space and a line feed to
be output, or you can put an explicit line feed in the last echo/print
from within your PHP block.
I've following questions related to the text from above paragraph :
What does exactly mean by 'Line feeds' in HTML?
How to add them to the HTML code as well as PHP code and make visible in a web browser? What HTML entities/tags/characters are used to achieve this?
Is the meaning of 'Line feed' same in case of HTML and PHP? If no, what's the difference in meaning in both the contexts?
Why the PHP manual is saying in first line of paragraph itself that? What does PHP Manual want to say by the below sentence?
"Line feeds have little meaning in HTML"
How can it be useful to remove a linefeed that follows immediately after a closing tag ?> when someone is putting in many blocks of PHP or include files containing PHP that aren't supposed to output anything?
Please someone clear my above mentioned doubts by giving answer in simple, lucid and easy to understand language. If someone could accompany the answer by suitable working code examples it would be of great help to me in understanding the concept more clearly.
Thank You.
What does exactly mean by 'Line feeds' in HTML?
It is a general computing term.
The character (0x0a in ASCII) which advances the paper by one line in a teletype or printer, or moves the cursor to the next line on a display.
— source: Wiktionary
How to add them to the HTML code
Press the enter key on your keyboard. Note that (with a couple of exceptions like <pre>) all whitespace characters are interchangeable in HTML. A new line will be treated as a space.
as well as PHP code
Ditto … or you could use the escape sequence \n inside a string literal.
and make visible in a web browser?
The material you quoted is talking about making source code look nice. You generally don't want line feed characters to be visible in a browser.
You could use a <pre> element instead.
Outside of <pre> elements (and the CSS setting they have by default) you can use a space instead of a new line for the same effect in HTML.
What HTML entities/tags/characters are used to achieve this?
… but the advice given in the last sentence of the material you quoted is probably a better approach.
'Lines feed' exactly means a 'New line' both in Html and Php, only the syntax is different.
In case of Html tag, you can use <br> or <br/> tag for a Lines feed. Basically, this tag shows a new line in the output of the Html attribute block, while running through the browser.
You can take the following example for <br> tag:
<html> <body>
<p> To break lines<br>in a text,<br/>use the br element. </p>
</body> </html>
Output:
To break linesin a text,use the br element.
In case of Php, you can use '\n' for a lines feed.
If you are using a string in Php, then instead of writing,
echo "New \nLine";
you can use nl2br() function to get line break, like:
echo nl2br("New \nLine");
Output:
New
Line

The difference between \n and <br /> in php [duplicate]

This question already has answers here:
\n vs. PHP_EOL vs. <br>?
(7 answers)
Closed 8 years ago.
I was studying php with tutorial. I recognized that in php is sometimes use like this
echo $myString."<br />"
Suddenly, there came another in the tutorial "\n" and I got confused. Can I use it just like I use
<br />
?
So like this?
echo $mySting."\n"
Is it like a html tag?
What's the difference between
<br /> and \n?
<br /> is a HTML line-break, whereas \n is a newline character in the source code.
In other words, <br /> will make a new line when you view the page as rendered HTML, whereas \n will make a new line when you view the source code.
Alternatively, if you're outputting to a console rather than somewhere that will be rendered by a web browser then \n will create a newline in the console output.
\n is a new line feed within the context of plain text, while <br /> is line break within the context of HTML.
HTML can interpret \n when using preformatted blocks (e.g. <pre>), but it does not by default, and should not unless there is a specific use case (like when quoting, citing poetry, or showing code).
<br /> should never be used to separate text that should otherwise be treated as a paragraph, heading or other group of text.
\n is a new line character (a literal new line as you would type in your code).
$newline = "
";
var_dump($newline === "\n");
// true
You can also use the PHP constant PHP_EOL (end of line). Note, that '\n' will render \n as a literal string; you must use double quotes to have it rendered as a new line character. Also note, that my above example may actually output false..since sometimes new lines are rendered as \r\n.
<br /> is an HTML element for a line break. This will show up as a new line when HTML is rendered in a browser.
The only time that \n will show up as a rendered line break in HTML, is when it is within a <pre> (pre-formatted text) element. Otherwise it would be the same as just formatting/indenting your HTML code:
<?php
echo "<html>\n\t<body>\n\t\tHello World!\n\t</body>\n</html>";
Outputs:
<html>
<body>
Hello World!
</body>
</html>

Displaying a Paragraph with line breaks

I have a situation where users can enter a question and this is written into a mysql table.
I then show this text in an admin panel for reivew and reply.
I can see the text in the admin panel has the same new lines (chrome developer) but it shows as one block of text (no new lines)...
I'm currently using to show this text... I've tried a but it still hows without line breaks.
How should I go about displaying this text with the new lines it contains? Is there a HTML tag to do this or do I need to use PHP and sub \n for br tag?
example below:
<span class="description">txthere
txthere
txthere
txthere
txthere.</span>
On display you can use CSS to style the HTML with the original line breaks, try white-space:
white-space: pre-line;
if it is textarea or console basically it puts a line break using /n which is not recognized by the browser as line break, you need to convert /n to <br/> to tell the browser that this is a line break, use this php's function
nl2br();
more information here: http://in.php.net/nl2br
this will convert all your /n tag to <br/> tag so that browser process it as line breaks

Use PHP to maintain format of a textarea

Hy Ho,
It is possible to maintain the format of a text area with a PHP form so that a message that is mail'd to the admin is formatted nicely.
ie. If someone writes in the textarea,
Dear Sir,
I am writing in connceti....
Many Thanks,
At the moment it emails as
Dear Sir, I am writing in connceti...
Many Thanks,
If not then I suppose the solution is Rich Text Editor replacement textarea. Thats all well and good, but what if javascript is disable.
Any ideas,
Marvellous
You probably need to replace (depending on system):
\n\r or \n or \r
By:
<br />
PHP function needed:
nl2br()
At this point it seems as though your emails might be sent in HTML format. HTML format automatically removes the line breaks if the given input is not in HTML format. If you don't want HTML formatting then try sending the message plain text, then your line breaks should still show.
To send the message as plain text, simply use the PHP mail() function, without any additional headers:
mail('john#gmail.com', 'Test Email', 'My Message...
with a line break!');
If you are still having problems, try integrating an HTML WYSIWYG editor, such as the powerful TinyMCE editor.
Note: for the HTML editor to send emails properly, you will need to send emails messages in HTML format (where you would need to supply headers as the fourth parameter in the mail() function). This will, for sure, solve your line break issues.
Hope that helps,
spryno724
The newlines in a <textarea> are preserved by both the browser and PHP. Probably they get lost in the email body itself or they are simply not being displayed by the browser even if they are there.
If you are sending a HTML mail you need to do nl2br(htmlspecialchars($your_textarea_data)) to add the necessary <br> tags.
If you are sending a plain text mail check if you are mixing LF (Unix-style) and CR+LF (DOS-style) newline sequences that maybe confuse your e-mail client.
This depends purely in your email format.
When you type that text, it actually looks like this
Dear Sir, \n
\n
I am writing in connceti....\n
\n
Many Thanks,
If you're sending text mail, you have to make sure the linebreaks are not stripped out. And if you're sending out HTML email, you have to make sure you replace those linebreaks for <br> tags

What good is new line character?

I don't really get it: what's the purpose of a new line character?
If I do this:
<?php
echo "This is a test. \n";
echo "This is another test.";
?>
Code results in both sentences being in the same line. Why doesn't the \n causes the second sentence being in second line?
The sentences are each in it's own line, if I do:
<?php
echo "This is a test. <br>";
echo "This is another test.";
?>
But I have also seen people do this:
<?php
echo "This is a test. <br>\n";
echo "This is another test.";
?>
Which essentially results in the same output as the second code snippet. Someone care to explain this?
The HTML standard treats a line break as just another white space character, which is why the <br> tag exists. Note however a line break will work within a <pre> tag, or an element with the white-space:pre CSS style.
The third example is just to make "pretty" HTML: it makes it easier to "view source" and check it by eye. Otherwise, you have a big long string of HTML.
as you have observed there are different ways to create a new line.
<br />
this is not a new line character, this is an XHTML tag which means, it works in XHTML.
correctly speaking it is not a new line character but the tag makes sure, one is inserted = it forces a line break. closing tag is mandatory.
XHTML specs
<br>
this is a HTML tag which forces a line break. closing tag is prohibited.
HTML 4.1 specs
\n
is an escape sequence for the ASCII new line char LF. A common problem is the use of '\n' when communicating using an Internet protocol that mandates the use of ASCII CR+LF for ending lines. Writing '\n' to a text mode stream works correctly on Windows systems, but produces only LF on Unix, and something completely different on more exotic systems. Using "\r\n" in binary mode is slightly better, as it works on many ASCII-compatible systems, but still fails in the general case. One approach is to use binary mode and specify the numeric values of the control sequence directly, "\x0D\x0A".
read more
PHP_EOL
is a php new line constant which is replaced by the correct system dependent new line.
so the message is, use everything in it's right place.
<br> will give you a new line in the user's view; \n will give you a new line in source code, ie. developer's view.
When the html is rendered, only the "<br />" renders the break line. However the markup is much easier to read when "<br />\n" is printed, so that everything is not in one long line.
\n is code based
<br /> is HTML tag based
There is a clear distinction between the two.
Your problem is the way html is rendered. If you look in the source code, the first example will have the two lines on separate lines, but the browser does not see line breaks as breaks that should be displayed. This allows you to have a long paragraph in your source:
rghruio grgo rhgior hiorghrg hg rgui
ghergh ugi rguihg rug hughuigharug
hruauruig huilugil guiui rui ghruf hf
uhrguihgui rhguibuihfioefhw8u
beruvbweuilweru gwui rgior
That would only wrap as the browser needed it to, allowing it to be easily editable at the right line length, but displayed at any resolution.
HTML does not care about new lines in the source code, you can put it all in one line. It will only interpret <br /> as a new line. You should use an \n to beautify your HTML-output though, but the better way is to not output it with PHP, but to use it in the HTML itself and only embed PHP stuff into it, like this:
<ul id="menu">
<?php foreach ($menu_items as $item): ?>
<li>
<a href="<?= htmlspecialchars($item['link']) ?>" title="<?= htmlspecialchars($item['title']) ?>">
<?= htmlspecialchars($item['title']) ?>
</a>
</li>
<?php endforeach; ?>
</ul>
That way you won't have to bother with formatting inside PHP, but you automagically have it, by design, in HTML. Also, you seperate Model-logic and View-logic from each other like this and leave the output to your HTTP Server rather than the PHP engine.
That's because you're creating HTML and view it in a browser, and whitespace is more or less ignored there. Ten spaces don't produce a bigger gap than one space, but that doesn't mean that the space character doesn't work. Try setting the content type to text/plain or look at the HTMLs source to see the effect of the newline.
The correct XHTML syntax for it would be
echo "This is the test code <br />\n";
The <br /> renders a new line onscreen, the "\n" renders a new line in the source coed
The new line character is useful otherwise, such as in a PDF. You're correct that the new line character has very little do with HTML as other people have said, it's treated as another while space character. Although it is useful inside the <pre> tag. It can also be used to format the HTML output to make it easier to read. (It's a little annoying to try to find a piece of HTML in a string that's 1000 characters wide.)
The new line character is also useful when storing data in the database. Usually you want to store the data without HTML special characters such as <br /> so that it can be easily used in other formats (again, such as PDF). On output, you want to use the nl2br() function to convert the new lines to <br />s.
The new line character is is useful for string functions.
For example:
explode( '\n' , $input );
Would split a string by a new line.
str_replace( '\n' , '<br />' , $input );
Would replace every newline in $input with a br tag.
Also because PHP also has a CLI, \n is useful for formatting:
eg.
echo 'Hello world';
Would, in the CLI, output;
Hello worldphp>
echo 'Hello world' . "\n";
would output;
Hello world
php>
Although it also has uses when writing web-based scripts, keep in mind PHP is more than a web engine; it also has a CLI where the br tag is useless.
<br /> is also useless if you're running a script from the command line.
$ php -f file.php
Output <br />$
I know not too many people use PHP from the command line, but it does come up:
file.php:
<?php
print "Output\n";
?>
At the command line:
$ php -f file.php
Output
$

Categories