Doug Smith
Doug Smith

Reputation: 29316

Why won't my PHP mail() function send it with the correct linebreaks?

Code is below. I want it to separate the [From: xxxxxx] part so the rest of the body/message appears on a different line.

<?php

    $name = $_POST["name"];
    $email = $_POST["email"];
    $message = $_POST["message"];

    $recipient = "[email protected]";
    $subject = "Message From Website";
    $body = "[From: " . $name . "]\n\n" . $message;
    $headers = "From: " . $email . "\r\n";
    $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";

    $success = mail($recipient, $subject, $body, $headers);

    echo $success;

?>

Upvotes: 0

Views: 80

Answers (1)

Marcio Mazzucato
Marcio Mazzucato

Reputation: 9285

You should use the HTML tag <br /> instead of \n

Like this:

$body = "[From: " . $name . "]<br /><br />" . $message;

And a suggestion using the mail() function:

if( mail($recipient, $subject, $body, $headers) )
    echo 'Success';
else
    echo 'Fail';

EDIT 1

As you are trying to send as plain text you should use \r\n, this is the escape sequence for new line in plain text. Example:

$formcontent="[From: " . $name . "]\r\n\r\n" . $message;

Upvotes: 2

Related Questions