Skip to content

Instantly share code, notes, and snippets.

@danilowm
Created March 8, 2012 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save danilowm/1997820 to your computer and use it in GitHub Desktop.
Save danilowm/1997820 to your computer and use it in GitHub Desktop.
Enviar email com Anexo em PHP
<?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";
// email
$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);
?>
<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>
@danilowm
Copy link
Author

Acho que isso deve resolver:

https://3v4l.org/dB6iZ

@Mauriciofelic21
Copy link

Mauriciofelic21 commented Aug 31, 2021 via email

@Mauriciofelic21
Copy link

Mauriciofelic21 commented Aug 31, 2021 via email

@Mauriciofelic21
Copy link

Mauriciofelic21 commented Aug 31, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment