HTML characters in mail php - php

<a href='http://stackoverflow.com' style='color:red;'>testing</a>
The above is my text inside textarea. I am passing this textarea value as a mail body. The word "testing" appears neither anchored nor red.
However, when I write this:
$body="<a href='http://stackoverflow.com' style='color:red;'>testing</a>";
..and pass $body as my mail body, it works fine.
What could cause this behavior?

my first guess is that you have "Magic Quotes" option on in the php settings, and the mail body ends up like:
<a href=\'http://stackoverflow.com\' style=\'color:red;\'>testing</a>
Try to use "view source" on the recived mail, or use "save as" and open the file in a simple text editor, and you should se how the mail ended up.
if my guess was right, you could either turn of magic quets, or use stripslashes() to remove the extra slaches

Related

Why does the PHP add extra space in the email body for mail()?

Recently I use the mail($to, $subject, $content, $headers) to send email with a very long content including some images link like http://foo.com/image/1.jpg. The problem is that the email body sometimes break at some point and show the link like http://foo.com/image/1.jp+g, which will break the link.
Or sometimes it just break the html tags like break <div> in to < div>, and finally the tag is shown in somewhere that is not supposed to be. Making the email looks so wired.
This problem has bugged me for a whole morning.
Actually, I tracked down this problem and find that the body is too long that in the email, it will add newline automatically in the $content when applied in the mail() message body. As a result the image link will be likehttp://foo.com/image/1.jp
g
and the tags will be like <
div>
This problem has troubled me for a whole morning.
Now I found the solution would be adding \n at some point in your message so that your mail body is not only on one line, and the mail() will not add its own new line at the point that you do not want it to.
Hope this can help someone if they have the similar problem.

PHP IMAP Plain text one jarbled paragraph

I'm using imap from php and retrieving a message. The contents can either be html or plain text. When html is available, I resort to using the html contents for displaying. However, if html text is not available, than I must use the plain text for rendering messages to the user.
My question is, when I display the plain text, it comes as one jarbled paragraph. I'd like to figure out how to format the plain text into a viewable fashion. This could just be a multipart issue, and the parts together form one jarbled paragraph. If so, what should I do? Erase the parts that are not the most recent? Is there a way to format the plain text regalrdess
Thanks
You can enclose the text in <pre> tags to preserve the formatting
You can also use nl2br to insert <br /> before all new lines

New line ("\n") in PHP is not working

For some strange reason, inserting echo "\n"; and other scape sequence characters are not working for me, that's why I am just using <br /> instead.
The images of the results of examples in books and other documentations seems just alright. I'm currently using XAMPP and already used WAMPP with the same actual result. Why is that?
Edit:
It seems that I cannot understand the concept after comparing your answers with this:
PHP Linefeeds (\n) Not Working
Edit:
Sorry I didn't realized that the link above is referring to a php code writing to a file. I just wonder why I have these few php sample programs that uses \n even though it outputs in a webpage. Thanks everyone.
When you run a PHP script in a browser, it will be rendered as HTML by default. If the books you’re using show otherwise, then either the code or the illustration is inaccurate. You can use “view source” to view what was sent to the browser and you’ll see that your line feeds are present.
<?php
echo "Line 1\nLine 2";
?>
This will render in your browser as:
Line 1 Line 2
If you need to send plain text to your browser, you can use something like:
<?php
header('Content-type: text/plain');
echo "Line 1\nLine 2";
?>
This will output:
Line 1
Line 2
PHP Linefeeds (\n) Not Working is referring to sending output to a file rather than the browser.
You should be looking for nl2br(). This will add line breaks (<br>) to your output which will be rendered by the browser; newlines are not.
The echo "\n" is probably working, just not the way you expect it to.
That command will insert a new line character. From the sounds of it, you're using a browser to view your output. Note that if you wrote an HTML file that had a body contents that looked like:
<p>This
is
a
test </p>
The browser rendering would not include the new lines, and would instead just show "This is a test"
If you want to see the newlines, you could view source, and you'll see that the source code includes the new lines.
The rule of thumb is that if you need new lines in a browser, you need to use HTML (e.g. <br />), while if you want it in plain text, you can use the \n
<br /> is the HTML Tag for new line, whereas
"\n" is to output a new line (for real).
The browser doesn't output a new line each time the HTML file goes to the next line.
You can use the nl2br function to convert \n to <br>
As said before, HTML does not render \n as new line. It only recognizes the <br> html tag
If you are working with HTML (viewing the result in browser for example) you have to use the HTML way of linebreaks which is: <br>
/n only works if it is used as a simple text but here as we code in a html doc it takes it as a HTML text hence you can use </br> tag instead.
PHP outputs on the browser and browser only render output in HTML, any other output format will be ignored by the browser.
If you want browser to keep your standard output format as it is, you should enclose your output under HTML's <pre> tag. It preserves the formatting:
echo "<pre>";
echo "This is first line\nThis is new line";
echo "</pre>";
This will be rendered as
This is first line
This is new line
Alternatively, you can mention content type to be plain text in the header:
header('Content-type: text/plain');
echo "This is first line\nThis is new line";
This will tell the browser to render the output as plain text. And the browser will encolse the output automatically in <pre> tag.
solution is echo nl2br or=> <br>

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

jquery text area problem

i have one text area
<textarea name="message" id="message" class="text_field" style="height:200px; width:500px"></textarea>
if i type data in the text area like this
hello
this is my test message
bye
'abc'
i use following statement to get data from text area
var message = $('#message').attr('value');
i pass this value to other php file like
var data = {message:message};
$.ajax({
type: "POST",
url: "show.php",
data: data,
etc
when i see data in post value firebug in show exactly i type the data (with new lines & spaces etc)
and in php file
$message=$_POST['message'];
$Content = file_get_contents($temp_page);
$Content = str_replace('%%a%%', $message, $Content);
now when i use
echo $Content
i get all the text in one line not exact i type in text area...
hello this is my test message bye 'abc'
Thanks
That is because browsers don't print newlines outside textareas or other elements that are defined to print the output as is, for example <pre>.
You need to use echo nl2br($Content); to substitute newlines with <br /> elements, which is the equivalent of a newline in (x)HTML.
If you're echoing straight to the browser, try viewing the source of the page. You may find that the browser is interpreting the text as HTML, in which case it will collapse all your whitespace and newlines. Viewing the source should in theory show you what you originally typed, with newlines, extra space etc.
Think about what happens if you just typed in some HTML source like this:
<p>
Hello there.
This is
my
HTML
source, and I hope
you
like it.
</p>
What does that look like? Right — the browser will treat all those newlines as simple whitespace, and collapse it all as it lays out the non-whitespace characters.
When you dump out the textarea contents (by the way: get the value with $('#whatever').val()) you're doing the same thing. If you want to preserve the formatting, well, that can be a headache. You can try putting it in a <pre> block, or you may have to explicitly find the newlines and convert them to <br> elements.
edit: See #Tatu's answer for the php function you want.
IF you right click and click 'View Source' (or some close equivalent), you should see the properly formatted text. This is due to the way that HTML works. Even if you write the following HTML:
hello
this is my test message
bye
'abc'
The output will be inline. You have to write after each line, or wrap the text in a <pre> tag.
Maybe you can try:
nl2br($Content);

Categories