Search code examples
phpline-breaks

Adding a line break in a series of php $ variables?


The below code is designed to send an email when a sale has been made. The body of the email contains information like the customers name, their email address etc. All I'd like to do is add a line break to each of the fields within the final output of the email. I've tried a simple HTML line break and /n and /r which didnt work....any ideas? Thanks!

<?php

//Enter email to receive purchase alerts
///////////////////////////////////////////
$alert_email = 'sales@my-domain.com';
///////////////////////////////////////////

$username = $_POST['username'];
$password = $_POST['password'];

$to      = $alert_email;
$subject = 'Sale! = ' .$course;
$message = $name. ' '. $email. ' '. $username. ' '. $password; 
$headers = 'From: sales@my-domain.com' . "\r\n" .
'Reply-To: hi@my-domain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

?>

The line that needs to be changed I am sure is this one (and line breaks inserted somewhere here:

$message = $name. ' '. $email. ' '. $username. ' '. $password; 

Thanks!


Solution

  • Send a HTML content by applying header Content-type:text/html

    $to      = $alert_email;
    $subject = 'Sale! = ' .$course;
    
    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    
    // More headers
    $headers .= 'From: <sales@my-domain.com>' . "\r\n";
    $headers .= 'Reply-To: hi@my-domain.com' . "\r\n";
    
    $message = $name. '<br /> '. $email. '<br /> '. $username. '<br /> '. $password; 
    
    mail($to, $subject, $message, $headers);