Múltiplos anexos em email
Estes são alguns pontos do que eu preciso - Eu tenho um sistema que em cada registro tem um ou mais anexos. - Os nomes destes anexos estão guardados no banco de dados, e os arquivos ficam na pasta anexos/ no servidor. - O usuário manda este registro via email e os anexos devem ir juntos. - Eu estou usando a função do PHP mail(). Objetivo: Enviar um email com um ou mais anexos Problema: Este código só está enviando um anexo, que é o último anexo que a query pega. OBS.: Eu não entendo de boundary e de anexos por email, eu peguei esse código na internet, mas eu fiz algumas alterações nele, porque no código original só mandava um anexo.
$assunto = "Registros". $boundary = "XYZ-".md5(date("dmYis"))."-ZYX". // cabeçalho do email$cabecalho = "MIME-Version: 1.0". PHP_EOL. $cabecalho.= "Content-Type: multipart/mixed. ". $cabecalho.= "boundary=". $boundary. PHP_EOL. $cabecalho.= "$boundary". PHP_EOL. $msg = "". // Anexos
$sql = " SELECT A.LOCAL, A.ARQUIVO FROM TB_ANEXOS AS A LEFT JOIN TB_REGISTROS AS R ON (A.ID_REGISTRO = R.ID) WHERE R.ID = $ID ". $resultado = mysqli_query($link, $sql). if ($resultado){ while ($dados = mysqli_fetch_array($resultado, MYSQLI_ASSOC)){ $LOCAL = $dados['LOCAL']. $ARQUIVO_NAME = $dados['ARQUIVO']. $path = 'anexos/'.$LOCAL. $fileType = mime_content_type( $path ). // Pegando o conteúdo do arquivo $fp = fopen( $path, "rb" ). // abre o arquivo enviado $anexo = fread( $fp, filesize( $path ) ). // calcula o tamanho $anexo = chunk_split(base64_encode( $anexo )). // codifica o anexo em base 64 fclose( $fp ). // fecha o arquivo $msg.= "Content-Type: ". $fileType.". name=\"". $ARQUIVO_NAME. "\"". PHP_EOL. $msg.= "Content-Transfer-Encoding: base64". PHP_EOL. $msg.= "Content-Disposition: attachment. filename=\"". $ARQUIVO_NAME. "\"". PHP_EOL. $msg.= "$anexo". PHP_EOL. $msg.= "--$boundary". PHP_EOL. }
} $msg.= "--$boundary". PHP_EOL. $msg.= "Content-Type: text/html. charset='utf-8'". PHP_EOL. $msg.= "
<!doctype html>
<html lang='pt-br'>
<head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0'> <meta http-equiv='X-UA-Compatible' content='ie=edge'> <title>$assunto</title> <style> h2, b, legend { color: #2d2d2d. } </style>
</head>
<body> <p>CORPO DO EMAIL</p>
</body>
</html>
". $msg.= "--$boundary". PHP_EOL. if(mail($para, $assunto, $msg, $cabecalho)) { echo "<p>Email enviado com sucesso</p>". } else {
echo '<p style="color: #f00">Erro!</p>'. }Discussão (0)
Carregando comentários...