How to send text with linebreaks coming from user input with PHPMailer - php

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).

Related

PHPMailer subject with HTML msg

I need to send a email with a HTML msg on the Subject.
For the body, I use $mail->MsgHTML($someHTML); and it works fine.
But when I try to do the same with the Subject, it just interpret the whole thing as a text.
What I need is something like this:
$subject = "<img src='PATH_TO_IMG_FILE_IN_MY_SERVER'> This is the subject"
$mail->Subject = $subject;
Already tried to use the $mail->isHTML(true); but for what I read, it works only for the email's body.
Definitively, no. Email doesn't support HTML in the subject line, or any other header.

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

HTML characters in mail 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

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

TextArea line breaks for e-mail

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.

Categories