Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Plain text emails - newlines disappear!

Status
Not open for further replies.

Dronealone

IS-IT--Management
Mar 13, 2002
64
GB
I'm trying to generate a plain text email from a textarea on a webpage.

However, I cannot get newlines to appear in the email.

The code is as follows:

TEXTAREA:

<textarea name=&quot;email&quot; cols=&quot;130&quot; rows=&quot;15&quot;><?php echo stripslashes($email); ?></textarea>

MAIL CODE:

[PLS SEE COMMENTS NEXT TO EACH LINE]

//$email = stripcslashes($email); //If I manually add the \n character in the textbox, they then appear in $email with \\n. I can use stripcslashes to get rid of the additional \. This makes the string identical to the manually assigned one below, but does not display newlines in the email - it shows the \n.

//$email = &quot;this\nis\nan&quot;; //If I assign the variable directly newlines are added

$headers .= &quot;Content-Type: text/plain; charset=ISO-8859-1\r\n&quot;;

for ($i = 0; $i < count($recipient); $i++)
{

mail($recipient[$i], $subject, $email, $headers);

}

break;

So if I don't manually add the \n to the characters in the textbox, the email all displays on one line. If I add the \n, another \ is added. I can then strip the slash, however this just displays the \n in the email Finally if I manually assign the value to the variable equivalent to the last step, it works.

What I want is people to be able to type in the textbox, use the return key and this be reflected as newlines in the email....

I've been stuck for ages on this.....Please help.

Thanks!

 
My guess is that the text box is using a different character for newlines than you are...

To diagnose it I'd search through the $email (as assigned by the textbox) using the ord() function... when you get to whatever it is that you expect to be a linebreak, take note of that character.

Then run a string_replace which replaces that character with \n.

If that didn't make sense lemme know and I'll try and be more explicit.

-Rob
 
Thanks for that worked a treat.

Basically CRLF was being added at newlines, when I removed the CR and left the LF it worked fine.

Thanks a lot!
 
Hi,

Fixed the newline problem, however I am now getting a different problem. I am using an uploaded .txt file as the source of the plain text. This works fine, except when there is a zero in the text, if there is the email stops at this point. Anybody any ideas? The code I am using is below:

case &quot;plain&quot;:

if(is_uploaded_file($file))
{
$fd = fopen ($file, &quot;r&quot;);

while($char = fgetc($fd))
{

$text = ord($char);

if($text != &quot;13&quot;)
{
$contents .= chr($text);

}

}

fclose ($fd);

}

$headers .= &quot;Content-Type: text/plain; charset=ISO-8859-1\r\n&quot;;

for ($i = 0; $i < count($recipient); $i++)
{

mail($recipient[$i], $subject, $contents, $headers);

}

break;

Thanks very much.
 
I think fgetc is reading the 0 as the end of a string. You can use this code to do it in chunks rather than by character.

I caution you though, only looking for a carriage return is likely to cause some problems... sleipnir posted a couple weeks ago the regular expression to catch the different kinds of linebreaks... I'm sure you can search for it, or if not he's usually kind enough to repost :).

Good luck.

Code:
$fd = fopen ($file, &quot;r&quot;);

/* Read the value of the file into $contents 4k at a time */
$contents = &quot;&quot;;
while (!feof ($fd)) {
    $contents .= fgets($fd, 4096);
}

$pat = &quot;/chr(13)/&quot;;
$replacement = &quot;&quot;;
$buffer = preg_replace($pat, $replacement, $buffer);

fclose ($fd);

/* Untested let me know how it goes */

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top