#  > Desenvolvimento >  > Linguagens de Programação >  >  Como enviar arquivo anexo php

## nikollas

Caros, estou precisando usar um scrip onde possa enviar um arquivo em anexo quando o usuario digitar seus dados e anexa o arquivo, veja abaixo o scrip que achei na net.


<? 
/* 
sendmail.php3 
This was developed by Alexander Rafael Benatti 
03/2000, UIN - 59862221 
*/ 
$mailheaders = "From: $from\\n\"; 
$mailheaders .= \"Reply-To: $from\\n\"; 
$mailheaders .= \"Cc: $cc\\n\"; 
$mailheaders .= \"Bcc: $bcc\\n\"; 
$mailheaders .= \"X-Mailer: Script para enviar arquivo atachado, desenvolvido por Alexander Benatti\\n\"; 
$msg_body = stripslashes($body); 
if ($attach != \"none\") 
{ 
$file = fopen($attach, \"r\"); 
$contents = fread($file, $attach_size); 
$encoded_attach = chunk_split(base64_encode($contents)); 
fclose($file); 

$mailheaders .= \"MIME-version: 1.0\\n\"; 
$mailheaders .= \"Content-type: multipart/mixed; \"; 
$mailheaders .= \"boundary=\\\"Message-Boundary\\\"\\n\"; 
$mailheaders .= \"Content-transfer-encoding: 7BIT\\n\"; 
$mailheaders .= \"X-attachments: $attach_name\"; 
$body_top = \"--Message-Boundary\\n\"; 
$body_top .= \"Content-type: text/plain; charset=US-ASCII\\n\"; 
$body_top .= \"Content-transfer-encoding: 7BIT\\n\"; 
$body_top .= \"Content-description: Mail message body\\n\\n\"; 
$msg_body = $body_top . $msg_body; 
$msg_body .= \"\\n\\n--Message-Boundary\\n\"; 
$msg_body .= \"Content-type: $attach_type; name=\\\"$attach_name\\\"\\n\"; 
$msg_body .= \"Content-Transfer-Encoding: BASE64\\n\"; 
$msg_body .= \"Content-disposition: attachment; filename=\\\"$attach_name\\\"\\n\\n\"; 
$msg_body .= \"$encoded_attach\\n\"; 
$msg_body .= \"--Message-Boundary--\\n\"; 
} 
mail($to, stripslashes($subject), $msg_body, $mailheaders); 
?> 

O problema que estou achando é quais as variaveis tenho que usar no codigo da pagina e onde colocar este script para funcinar.

Agradeço desde já....

[]´s

----------


## nikollas

Achei na net este scrip que envia e-mail com anexo e funcionou 100%.

<html> 
<head> 
<title>.:: E-mail com arquivo ataxado ::.</title> 
</head> 
<style> 
.input {font-family: Verdana; font-size: 10px; color:#00000; background:#FFFFFF; border-right:1px solid #000000; border-left:1px solid #000000; border-top:1px solid #000000; border-bottom:1px solid #000000;} 
table {font-family: verdana; font-size: 11px; color: #000000; } 
.titulo {font-family: verdana; font-size: 13px; color: #FB3832; } 
a {text-decoration:none; font-family: verdana; font-size: 11px; color: #000099; } 
a:hover {text-decoration:underline; font-family: verdana; font-size: 11px; color: #000099; } 
</style> 
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" oncontextmenu="return false" onselectstart="return false"> 
<table cellpadding="0" cellspacing="0" border="0">
<? 
if($aux!="1") 
{ 
?>
<form action="envia_mail.php" method="post" enctype="multipart/form-data">
<input type="Hidden" name="aux" value="1">
<tr> 
<td height="18" colspan="2" valign="top"><div align="center"></div></td>
</tr>
<tr> 
<td width="66" valign="top">Nome:</td>
<td valign="top"><input type="Text" name="nome" size="70" class="input"></td>
</tr>
<tr> 
<td valign="top">E-mail:</td>
<td valign="top"><input type="Text" name="mail_sender" size="30" class="input"></td>
</tr>
<tr> 
<td valign="top">Assunto:</td>
<td valign="top"><input type="Text" name="assunto" size="30" class="input"></td>
</tr>
<tr> 
<td valign="top">Mensagem:</td>
<td valign="top"><textarea name="msg" class="input" cols="70" rows="10"></textarea></td>
</tr>
<tr> 
<td valign="top">Arquivo:</td>
<td valign="top"><input type="File" name="arquivo" class="input" size="30"></td>
</tr>
<tr> 
<td valign="top" colspan="2" align="center"><input type="Submit" value="Enviar" class="input"></td>
</tr>
</form>
<? 
} 
else 
{ 
$corpo = "Nome:$_POST[nome]<br>E-mail: $_POST[mail_sender]<br>Mensagem: $_POST[msg]"; 
$bound = "XYZ-" . date("dmYis") . "-ZYX"; 
if (($fp = fopen($_FILES['arquivo']['tmp_name'],"rb"))) 
{ 
$anexo = fread($fp,filesize($_FILES['arquivo']['tmp_name'])); 
$anexo = base64_encode($anexo); 
fclose($fp); 
$anexo = chunk_split($anexo); 
} 
if ($anexo) 
{ 
$mensagem = "--$bound\nContent-type: text/html\nContent-Transfer-Encoding: 7bit\n\n$corpo\n\n" 
. "--$bound\nContent-type: $_FILES[arquivo][type]\nContent-Disposition: attachment; filename=" . $_FILES['arquivo']['name'] . "\nContent-Transfer-Encoding: base64\n\n$anexo\n" 
. "--$bound\r\n"; 
mail("[email protected]",$assunto,$mensagem,"From: $_POST[mail_sender]\nMIME-Version: 1.0\nContent-type: multipart/mixed; boundary=\"$bound\""); 
print("Enviado com Sucesso! anexo"); 
print($fp);
} 
else 
{ 
mail("[email protected]",$assunto,$corpo,"From: $_POST[mail_sender]\nContent-type: text/html"); 
print("Enviado com Sucesso! sem anexo"); 
} 
} 
?>
</table> 
</body> 
</html>

----------

