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>
@marcelosantos89
Copy link

Nome: $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); ?>

Fixed! :)

@nmoa77
Copy link

nmoa77 commented Mar 18, 2014

ola,
o ficheiro esta a vir com o tamanho errado, aparece 87 B em vez de KB, assim o windows não reconhece o ficheiro.
outra coisa, o strong do nome não está a aparecer, aparece com a tag html
obrigado

@jlbardella
Copy link

Ola boa noite a todos, executei o codigo conforme acima e me retornou um erro

[21-Jan-2020 04:26:38 UTC] PHP Parse error: syntax error, unexpected '$boundary' (T_VARIABLE) in /home1/metalu29/public_html/form/enviar_email.php on line 17

@johnnydemarch
Copy link

Ola boa noite a todos, executei o codigo conforme acima e me retornou um erro

[21-Jan-2020 04:26:38 UTC] PHP Parse error: syntax error, unexpected '$boundary' (T_VARIABLE) in /home1/metalu29/public_html/form/enviar_email.php on line 17

Como resolveu? Mesmo erro aqui...

@mmosczcertdox
Copy link

Precisa incluir as barras invertidas ( \ )antes do aspas ( " )

boundary="$boundary"\r\n

@Mauriciofelic21
Copy link

Fiz o envio perfeito, mais no email nao chega as informações escrita apenas o anexo... o que seria?

@danilowm
Copy link
Author

@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
`Mensagem:

<textarea name="texto"></textarea>

`

E no PHP passar o parametro Mensagem:
$texto = $_POST['texto'];
$mensagem .= "<strong>Mensagem: </strong> $texto \r\n";

sacou a ideia?

@Mauriciofelic21
Copy link

Mauriciofelic21 commented Aug 31, 2021 via email

@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