Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Olá.
Sou novo no fórum, e também com PHP. Fiz e faço alguns scripts para sites com um amigo meu.
O que ocorre é que me foi pedido para fazer um formulário de contato que envie um email com anexo, mas com a possibilidade de anexar mais de um arquivo.
Consegui com a ajuda de um
do youtube fazer o envio de email com anexo.
Com a ajuda de um script jquery, consegui fazer o upload múltiplo, e adaptei a parte do script que "insere" o anexo no email, colocando-a em um laço de repetição, mas o email recebido dessa forma fica apenas com o título e um anexo sem nome nem conteúdo.
Aqui o código alterado:
<?php
$date = date("d/m/Y h:i");
$resposta = "";
if (isset($_POST) && !empty($_POST)) {
$from = $_POST['email'];
$to = "raulzitomuniz@gmail.com";
$subject = $_POST['assunto'];
$message = "
ENVIADO POR:\n
Nome: $" . $_POST['nome'] . "\n
Email: $from\n
Telefone: " . $_POST['telefone'] . "\n
Mensagem: " . $_POST['mensagem'] . "\n
ENVIADO EM: $date";
$header = "From: " . $from . "\r\n";
$header .= "Reply-To: " . $replyto . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format. \r\n";
$header .= "--" . $uid . "\r\n";
$header .= "Content-type:text/plain; charset=iso-9989-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message . "\r\n\r\n";
if (!empty($_FILES['file']['name'])) {
$count = count($_FILES['file']['name']);
for ($i = 0; $i < $count; $i++) {
$file_name = $_FILES['file']['name'][$i];
$temp_name = $_FILES['file']['tmp_name'][$i];
$file_type = $_FILES['file']['type'][$i];
$base = basename($file_name);
$extension = substr($base, strlen($base) - 4, strlen($base));
$allowed_extensions = array(".doc", ".jpg", "jpeg", ".pdf", "docx", ".png");
if (in_array($extension, $allowed_extensions)) {
$file = $temp_name;
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time()));
$header .= "--" . $uid . "\r\n";
$header .= "Content-Type: " . $file_type . "; name=\"" . $file_name . "\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"" . $file_name . "\r\n\r\n";
$header .= $content . "\r\n\r\n";
} else {
$resposta .= "$file_name não enviado, formato inválido";
}
}
}
if (mail($to, $subject, "", $header)) {
$resposta .= "Email enviado";
} else {
$resposta .= "Falha ao enviar o email. Contate o administrador.";
}
}
echo "<script>window.location='contato.php?resposta=$resposta'</script>";
?>
o form que envia as informações:
<html xmlns="[http://www.w3.org/1999/xhtml">](http://www.w3.org/1999/xhtml)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript" src="upload/jquery-1.4.2.min.js" language="javascript" ></script>
<script type="text/javascript" src="upload/jquery.MetaData.js" language="javascript" ></script>
<script type="text/javascript" src="upload/jquery.MultiFile.js" language="javascript" ></script>
</head>
<body>
<?php
if (isset($_GET['resposta'])) {
echo $_GET['resposta'];
}
?>
<form action="attacher.php" method="POST" enctype="multipart/form-data">
Nome:<br />
<input name="nome" type="text" class="formFields" value="" size="30" /><br />
E-mail:<br />
<input name="email" type="text" class="formFields" value="" size="30" /><br />
Telefone:<br />
<input name="telefone" type="text" class="formFields" value="" size="30" id="telefone" /><br />
Assunto:<br />
<input name="assunto" type="text" class="formFields" value="" size="30" id="assunto" /><br />
Mensagem:<br />
<textarea name="mensagem" cols="40" rows="10" class="fechar"></textarea>
<input type="file" class="multi" name="file[]" />
<input name="submit" type="submit" class="informacao" value=" Enviar " />
<input name="reset" type="reset" class="informacao" value=" Limpar " />
</form>
</body>
</html>
Se puderem me ajudar a solucionar este problema ficarei muito agradecido.Carregando comentários...