Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Form mail inserting ! and line break into long strings

I have a form on my company's site which takes a name, telephone number, and comments (and a couple other things). The comments box lets you type up to 5000 characters - a large limit to allow for very verbose customers. A valid form has its contents sent using php form mail as a plain-text e-mail to our sales dept.

For some reason, if the Comments are longer than about 1000 characters, they will have and exclamation point, a line break, and sometimes an indent inserted in. Note this applies to the e-mail only; if the form has an error in it, the data gets inserted into the form and errors are marked, and the comments do not have the exclamation marks + line breaks yet.

I found one forum post about it suggesting that there is a character limit of about 990 characters that leads to this problem.

Does anyone know the cause? Does anyone know a fairly easy fix for this?

Relevant PHP code:

$to = $email;

$subject = "Website Order Received: $offer";

$contents = "
Order Form Received -\n
Name: $name\n
Company: $company\n
Email: $email\n
Phone: $phone $phoneExt\n
Order Contents:\n" .
($offer == 'web-demo' ? "- I want a live software demonstration.\n" : "") .
($offer == 'pricing' ? "- I'd like pricing information.\n" : "") .
($offer == 'holiday-pricing' ? "- I'd like to sign up before December 31st for the special holiday offer!\n" : "") .
($offer == 'bid-help' ? "- Please give me marketing materials and other assistance for winning bids.\n" : "") .
($offer == 'demo-cd' ? "- Send me the full-version demonstration CD.\n" : "");
if (!empty ($comments)) {
    $comments = str_replace("
", "\n", $comments); // Preserves line breaks in the comments.
    $contents = $contents."\nComments: $comments\n\n";
}
$contents = str_replace("\n", "\r\n", $contents);

mail($to, $subject, $contents);
like image 674
Martin Avatar asked Jan 09 '12 21:01

Martin


People also ask

How do I insert a line break into the body of an email in PHP?

Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code.

How to give br in PHP?

Apart from nl2br() function, a html break line tag </br> is used to break the string. The </br> tag should be enclosed in double quotes, e.g., "</br>". To create the newline in PHP, let's see the number of examples.


1 Answers

There is a limit to the number of characters in a line of an email:

There are two limits that this standard places on the number of characters in a line. Each line of characters MUST be no more than 998 characters, and SHOULD be no more than 78 characters, excluding the CRLF. (RFC 2882)

You can use the PHP function wordwrap to achieve this:

$contents = wordwrap($contents);

In any case, this will improve the readability of the emails sent with your script, as well as making them standards-compliant.

like image 128
lonesomeday Avatar answered Nov 14 '22 22:11

lonesomeday