Pedro Henrique Kuzminskas Pedro Henrique Kuzminskas - 1 month ago 14
PHP Question

line breaks isnt working

why the message is coming in one line? after i added the

$headers
my message started become in single line.

<?php

// your email
$user_email = "marketing@fontlife.com.br";

$mail = array(
"name" => htmlspecialchars($_POST['cf_name']),
"email" => htmlspecialchars($_POST['cf_email']),
"subject" => htmlspecialchars($_POST['cf_subject']),
"assunto" => 'Novo contato realizado via website',
"message" => htmlspecialchars($_POST['cf_message'])
);

$nome = $_POST['cf_name'];
$email = $_POST['cf_email'];
$assunto = $_POST['cf_subject'];
$assunto_mensagem = 'Novo contato realizado via website';
$mensagem = $_POST['cf_message'];

$assunto = 'Novo contato realizado a partir do site Font Life';

$headers = "MIME-Version: 1.1\r\n".
"Content-type: text/html; charset=utf-8\n".
"Content-Transfer-Encoding: 8bit\n".
"From: ". $_POST['cf_name'] ." <". $_POST['cf_email'] .">\n".
"Reply-to: ". $_POST['cf_name'] ." <". $_POST['cf_email'] .">\n".
"Date: ". date( "r" ). "\n";

function validate($arr){

return !empty($arr['name']) && strlen($arr['message']) > 20 && filter_var($arr['email'],FILTER_VALIDATE_EMAIL);

}

if(validate($mail)){

echo mail($user_email, $mail['assunto'],
"Nome : $nome\n"
."Assunto : $assunto\n"
."E-mail : $email\n"
."Mensagem : $mensagem\n",
$headers
);

}

?>


the message is coming like these:

Nome : Pedro Assunto : Novo contato realizado a partir do site Font Life E-mail : pedro.hkms@outlook.com Mensagem : asdklnasklasdmklasdmasd├žlasd,m├žasd

there is an error in my code?

Answer
  1. You should split the headers with \r\n (and not only \n).
  2. The content part should be html - so in the content you should use <br> for line-break.


This is the headers part:

$headers = "MIME-Version: 1.1\r\n".
           "Content-type: text/html; charset=utf-8\r\n".
           "Content-Transfer-Encoding: 8bit\r\n".
           "From: ". $_POST['cf_name'] ." <". $_POST['cf_email'] .">\r\n".
           "Reply-to: ". $_POST['cf_name'] ." <". $_POST['cf_email'] .">\r\n".
           "Date: ". date( "r" );

This is the call to the mail function:

mail($user_email, $mail['assunto'],
    "Nome : $nome<br />\n". 
    "Assunto : $assunto<br />\n".
    "E-mail : $email<br />\n".
    "Mensagem : $mensagem<br />\n",
    $headers
);
Comments