Jump to content

A question about PHP email


phppup

Recommended Posts

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 by phppup
Typos
Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.