Question

I'm trying to send an email with PHP.

This is my code:

$subject = 'You got mail';
$message = 'This is one line.\n' .
           'This is another.';
mail('your@email.com', $subject, $message);

When I receive the mail, the two lines of the message body appear as one line, with the newline \n given as part of the message text:

This is one line.\nThis is another.

Why is that, and how can I get my lines to break?


Solution:

ôkio's answer is correct. But instead of using double quotes, I now use the predefined end-of-line variable:

$message = 'This is one line.' . PHP_EOL .
           'This is another.';
Was it helpful?

Solution

Try using double quotes :

$subject = "You got mail";
$message = "This is one line.\n" .
           "This is another.";
mail('your@email.com', $subject, $message);

Single quote do not interpret the content of the string and display it as it is. To do so, you must use double quotes.

A more complete answer : What is the difference between single-quoted and double-quoted strings in PHP?

OTHER TIPS

In order to make PHP to interpret your \n character as new line, you have to enclose it in double quotes:

$subject = 'You got mail';
$message = 'This is one line.'. "\n".
           'This is another.';
mail('your@email.com', $subject, $message);

if it plain text It's suppose to be: \r\n. you should use html mail and then use
its much better.

It would be best to write your email content as HTML, so you can do all the formatting that your heart desires.

First, write that html content. Then before calling mail(), set the headers;

$message = '<p>This is one line</p><p>This is another.</p>';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

mail('your@email.com', $subject, $message);

Look at the examples here: http://php.net/manual/en/function.mail.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top