phppup Posted March 11 Share Posted March 11 (edited) I can probably find this answer by implementing the appropriate code, but in the planning stages, I thought i'd ask the more advanced brethren. The plan: a form that includes an HTML textarea where a user can type a message. Upon submission, the form will be processed and the contents of the textarea will be POSTed into an email that will be sent using the PHP function. The question: if a user types (using the keyboard return key) H E L P into the textarea, will it be displayed with new lines within the email? Or will it become a single line string? Are there particular settings that can be made so that the email is a duplicate in the EXACT format of text that is entered into the form? Edited March 11 by phppup Typos Quote Link to comment https://forums.phpfreaks.com/topic/318992-a-question-about-php-email/ Share on other sites More sharing options...
Barand Posted March 11 Share Posted March 11 see nl2br() Quote Link to comment https://forums.phpfreaks.com/topic/318992-a-question-about-php-email/#findComment-1617626 Share on other sites More sharing options...
gizmola Posted March 12 Share Posted March 12 It depends. Most people don't do email correctly. Technically speaking, an email done correctly should be multi-part mime with sections separated that include a content-type section, with a pure text version and charset (Content-Type: text/plain; charset="utf-8" for example), and a separate email version. For text, the newlines will be interpreted correctly. HTML does not honor newlines in whitespace, but as Barand linked to you, you can use nl2br(). These days, a lot of people just send html, and many email clients will figure this out and provide an html version of the email, even if it's the only part of the message body. This gist has some helpful notes, describing how a Multipart Mime email should be constructed. The better php email libraries take care of these details for you, if you utilize their api's correctly, but it helps to understand it in advance, as email deliverability is already a big problem for many companies. Quote Link to comment https://forums.phpfreaks.com/topic/318992-a-question-about-php-email/#findComment-1617633 Share on other sites More sharing options...
maxxd Posted March 12 Share Posted March 12 Whatever you end up doing with the email itself, do yourself a favor and use one of the major libraries instead of php's native mail() function. I'm most familiar with PHPMailer, but I believe laravel uses Symfony Mailer. Either is going to make your like much easier in the long run. Quote Link to comment https://forums.phpfreaks.com/topic/318992-a-question-about-php-email/#findComment-1617636 Share on other sites More sharing options...
Danishhafeez Posted March 12 Share Posted March 12 When a user types using the keyboard return key in a textarea, it typically creates new lines (\n) in the submitted content. So if the user types: H E L P It will be submitted as: H\nE\nL\nP When you process this content in PHP and send it via email, if you use the content as is without any modifications, the new lines will be preserved in the email. However, how the email client renders these new lines can vary. To ensure the exact format of the text entered into the form is preserved in the email, you should set the content type of the email to plain text and include appropriate headers. Here's a simple example using PHP's mail function: <?php $to = 'recipient@example.com'; $subject = 'User Request'; $message = $_POST['message']; // Assuming the textarea content is posted as 'message' $headers = 'From: your_email@example.com' . "\r\n" . 'Reply-To: your_email@example.com' . "\r\n" . 'Content-Type: text/plain; charset=UTF-8' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ?> In this example, $_POST['message'] contains the content of the textarea. By setting the content type to text/plain, you ensure that the email will be sent as plain text without any HTML formatting, which helps preserve the exact format of the text entered by the user. Best regard Danish Hafeez | QA Assistant ICTInnovations Quote Link to comment https://forums.phpfreaks.com/topic/318992-a-question-about-php-email/#findComment-1617638 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.