Hi all,

I have this script sending an really simple html email. It works fine, but I cant seem to preserve the line breaks when sending the email to the client.

Any idea how I can preserve line breaks? Thought nl2br would do the trick..

// Data der skal emailes :
    $msg    = strip_tags( $_GET['msg'] );
    $msg    = nl2br( $msg );        
    $page   = $_GET['page'];

    // Modtager :
    $to = 'xxx@hotmail.com';

    // Email emne linje :
    $subject = 'Besked fra webshop - Fejl i specifikationer';

    // Email besked :
    $message = '
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Specifikations fejl på webshop</title>
    </head>
    <body>
      <div style="width:70%; height:500px; background:#f5f5f5; border:1px solid #f9f9f9; padding:10px;">
        <div style="width:100%; height:3px; background:#333;"></div>
        <br />
        <h2>Email vedr. specifikations fejl fra:</h2>
        <h4>'.$page.'</h4>
        <br />
        <hr />
        <br />
        <h2>Kundens besked:</h2>
        <br />
        '.$msg.'
      </div>
    </body>
    </html>';        

    // To send HTML mail, the Content-type header must be set
    $headers  = "From: Webshop - Specifikations fejl indberettet\r\n"; 
    $headers .= "Content-type: text/html\r\n";

    // Afsend email og udskriv en bekræftelse :
    if( mail($to, $subject, $message, $headers) )
    {
        echo '<p>Mange tak for din besked! Vi vil se på din henvendelse snarest muligt.</p>'; 
    }

I would expect in html formated mail <br> tag would produce a line break. This can't be the case in your message ($msg) since you are stripping all the tags. Maybe you should do it this way:

$msg = strip_tags( $_GET['msg'], '<br>' );

provided that $_GET['msg'] already contains html breaks (please test if this removes also <br/> and <br /> tags).

[ EDIT ] please disregard my post since i missinterpreted it. You are already using nlb2br() function. Sory.

@broj1
Thanks for the input. Im not sure if it could be the headers maybe? Arent they looking like they should you reckon?

I don't see anything incorrect in headers. But since the message is appended to the URI (by GET method) how are linebreaks encoded? Normal linebreaks are not allowed in the URI.

Its sent via ajax using get - I will give it a try with post... - Also takes larger messages anyways.

Yes, that would be better. GET is recommended only for retrieval of resources from the server and allows a limited set of characters, characters that are not allowed should be encoded.

Well now I have changed the method to post, thhis is it: (STILL no line breaks..) - Have used that script before, so im a bit confused here.. Any ideas?

function send_email()
{
// Besked indtastet fra kunde hente - og siden den er skrevet fra
var msg     = document.getElementById('feedback_value').value;  
var page    = document.URL;

// xmlhhttprequest til php fil der modtager get svar og afsendser email
var xmlhttp;
    if ( window.XMLHttpRequest ) 
    {
        xmlhttp = new XMLHttpRequest();
    } else 
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
// Handling af response fra email script
xmlhttp.onreadystatechange = function ()
{
    if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 )
    {
        document.getElementById( "response_target" ).innerHTML = xmlhttp.responseText;
    }
    // Else = error i xmlhttprequest - Log respons i console
    else
    {
        console.log( "xmlhttp.responseText" );
    }
 }
// Send besked til php filen der emailer den videre
xmlhttp.open( "POST", "spec-fejl-send-email.php", true );
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("msg="+msg+"&page="+page);
}

Recieving it in php here:

if( isset( $_POST['msg'], $_POST['page'] ) )
{
    // Data der skal emailes :
    $msg    = nl2br( $_POST['msg'] );
    $msg    = strip_tags( $_POST['msg'] );
    $msg    = trim( $msg );
    $msg    = wordwrap( $msg, 70);
    $msg    = str_replace( "\n.", "\n..", $msg );

    if( empty( $msg ) )
    {
        $msg = '<p>Kunden har ikke indtastet en besked.</p>';
    }

    $page   = $_POST['page'];

    // Modtager :
    $to = 'xxx';

    // Email emne linje :
    $subject = 'Besked fra webshop - Fejl i specifikationer';

    // Email besked :
    $message = '
    <html>
    <head>
      <title>Specifikations fejl på webshop</title>
    </head>
    <body>       
    <table width="70%">
        <tr>
            <td style="padding:10px; min-height:500px; background:#F5F5F5; border:1px solid #f9f9f9;">
                <table width="100%">
                    <tr>
                        <td>
                            <div style="width:100%; height:3px; background:#CCC;">&nbsp;</div>
                        </td>
                    </tr>
                </table>
                    <h2>Email vedr. specifikations fejl fra:</h2>
                    <h4>'.$page.'</h4>
                    <hr />
                    <h2>Kundens besked:</h2>
                    <br />
                    '.$msg.'            
            </td>
        </tr>        
    </table>       
    </body>
    </html>';        

    // Headers :        
    $headers  = "From: Webshop - Specifikations fejl indberettet\r\n"; 
    $headers .= "Content-type: text/html\r\n";  

    // Afsend email og udskriv en bekræftelse :
    if( mail( $to, $subject, $message, $headers ) )
    {
        echo '<p>Mange tak! En af vores medarbejdere har modtaget din besked, og vil hurtigst muligt rette eventuelle fejl.</p>'; 
    }
    // Else - Der var en fejl i afsendelse af email - Prøv igen :
    else 
    {
        echo '<p>Hov! Der skete en fejl da din besked blev afsendt - Prøv venligst igen.</p>';    
    }       
}
// Else - Der var ikke et get request der indeholdt 'msg'
else 
{
    echo '<p>Der opstod en fejl, prøv at send din besked igen..</p>'; 
}

Maybe you should chek out how the message looks when sent to server. Is it comming from a textarea? I suggest you put this temporary debug code before line 3:

die($_POST['msg']);

This will display what comes across and stop the script. You can examine the text and see how linebraks are being encoded or post the output here.

Right, just tried that:

output:

Hej her er der en besked. Denne her indeholder også æ ø å

The carachters are fixed later though.. But using die($_POST['msg']) gave me that line, with NO line breaks. So they are not sent with the requst or?

Is the source of the message a textarea?

I tested this with the textarea. If I enter linebreaks they are sent with POST as %0D%0A which is "\r\n" I guess. The nl2br() function adds <br> tags and newlines display nicely in HTML.

I think that because i first used nl2br, and then stripped tags, I never really got any <br />....

It is working now, thanks for the effort broj1, I appreciate it! :-)