Mail() -> white attached php file

Asked

Viewed 345 times

0

I created an html page with a modal with a form that sends the data filled in the fields and also sends an attached file. I get the email and the file, but when downloading the file and opening it is blank with nothing written. This is the full php code:

<?php
if (isset($_POST['btn-enviar'])) {
    //Variaveis de POST
    //====================================================
    $nome = $_POST["nome"];
    $dados= $_POST["dados"];
    $email_form = $_POST["email_form"];
    //====================================================
    //email para o qual vamos enviar
    //====================================================
    $email = "[email protected]";
    //====================================================
    //Separador das partes do e-mail
    //====================================================
    $boundary = "XYZ-".md5(date("dmYis"))."-ZYX";
    //====================================================
    // Arquivo enviado via formulário
    //====================================================
    $path = $_FILES['arquivo']['tmp_name']; 
    $fileType = $_FILES['arquivo']['type']; //tipo
    $fileName = $_FILES['arquivo']['name']; //nome
    //====================================================
    // Pegando o conteúdo do arquivo
    //====================================================
    $fp = fopen( $path, "rb" ); // abre o arquivo enviado
    $anexo = fread( $fp, filesize( $path ) ); // calcula o tamanho
    $anexo = chunk_split(base64_encode( $anexo )); // codifica o anexo em base 64
    fclose( $fp ); // fecha o arquivo
    //====================================================
    // Cabeçalho do email (evitar span)
    //====================================================
    $headers = "MIME-Version: 1.0" . PHP_EOL;
    $headers .= "Content-Type: multipart/mixed; ";
    $headers .= "boundary=" . $boundary . PHP_EOL;
    $headers .= "$boundary" . PHP_EOL;
    //====================================================
    //Definição da mensagem em HTML
    //====================================================
    $assunto = "FJUni Currículo - $nome";
    $mensagem  = "--$boundary" . PHP_EOL;
    $mensagem .= "Content-Type: text/html; charset='utf-8'" . PHP_EOL;
    $mensagem .= "Nome: $nome \n"; 
    $mensagem .= "\n Dados: $dados\n";
    $mensagem .= "\n E-mail: $email_form \n"; 
    $mensagem .= "\n Assunto: \n $assunto \n";  // Adicione aqui sua mensagem
    $mensagem .= "--$boundary" . PHP_EOL;
    //====================================================
    //Anexando um arquivo
    //====================================================
    $mensagem .= "Content-Type: ". $fileType ."; name=\"". $fileName . "\"" . PHP_EOL;
    $mensagem .= "Content-Transfer-Encoding: base64" . PHP_EOL;
    $mensagem .= "Content-Disposition: attachment; filename=\"". $fileName . "\"" . PHP_EOL;
    $mensagem .= "$anexo" . PHP_EOL;
    $mensagem .= "--$boundary" . PHP_EOL;
    //====================================================
    //Enviando o email
    //====================================================
    mail($email, $assunto, $mensagem, $headers); 
    echo "Email enviado com Sucesso!";
    //====================================================
}
?>

HTML form:

<form id="form-modal" action="envia.php" method="post" enctype="multipart/form-data" name="formulario">
                <h3>Preencha o formulário abaixo com os seus dados:</h3>
                <p class="campo-form">
                    <label>Nome *:</label>
                   <input type="text" name="nome" required/>
                </p>
                <p class="campo-form">
                    <label>Dados *:</label>
                    <input type="text" name="dados" required/>
                </p>
                <p class="campo-form">
                    <label>E-mail *:</label>
                    <input type="email" name="email_form" required/>
                </p>

                <p id="frase-form">Clique no botão abaixo para anexar o seu arquivo.</p>
                </br>
                <center>
                    </br>
                    <label for='btn-anexo' class="label-btn">ANEXAR ARQUIVO</label>
                    </br>
                    </br>
                    </br>
                    </br>
                    </br>
                    <input id="btn-anexo" type="file" name="arquivo"/>

                    <input id="btn-envio" type="submit" name="btn-enviar" value="ENVIAR" />
                </center>
            </form>

Please help me if anyone knows! :(

  • What is the difference between this question and the other what you had done?

  • In the other I didn’t even receive the file, now I get but it comes blank

  • shows the form code. Checks whether the input type file has name="file". Check that your form contains the enctype="Multipart/form-data" attribute without which the upload will not work.

  • @Leocaracciolo yes has it all

  • I’m sorry, there was an error in the answer code and I fixed it.

1 answer

1


The correction was made in the passage Attaching a file.

if (isset($_POST['btn-enviar'])) {
//Variaveis de POST
//====================================================
$nome = $_POST["nome"];
$dados= $_POST["dados"];
$email_form = $_POST["email_form"];
//====================================================
//email para o qual vamos enviar
//====================================================
$email = "[email protected]";
//====================================================
//Separador das partes do e-mail
//====================================================
$boundary = "XYZ-".md5(date("dmYis"))."-ZYX";
//====================================================
// Cabeçalho do email (evitar span)
//====================================================
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=" . $boundary . PHP_EOL;
$headers .= "$boundary" . PHP_EOL;
//====================================================
//Definição da mensagem em HTML
//====================================================
$assunto = "FJUni Currículo - $nome";
$mensagem = "--$boundary" . PHP_EOL;
$mensagem .= "Content-Type: text/html; charset='utf-8'" . PHP_EOL;
$mensagem .= "\n Nome: $nome " . PHP_EOL ."<br>"; 
$mensagem .= "\n Dados: $dados" . PHP_EOL ."<br>";
$mensagem .= "\n E-mail: $email_form" . PHP_EOL ."<br>"; 
$mensagem .= "\n Assunto: $assunto" . PHP_EOL; // Adicione aqui sua mensagem
//====================================================
//Anexando um arquivo
//====================================================

$arquivo = isset($_FILES["arquivo"]) ? $_FILES["arquivo"] : FALSE;
if(file_exists($arquivo["tmp_name"]) and !empty($arquivo)){
$fp = fopen($_FILES["arquivo"]["tmp_name"],"rb");
$anexo = fread($fp,filesize($_FILES["arquivo"]["tmp_name"]));
$anexo = base64_encode($anexo);
fclose($fp);
$anexo = chunk_split($anexo);
$mens = "--$boundary\n";
$mens .= "Content-Transfer-Encoding: 8bits\n";
$mens .= "Content-Type: text/html; charset=\"ISO-8859-1\"\n\n";
$mens .= "$mensagem\n";
$mens .= "--$boundary\n";
$mens .= "Content-Type: ".$arquivo["type"]."\n";
$mens .= "Content-Disposition: attachment; 
filename=\"".$arquivo["name"]."\"\n";
$mens .= "Content-Transfer-Encoding: base64\n\n";
$mens .= "$anexo\n";
$mens .= "--$boundary--\r\n";
}else{
$mens=$mensagem;
}

//====================================================
//Enviando o email
//====================================================
mail($email, $assunto, $mens, $headers); 
echo "Email enviado com Sucesso!";
//====================================================
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.