Bad html code when I send mail with PHP

130 Views Asked by At

I have a problem when I send mail by PHP. This is my complete code:

<?php

if ($_POST){

$num = md5(time()); 

//MAIL BODY
$body = "
<html>
<head>
<title>CadizCarnaval - Colabora</title>
</head>
<body style='background:#EEE; padding:30px;'>
<h2 style='color:#767676;'>Mensaje recibido sección Colabora.</h2>";

$body .= "
<strong style='color:#0090C6;'>Nombre: </strong>
<span style='color:#767676;'>" . $_POST["nombre"] . "</span><br>";

$body .= "
<strong style='color:#0090C6;'>Email: </strong>
<span style='color:#767676;'>" . $_POST["mail"] . "</span><br>";

$body .= "
<strong style='color:#0090C6;'>Mensaje: </strong>
<span style='color:#767676;'>" . $_POST["mensaje"] . "</span><br>";

$body .= "</body></html>";

$nombre = $_POST['nombre'];
$email = $_POST['mail'];
$asunto = $_POST['asunto'];
$mensaje = $_POST['mensaje'];
$para = '[email protected]';

// MULTI-HEADERS Content-Type: multipart/mixed and Boundary is mandatory.
$headers = "From: $nombre <$email>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; "; 
$headers .= "boundary=".$num."\r\n";
$headers .= "--".$num."\n"; 

// HTML HEADERS 
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$body."\n";
$headers .= "--".$num."\n";

if (isset ($_FILES["archivos"])) {
    $tot = count($_FILES["archivos"]["name"]);
    for ($i = 0; $i < $tot; $i++){
        $_name=$_FILES["archivos"]["name"][$i];
        $_type=$_FILES["archivos"]["type"][$i];
        $_size=$_FILES["archivos"]["size"][$i];
        $_temp=$_FILES["archivos"]["tmp_name"][$i];  

        //FILES EXISTS
        if(strcmp($_name, "")){
            $fp = fopen($_temp, "rb");
            $file = fread($fp, $_size);
            $file = chunk_split(base64_encode($file));
        }

        // FILES HEADERS
        $headers .= "Content-Type:application/octet-stream ";
        $headers .= "name=\"".$_name."\"r\n";
        $headers .= "Content-Transfer-Encoding: base64\r\n";
        $headers .= "Content-Disposition: attachment; ";
        $headers .= "filename=\"".$_name."\"\r\n\n";
        $headers .= "".$file."\r\n";
        $headers .= "--".$num."\r\n";
    }


}else { //FILES NO EXISTS

// HTML HEADERS
$headers = "From: $nombre <$email>\r\n";
$headers .= "Ficheros adjuntos no recibidos.\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
} 

// SEND MAIL
if (mail($para, $asunto, $body, $headers)){
    echo "<script language='javascript'> alert('Mensaje enviado, muchas gracias.'); window.location.href='http://[...]/inicio.html';</script>";
} 
else {
    echo "Falló el envio";
}
}
?>

Sends email correctly but shows html code that I do not want:

enter image description here

I need your help to remove this last html code. Thanks.

1

There are 1 best solutions below

1
On

Use single quotes in $_POST["nombre"], $_POST["mail"], $_POST["mensaje"]