Reputation: 91
My understanding of PHP is that you can use either "\n" or "\r\n" or echo "<br>";
to create a new line. But my application of them simply doesn't create a new line.
What am I doing wrongly here?
Here is the code:
<?php
session_start(); // before any HTML is echoed
if($_POST) {
//$email = "";
$email = $_POST['email'];
$password = $_POST['password'];
if(isset($_POST['email'])) {
$email = str_replace(array("\r", "\n", "%0a", "%0d"), '', $_POST['email']);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
}
if(isset($_POST['password'])) {
$password = htmlspecialchars($_POST['password']);
}
$recipient = "[email protected]";
$headers = 'MIME-Version: 1.0' . "\r\n"
.'Content-type: text/html; charset=utf-8' . "\r\n"
.'From: ' . $email . "\r\n";
$email_content .= "Email: $email" . "\r\n";
echo "<br />\n";
$email_content .= "Password: $password";
echo $email_content;
if(mail($recipient, $email_content, $headers)) {
header("Location: default-image.png");
echo " <script language=javascript>
//alert('Done, Click Ok');
window.location='default-image.png';
</script>";
} else {
echo '<p>ERROR! Please go back and try again.</p>';
}
} else {
echo '<p>Something went wrong</p>';
}
?>
Thanks for your time and input.
Upvotes: 1
Views: 60
Reputation: 835
You should use '\n' for new line it will show in output
$email_content .= "Email: $email" . "\n\n\n";
$email_content .= "\n\n";
$email_content .= "Password: $password";
echo $email_content;
you will see the password start with new line
Upvotes: 0
Reputation: 69
tl;dr
Use \n to clean your code. Use <br />
to add break lines. Make sure to concatenated <br />
to your variable.
The \n will clean up the source code view and the <br />
needs to be concatenated to your variable.
$email_content .= "Email: $email" . "<br />\n";
$email_content .= "Password: $password";
Note that if you don't include the \n, when viewing your source code in the browser, you might see that the html could be on the same line.
<?php
$email = "foo";
$password = "bar";
$email_content1 = "";
$email_content2 = "";
//without the \n to cleanup the source code
$email_content1 .= "Email: $email" . "<br />";
$email_content1 .= "Password: $password";
echo $email_content1;
//ignore, used for break
echo "\n\n<br />\n\n";
//with the \n to cleanup the source code
$email_content2 .= "Email: $email" . "<br />\n";
$email_content2 .= "Password: $password";
echo $email_content2;
?>
The source code would look like:
Email: foo<br />Password: bar
<br />
Email: foo<br />
Password: bar
Upvotes: 0
Reputation: 27295
You assign the values to a variable. The echo
is not in that context and will output your values to your page output.
$email_content .= "Email: $email" . "\r\n";
$email_content .= "<br />\n";
$email_content .= "Password: $password";
echo $email_content;
That is the correct way.
The next thing is that <br />
is the representation for a new line in HTML. \n
and \r\n
is the ASCII representation for a new line. This is mostly used in text files and other editors or CSV files for example. So you mixup different things.
Upvotes: 1