Warning: count(): Parameter must be an array or an object that implements Countable in /home/trapizomba/www/wp-includes/post-template.php on line 284

Warning: count(): Parameter must be an array or an object that implements Countable in /home/trapizomba/www/wp-includes/post-template.php on line 284

Warning: count(): Parameter must be an array or an object that implements Countable in /home/trapizomba/www/wp-includes/post-template.php on line 284

Warning: count(): Parameter must be an array or an object that implements Countable in /home/trapizomba/www/wp-includes/post-template.php on line 284

PHP: Enviando e-mail com anexo

Após muita procura, finalmente encontrei um script para enviar email em PHP com anexo. Esse aqui sim, funciona!

Como sou muito legal, e é muito difícil encontrar um código que realmente cumpra o que promete, vou compartilhar.

Precisamos criar dois arquivos:  “contato.html”, que conterá o formulário e onde vamos buscar o arquivo a ser anexado e o “envia.php”, que vai fazer o envio da mensagem.





Arquivo “contato.html”

<html>
<head>
<title>Contato </title>
</head>

<body>
<form id=”formulario” name=”formulario” action=”envia.php” enctype=”multipart/form-data” method=”post”>
<p>Nome</p>
<input name=”nome” type=”text” size=”30″ />
<p>Email</p>
<input name=”email” type=”text” size=”30″ />
<p>Telefone</p>
<input name=”telefone” type=”text” size=”30″ />
<p>Currículo</p>
<input type=”file” name=”fileAttach” class=”input”/>
<input name=”Enviar” type=”submit” value=”ENVIAR” class=”button” />

</body>
</html>

Arquivo “envia.php”

<?php
ob_start();
?>
<html>
<head>
<title>Enviando currículo</title>
</head>
<body>
<?
$strTo = “seuemailderecebimento.com.br”;
$strSubject = “Assunto da Mensagem”;
$strMessage = “Nome: “. ($_POST[“nome”]) . “<br />”;
$strMessage .= “E-mail: “. ($_POST[“email”]) . “<br />”;
$strMessage .= “Telefone: “. ($_POST[“telefone”]) . “<br />”;

//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));

$strHeader = “”;
$strHeader .= “From: “.$_POST[“nome”].”<“.$_POST[“email”].”>nReply-To: “.$_POST[“email”].””;

$strHeader .= “MIME-Version: 1.0n”;
$strHeader .= “Content-Type: multipart/mixed; boundary=””.$strSid.””nn”;
$strHeader .= “This is a multi-part message in MIME format.n”;

$strHeader .= “–“.$strSid.”n”;
$strHeader .= “Content-type: text/html; charset=utf-8n”;
$strHeader .= “Content-Transfer-Encoding: 7bitnn”;
$strHeader .= $strMessage.”nn”;

//*** Attachment ***//
if($_FILES[“fileAttach”][“name”] != “”)
{
$strFilesName = $_FILES[“fileAttach”][“name”];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES[“fileAttach”][“tmp_name”])));
$strHeader .= “–“.$strSid.”n”;
$strHeader .= “Content-Type: application/octet-stream; name=””.$strFilesName.””n”;
$strHeader .= “Content-Transfer-Encoding: base64n”;
$strHeader .= “Content-Disposition: attachment; filename=””.$strFilesName.””nn”;
$strHeader .= $strContent.”nn”;
}

$flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error //

if($flgSend)
{
echo “Envio do e-mail completado!”;
header(“Refresh: 2;http://www.seusite.com.br/contato.html”);
}
else
{
echo “Não foi possível enviar a mensagem.”;
header(“Refresh: 2;http://www.seusite.com.br/contato.html”);
}
?>
</body>
</html>
<!— This file download from www.shotdev.com –>

Este código funciona perfeitamente em servidor Linux. Caso apresente erro no Windows, tente trocar as barra de caminho de arquivo e de url para essa: “”

PHP: Enviando e-mail com anexo
Tagged on:             
%d blogueiros gostam disto: