I am having one string which is of near about 100 lines which is similar to this
My name is John. \r\n I am a boy. \r\n I am so in so
Here the above string is coming from backed so when I send mail using PHP mail function it should output as below.
My name is John.
I am a boy.
I am so in so
But unfortunately it give this output to me.
My name is John. I am a boy. I am so in so
The method I am using is similar to this.
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type:text/html;charset=UTF-8\r\n";
$msg = "Above Str";
mail(to#user, Subject, $msg, $header);
Can anyone help to do make this proper so.
This
$header .= "Content-type:text/html;charset=UTF-8\r\n";
is telling the client to render it as HTML, where line breaks aren't rendered.
If you want it as HTML, you can try:
$msg = nl2br($theString);
to convert line breaks to HTML <br />.
If you want the clients to render it as text, not HTML, change the content type to text/plain:
$header .= "Content-type:text/plain;charset=UTF-8\r\n";
You can try using PHP's nl2br() function to convert the line breaks in the string to HTML line breaks before sending the email:
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type:text/html;charset=UTF-8\r\n";
$msg = nl2br('Above Str');
mail('to#user', 'Subject', $msg, $header);
This will convert the \r\n line breaks in the string to HTML line breaks before sending the email.
You specified Content-type: text/html, so the body of your e-mail message is a HTML document. As such, just like when building a regular webpage, newlines themselves have no visible effect.
Use either paragraphs (<p>...</p>) or <br> to get the result you're after.
for this type of output, u need to just create a separate page and add all content that you want to send over the mail.
then
$msg = Load page;
then call or load your that page here and your code will start working try it
Related
I need to send text from users typed in a html form (textarea) along with emails. I use PHPMailer with HTML setup. This works perfect also because I need to embedd logos. The only problem is that email programs which receive the mail ignores line breaks from this text which. I parse the text like this :
$mail->isHTML(true);
$mail->Body = $_POST['mail_Text'];
What do I need to do ?
nl2br() should work for you.
So, add mail body like this.
$mail->Body = nl2br($_POST['mail_Text']);
nl2br() returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).
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.
I have a block of Heredoc for email that I am sending out. In this block I am trying print selected text in bold/strong.
Take the following block of heredoc as an example.
<<<ENDINGTEXT
Example text
more text
<b>This in bold</b>
ENDINGTEXT;
When I send above out as an email. The "b" tags do not convert the text within it to be bold. In the email it gets displayed as it is with b tags intact.
can someone please show me the way to achieve this as well some basic HTML integration in heredoc.
That is because the email which you are sending does not contain HTML enabled headers.
You need to set the headers like this.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
For a detailed example, see the PHP Manual here
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
How can I take the text from a textarea (html) and insert line breaks. Right now, if I input info to send out as an e-mail, it puts all the text on the same line without any line breaks.
Using $_POST['field'] to get the data from the form and sending using PHP mail.
Use nl2br() function. It replaces all newlines within a string with html br tags.
use \n for new line, or \r\n for return followed by new line
ie.
<?php
printf("This is the first line. \n");
printf("This is the second line");
?>
ie. to replace html tag with newline:
str_replace ('<br>' , '\r\n', $_POST['field'])
alternativly set the email you are sending out to be html encoded (add html header)
In php, replace \n with html br tag,
$newTxt = str_replace("\n",'<br>',$txt)
or nl2br() will serve your purpose.
remove stripslashes(), this will work.