PHP interrupt lines do not work

I am trying to create a letter using line breaks, so it looks like this:

Name:
Email:
Message:

I added \r\n, but did nothing, then I tried to add $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";and thought that I need to say that this is HTML, and that also did nothing.

What am I doing wrong?

<?php
   session_start();

   if ($_SERVER['REQUEST_METHOD'] == 'POST'){
      ob_start();

      if(isset($_POST['name'])
      && isset($_POST['email'])
      && isset($_POST['message'])
      && isset($_POST['token'])){

         if($_SESSION['token'] != $_POST['token']){
            $response = "0";
         } else {
            $_SESSION['token'] = "";
            $name = $_POST['name'];
            $email = $_POST['email'];
            $message = $_POST['message'];

            $to = "support@loaidesign.co.uk";
            $subject = "New Message From: $name";
            $message = "Name: $name\r\n
                        Email: $email\r\n\r\n
                        Message: $message";

            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
            $headers .= 'From: '.$email . "\r\n";
            $mailed = ( mail($to, $subject, $message, $headers) );

            if( isset($_POST['ajax']))$response = ($mailed) ? "1" :
            "0"; else $response = ($mailed) ? "<h2>Success!</h2>" :
            "<h2>Error! There was a problem with sending.</h2>";
            echo $response;
         }

      } else {
         echo "Form data error!";
      }

      ob_flush();
      die();
   }

?>
+4
source share
4 answers

You should try with <br/>HTML tags because you specified the content type text/html:

$message = "Name: $name<br/>
            Email: $email<br/><br/>
            Message: $message";
+6
source

To display line breaks in html you have to use <br/>(or use tags <pre></pre>if you really care about use \r\n).

, php PHP_EOL.

+4

"\ r\n" html : br - .

0

text/html try:

$message = "Name: $name\r\n".
           "Email: $email\r\n\r\n".
           "Message: $message";
0
source

Source: https://habr.com/ru/post/1525285/


All Articles