OK, this may be an elementary question, and hopefully there is a quick and easy resolution...

I have always had this issue and previously just dealt with it, but want to get it working the way it should be.

When I generate an email from within a PHP script that includes form input from a visitor, it always comes through as a single continual block of text, no matter how it is entered.

Is there a way to at least format it with line feeds for each carriage return they enter?

I run all form entry through a clean string function and then send it to a mail sender function included below.

Any suggestions would be greatly appreciated. And if there is a more up to date means of handling email sending that wouldn't hurt either... I've been using these functions for years.


I thought that it may be the clean string function I use so I bypasse it temporarily.

this is the message as it is delivered
Message :
Testing without the cleanstring function. Thinking that maybe the linefeeds will at least be intact. testing.

and as it was entered in the textarea form field:

Testing without the cleanstring function.

Thinking that maybe the linefeeds will at least be intact.


// ***********************************************************************
// trim white space front and back / replace special characters/protect DB data
function cleanString($string){
  return $string;
}//end function

// Function to send emails
//  call with email address / from address / subject / body of message
function mail_sender($to,$from,$subject,$message_body)
$message = "<html><head><title>".$sitename."</title></head><body>";
$message .= "</body></html>";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";// More headers
$headers .= "From: <".$from.">" . "\r\n";   //  modified function to include $from
$message = wordwrap($message,70);
// Send email

And just in case it comes up as being needed, here is the form I am currently using.

                <!-- CONTACT FORM -->

                    <form  action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">
                    <label>Name<?php if ($name_error != ''){print ' - <b><font color="#FF0000">'.$name_error.'</font></b>';} ?></label>
            <input class="span6" type="text" name="cf_name" value="<?php print $prev_cf_name; ?>"><br>

                    <label>E-Mail<?php if ($email_error != ''){print ' - <b><font color="#FF0000">'.$email_error.'</font></b>';} ?></label>
            <input class="span6" type="text" name="cf_email" value="<?php print $prev_cf_email; ?>"><br>

                    <label>Phone<?php if ($phone_error != ''){print ' - <b><font color="#FF0000">'.$phone_error.'</font></b>';} ?></label>
            <input class="span6" type="text" name="cf_phone" value="<?php print $prev_cf_phone; ?>"><br>

                    <label>Subject<?php if ($subject_error != ''){print ' - <b><font color="#FF0000">'.$subject_error.'</font></b>';} ?></label>
            <input class="span6" type="text" name="cf_subject" value="<?php print $prev_cf_subject; ?>"><br>

                    <label>Message<?php if ($message_error != ''){print ' - <b><font color="#FF0000">'.$message_error.'</font></b>';} ?></label>
            <textarea class="span8" name="cf_message" rows="5" cols="25"><?php print $prev_cf_message; ?></textarea><br>

            <input type="hidden" name="subbed" value="y">
                    <label>&nbsp;</label><input class="btn btn-large btn-info" type="submit" name="submit" value="Send Message">

              <!-- END CONTACT FORM -->

Recommended Answers

All 5 Replies

Maybe not a good idea to use wordwrap() after you've sorted the entire HTML of the message out. Maybe add it right at the beginning if your that desperate to use it, but I would consider some validation instead of just chopping half of the message off.

Apart from that, It could be because you're using <input>, which is submitted as one continual block of text. What you may be interested in is a HTML Editor, like TinyMCE. This will allow you to format your email using HTML, so therefore it can have user-defined line breaks and other formatting bits and bobs.

I can't see anything other thn that in your code, but there could be some issue when calling the send function, so that's the place to look afterwards.

However, be mindful that this type of email can have security issues, so be very careful and just make sure you properly sanitize everything.

Thank you mattster for your response...

it has been suggested to me a few times over the years in other queries to use TinyMCE in my scripting. Quite honestly, I've downloaded it a couple of times to do just that, but was unsuccessful in those attempts. I don't know if it was just a lack of patience at the time or if I just wasn't smart enough to figure it out.

Looks like they have released a new version since my last attempt, and it appears that they may have simplified it, so I will try one more time.

Could you give me an idea of how it is implemented in a situation like mine, where I just have a simple Contact Form on a website for a visitor to fill out and submit for more information?

Does it simply take the place of the Textarea input? I just don't want to invest a lot of time in something that ultimately won't be completed, since I've tried it a couple times in the past.

Thanks in advance for your response.


Sure no worries, its pretty simple aha.
A quick TinyMCE (with CDN) tutorial:

1) Shove this into your page, just before the </body> tag if possible

<script src="http://tinymce.cachefly.net/4.1/tinymce.min.js"></script>

And here you are, done! You'll have the default one going, so if you want to snazz it up a bit or change the settings, just alter the bit inside tinymce.init({}) bit.

All it does is changes the look of your textarea, and submits HTML code instead of plain text. So all it is doing is helping your user add HTML to their textarea, if that makes any sence.

Thank you again for your response, but after posting my question, I decided to download it and see what I could do...

Got it working perfectly as soon as I got the path to the file set correctly.

Thanks again for your help.

That's really good to know :)

No worries anytime :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.