Created
March 8, 2012 01:23
-
-
Save danilowm/1997820 to your computer and use it in GitHub Desktop.
Enviar email com Anexo em PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$nome = $_POST['nome']; | |
$arquivo = $_FILES["arquivo"]; | |
// Para quem vai ser enviado o email | |
$para = "seu[@]email.com.br"; | |
$boundary = "XYZ-".date("dmYis")."-ZYX"; | |
$fp = fopen($arquivo["tmp_name"], "rb"); // abre o arquivo enviado | |
$anexo = fread($fp, filesize($arquivo["tmp_name"])); // calcula o tamanho | |
$anexo = base64_encode($anexo); // codifica o anexo em base 64 | |
fclose($fp); // fecha o arquivo | |
// cabeçalho do email | |
$headers = "MIME-Version: 1.0\n"; | |
$headers .= "Content-Type: multipart/mixed; "; | |
$headers .= "boundary="$boundary"\r\n"; | |
$headers .= "$boundary\n"; | |
$mensagem = "--$boundary\n"; | |
$mensagem .= "Content-Type: text/html; charset='utf-8'\n"; | |
$mensagem .= "<strong>Nome: </strong> $nome \r\n"; | |
$mensagem .= "--$boundary \n"; | |
// anexo | |
$mensagem .= "Content-Type: ".$arquivo["type"]."; name="".$arquivo['name']."" \n"; | |
$mensagem .= "Content-Transfer-Encoding: base64 \n"; | |
$mensagem .= "Content-Disposition: attachment; filename="".$arquivo['name']."" \r\n"; | |
$mensagem .= "$anexo \n"; | |
$mensagem .= "--$boundary \n"; | |
// enviar o email | |
mail($para, $assunto, $mensagem, $headers); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form action="enviar_email.php" method="post" enctype="multipart/form-data"> | |
<label>Nome:</label><br/> | |
<input type="text" name="nome" /><br/><br/> | |
<label>Arquivo:</label><br/> | |
<input type="file" name="arquivo" /><br/><br/> | |
<input type="submit" value="Enviar" /> | |
</form> |
Mauriciofelic21
commented
Aug 31, 2021
via email
vou dar uma olhada, muito obrigado
Em ter., 31 de ago. de 2021 às 14:34, Danilo Iannone <
***@***.***> escreveu:
… ***@***.**** commented on this gist.
------------------------------
Acho que isso deve resolver:
https://3v4l.org/dB6iZ
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/1997820#gistcomment-3878255>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AO36GOPDBQVNEVP7O5UMDGLT7UHD3ANCNFSM4KJNIHGA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Infelizmente não deu irmão apenas o anexo é enviado 😔 . Mas obrigado .
Em ter., 31 de ago. de 2021 13:34, Danilo Iannone ***@***.***>
escreveu:
… ***@***.**** commented on this gist.
------------------------------
Acho que isso deve resolver:
https://3v4l.org/dB6iZ
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/1997820#gistcomment-3878255>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AO36GOPDBQVNEVP7O5UMDGLT7UHD3ANCNFSM4KJNIHGA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Amigo, consegui aqui, vou te passar o código às vezes você precisar algum
dia, ou atualize no seu site.. abraço!
<?php
$emaildestino = $_POST['subcategoria'];
$nome=$_POST['nome'];
$sobrenome=$_POST['sobrenome'];
$email=$_POST['email'];
$telefone=$_POST['telefone'];
$arquivo = $_FILES["arquivo"];
$descricao=$_POST['mensagem'];
$assunto = "E-mail do Site;
/* Destinatário e remetente - EDITAR SOMENTE ESTE BLOCO DO CÓDIGO */
$to = $emaildestino;
$remetente = $email; // Deve ser um email válido do domínio
/* Cabeçalho da mensagem */
$boundary = "XYZ-" . date("dmYis") . "-ZYX";
$headers = "MIME-Version: 1.0\n";
$headers.= "From: $remetente\n";
$headers.= "Content-type: multipart/mixed; boundary=\"$boundary\"\r\n";
$headers.= "$boundary\n";
/* Layout da mensagem */
$corpo_mensagem = "
<br>Formulário via site
<br>--------------------------------------------<br>
<br><strong>Nome:</strong> $nome
<br><strong>Sobrenome:</strong> $sobrenome
<br><strong>E-mail:</strong> $email
<br><strong>Telefone:</strong> $telefone
<br><strong>Descrição :</strong> $descricao
<br>--------------------------------------------
";
/* Função que codifica o anexo para poder ser enviado na mensagem */
if(file_exists($arquivo["tmp_name"]) and !empty($arquivo)){
$fp = fopen($_FILES["arquivo"]["tmp_name"],"rb"); // Abri o arquivo
enviado.
$anexo = fread($fp,filesize($_FILES["arquivo"]["tmp_name"])); // Le o
arquivo aberto na linha anterior
$anexo = base64_encode($anexo); // Codifica os dados com MIME para o
e-mail
fclose($fp); // Fecha o arquivo aberto anteriormente
$anexo = chunk_split($anexo); // Divide a variável do arquivo em
pequenos pedaços para poder enviar
$mensagem = "--$boundary\n"; // Nas linhas abaixo possuem os parâmetros
de formatação e codificação, juntamente com a inclusão do arquivo anexado
no corpo da mensagem
$mensagem.= "Content-Transfer-Encoding: 8bits\n";
$mensagem.= "Content-Type: text/html; charset=\"utf-8\"\n\n";
$mensagem.= "$corpo_mensagem\n";
$mensagem.= "--$boundary\n";
$mensagem.= "Content-Type: ".$arquivo["type"]."\n";
$mensagem.= "Content-Disposition: attachment;
filename=\"".$arquivo["name"]."\"\n";
$mensagem.= "Content-Transfer-Encoding: base64\n\n";
$mensagem.= "$anexo\n";
$mensagem.= "--$boundary--\r\n";
}
else // Caso não tenha anexo
{
$mensagem = "--$boundary\n";
$mensagem.= "Content-Transfer-Encoding: 8bits\n";
$mensagem.= "Content-Type: text/html; charset=\"utf-8\"\n\n";
$mensagem.= "$corpo_mensagem\n";
}
/* Função que envia a mensagem */
if(mail($to, $assunto, $mensagem, $headers))
{
echo "<br><br><center><b><font color='green'>Mensagem enviada com
sucesso!";
}
else
{
echo "<br><br><center><b><font color='red'>Ocorreu um erro ao enviar a
mensagem!";
}
?>
Em ter., 31 de ago. de 2021 às 14:05, Danilo Iannone <
***@***.***> escreveu:
… ***@***.**** commented on this gist.
------------------------------
@Mauriciofelic21 <https://github.com/Mauriciofelic21> beleza?
nesse exemplo não tem "nada escrito", somente Nome e Anexo
para enviar um corpo de mensagem, terá que passar os devidos parametros
Um novo campo no HTML para textarea
<label>Mensagem:</label><br/> <textarea name="texto"></textarea><br/><br/>
E no PHP passar o parametro Mensagem:
$texto = $_POST['texto'];
$mensagem .= "<strong>Mensagem: </strong> $texto \r\n";
sacou a ideia?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/1997820#gistcomment-3878210>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AO36GONCACI7CJPIZHQ6DVTT7UDUVANCNFSM4KJNIHGA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment