Home > Net >  After define the header breaks dont work anymore
After define the header breaks dont work anymore

Time:03-10

after I define a PHP header with UTF-8 my breaks on $msg don't work anymore. Where is the problem? Is there another way to make breaks?

Thanks!

Code:

<?php  

$username = $_POST['name'];
  $lastname = $_POST['lastname'];
$useremail = $_POST['mail'];
  $fon = $_POST['phone'];
$usermsg = $_POST['nachricht'];
$myemail = "Mail@XY";

$msg = "Antwort an: $useremail\n". "Name: $username $lastname \n". "Telefonnummer: $fon \n". "Nachricht: $usermsg";

$subject = "Neue Nachricht vom Kontaktformular";

$headers = 'Content-type: text/html; charset=utf-8'. "\r\n"
          .'From: ' . $myemail . "\r\n";

if(empty($username)||empty($useremail)||empty($usermsg)){
  header("location:index.html?error");
}

else{
  $to = "Mail@XY";
  mail($to, $subject, $msg, $headers);
} 

?>

CodePudding user response:

It's not the utf-8 which has caused this, it's the text/html.

You're telling the receiving mail client to parse and display the email as a HTML document. As you hopefully know, in HTML a line break is specified using the <br> tag. HTML rendering engines do not take any notice of \n or \r\n - these are only useful in plain-text documents.

This:

$msg = "Antwort an: $useremail<br>". "Name: $username $lastname <br>". "Telefonnummer: $fon <br>". "Nachricht: $usermsg";

should fix it.

CodePudding user response:

$msg = "Antwort an: $useremail\n". "Name: $username $lastname \n". "Telefonnummer: $fon \n". "Nachricht: $usermsg";

If you aim HTML-mail format, \ns in the $msg must be replaced with the HTML tag <br>.

$msg = "Antwort an: $useremail<br>". "Name: $username $lastname<br>". "Telefonnummer: $fon<br>". "Nachricht: $usermsg";

If you aim text-mail format then try PHP_EOL or \r\n instead of \n in your $msg.

  • Related