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?
– Sergio
In the other I didn’t even receive the file, now I get but it comes blank
– Sarah
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.
– user60252
@Leocaracciolo yes has it all
– Sarah
I’m sorry, there was an error in the answer code and I fixed it.
– user60252