sEnte Posted January 3, 2013 at 01:34 AM Report #489334 Posted January 3, 2013 at 01:34 AM (edited) dá para enviar anexos? Edited January 6, 2013 at 09:35 PM by sEnte "If It Ain't Broke, Break it and build something Cooler!" Unknown
tiagotavares Posted January 3, 2013 at 07:01 AM Report #489339 Posted January 3, 2013 at 07:01 AM Bom dia, Sim podes enviar anexos em PHP (deixo em baixo um link para um exemplo) Enviar anexos Depois de veres como se implementa acho que devias de usar uma classe já feita que te vai poupar tempo PHPMailer Tiago Tavares
sEnte Posted January 6, 2013 at 10:17 PM Author Report #489954 Posted January 6, 2013 at 10:17 PM Descobri uma maneira mais simples mas agota tenho dificuldade em lidar com o ficheiro "If It Ain't Broke, Break it and build something Cooler!" Unknown
sEnte Posted January 8, 2013 at 04:05 PM Author Report #490150 Posted January 8, 2013 at 04:05 PM (edited) Andei a mexer nisto e já consegui alguma coisa, mas mesmo assim ainda continuo com problemas... <?php if(empty($_POST) === false){ $errors = array(); $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $file = $_FILES['filename']; if(empty($name) === true || empty($email) === true || empty($message) === true){ $errors[] = 'Name, email and message are required!'; } else { if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){ $errors[] ='Not a valid email'; } if(ctype_alpha($name) === false){ $errors[] ='Name must only contain letters'; } } if(empty($errors) === true){ $boundary = md5(rand()); $headers = array( 'MIME-Version: 1.0', "Content-type: multipart/mixed; boundary=\"{$boundary}\"", "From: {$email}" ); $message = array( "--{$boundary}", 'Content-type: text/html', 'Content-Transfer-Encoding: 7bit', '', chunk_split($message), "--{$boundary}", "Content-type: {$file['type']}; name=\"{$file['filename']}\"", "Content-Disposition: attachment; filename=\"{$file['filename']}\"", 'Content-Transfer-Encoding: base64', '', chunk_split(base64_encode(file_get_contents($file['tmp_name']))), "--{$boundary}--" ); //send email mail($email, 'Contact form', implode("\r\n", $message), implode("\r\n", $headers)); //redirect user header('Location: index.php?sent'); exit(); } } ?> manda o fichiero mas o nome não vai como deve ser (se for por exemplo ficheiros jpg ou pdf vai mas se mandar por exemplo php já não vai) Queria pegar o type e size do ficheiro para mandar, e depois mais tarde fazer uma verificação de tipo de ficheiro e tamanho. Se quiserem experimentar.... Vão receber o mail no vosso mail como podem confirmar pelo código http://exp.net84.net/testcontact.php Edited January 8, 2013 at 06:34 PM by brunoais código retirado do pastebin "If It Ain't Broke, Break it and build something Cooler!" Unknown
brunoais Posted January 8, 2013 at 06:36 PM Report #490165 Posted January 8, 2013 at 06:36 PM Parece-me que o servidor aonde tens isso não permite o envio de ficheiros com a extensão php... Já tentaste ver os logs? "[Os jovens da actual geração]não lêem porque não envolve um telecomando que dê para mirar e atirar, não falam porque a trapalhice é rainha e o calão é rei" autor: thoga31 Life is a genetically transmitted disease, induced by sex, with death rate of 100%.
sEnte Posted January 8, 2013 at 07:00 PM Author Report #490173 Posted January 8, 2013 at 07:00 PM logs? não. Estou a mandar para o gmail e o servidor é o 000wenhost O que acontece é que como disse se mandar jpg ou pdf funciona mas se mandar um ficheiro php por exemplo o nome nem seqer fica como deve ser. "If It Ain't Broke, Break it and build something Cooler!" Unknown
brunoais Posted January 8, 2013 at 07:37 PM Report #490187 Posted January 8, 2013 at 07:37 PM Então vai ver os logs na tua conta no "000wenhost". É lá que está a razão pq é que o ficheiro não está a ser enviado. "[Os jovens da actual geração]não lêem porque não envolve um telecomando que dê para mirar e atirar, não falam porque a trapalhice é rainha e o calão é rei" autor: thoga31 Life is a genetically transmitted disease, induced by sex, with death rate of 100%.
sEnte Posted January 8, 2013 at 07:46 PM Author Report #490189 Posted January 8, 2013 at 07:46 PM O ficheiro envia, eu é que não consigo fazer com que envie o nome como está no computador e a respectiva extensão. "If It Ain't Broke, Break it and build something Cooler!" Unknown
brunoais Posted January 8, 2013 at 07:55 PM Report #490195 Posted January 8, 2013 at 07:55 PM Faz echo do nome no script q está no servidor e confirma o que aparece. Faz tb echo do e-mail completo que envias. Escreve aqui o output entro de tags . Assim já devo conseguir ajudar-te. "[Os jovens da actual geração]não lêem porque não envolve um telecomando que dê para mirar e atirar, não falam porque a trapalhice é rainha e o calão é rei" autor: thoga31 Life is a genetically transmitted disease, induced by sex, with death rate of 100%.
sEnte Posted January 8, 2013 at 08:00 PM Author Report #490198 Posted January 8, 2013 at 08:00 PM (edited) Não estou a perceber. quando faço echo '$_FILES'; apenas aparece isto Array do mail pralem do nome que não aparece em lado nenhum, meto o mail que é para onde vai o mail e o camplo $message é uma cena muito random. carego num monte de letras e pronto. Posso estar enganado mas deve ser alguma coisa aqui que não está a funcionar. $message = array( "--{$boundary}", 'Content-type: text/html', 'Content-Transfer-Encoding: 7bit', '', chunk_split($message), "--{$boundary}", "Content-type: {$file['type']}; name=\"{$file['filename']}\"", "Content-Disposition: attachment; filename=\"{$file['filename']}\"", 'Content-Transfer-Encoding: base64', '', chunk_split(base64_encode(file_get_contents($file['tmp_name']))), "--{$boundary}--" ); Edited January 8, 2013 at 08:03 PM by sEnte "If It Ain't Broke, Break it and build something Cooler!" Unknown
brunoais Posted January 8, 2013 at 08:07 PM Report #490199 Posted January 8, 2013 at 08:07 PM Nem era suposto fazeres echo de um array. Só era suposto fazeres do nome do ficheiro. É normal no $message aparecer um monte de "jiberish" mas realmente sabes o que interessa nesse "jibberish"? De qq modo faz como te pedi. Faz echo do resultado de: implode("\r\n", $message) "[Os jovens da actual geração]não lêem porque não envolve um telecomando que dê para mirar e atirar, não falam porque a trapalhice é rainha e o calão é rei" autor: thoga31 Life is a genetically transmitted disease, induced by sex, with death rate of 100%.
sEnte Posted January 8, 2013 at 08:12 PM Author Report #490202 Posted January 8, 2013 at 08:12 PM (edited) o que tentei fazer diz apenas Array implode(" ", Array) tentei fazer também echo "$_FILES['filename']"; e aparece Array Edited January 8, 2013 at 08:13 PM by sEnte "If It Ain't Broke, Break it and build something Cooler!" Unknown
brunoais Posted January 8, 2013 at 08:19 PM Report #490204 Posted January 8, 2013 at 08:19 PM Lê o que escrevi com atenção. Não estás a fazer o que pedi "[Os jovens da actual geração]não lêem porque não envolve um telecomando que dê para mirar e atirar, não falam porque a trapalhice é rainha e o calão é rei" autor: thoga31 Life is a genetically transmitted disease, induced by sex, with death rate of 100%.
sEnte Posted January 8, 2013 at 08:21 PM Author Report #490205 Posted January 8, 2013 at 08:21 PM fiz echo 'implode("\r\n", $message)'; "If It Ain't Broke, Break it and build something Cooler!" Unknown
brunoais Posted January 8, 2013 at 08:24 PM Report #490206 Posted January 8, 2013 at 08:24 PM Procura por: mail($email, 'Contact form', implode("\r\n", $message), implode("\r\n", $headers)); Escreve depois: echo implode("\r\n", $message); echo "\n\n\n\n"; echo $_FILES['filename']['name']; eu quero esse output. "[Os jovens da actual geração]não lêem porque não envolve um telecomando que dê para mirar e atirar, não falam porque a trapalhice é rainha e o calão é rei" autor: thoga31 Life is a genetically transmitted disease, induced by sex, with death rate of 100%.
sEnte Posted January 8, 2013 at 08:31 PM Author Report #490210 Posted January 8, 2013 at 08:31 PM Ai não queres não lol tem aqui o equivalente a umas 10 ou 15 páginas de texto maluco, caracteres sem o menor nexo "If It Ain't Broke, Break it and build something Cooler!" Unknown
sEnte Posted January 8, 2013 at 10:57 PM Author Report #490236 Posted January 8, 2013 at 10:57 PM aqui fica a solução do amigo brunoais <?php error_reporting(-1); if(empty($_POST) === false){ $errors = array(); $name = $_POST['name']; $email = $_POST['email']; $file = $_FILES['filename']; if(empty($name) === true || empty($email) === true || empty($_POST['message']) === true){ $errors[] = 'Name, email and message are required!'; } else { if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){ $errors[] ='Not a valid email'; } if(ctype_alpha($name) === false){ $errors[] ='Name must only contain letters'; } } $message; if(empty($errors) === true){ if($_FILES['filename']['error'] == UPLOAD_ERR_OK){ $boundary = '-----' . md5(rand()) . '---'; $headers = array( 'MIME-Version: 1.0', "Content-type: multipart/mixed; boundary=\"{$boundary}\"", "From: {$email}" ); $message = array( "--{$boundary}", 'Content-type: text/html', 'Content-Transfer-Encoding: 7bit', '', chunk_split($_POST['message']), "--{$boundary}", "Content-type: {$file['type']}; name=\"{$file['name']}\"", "Content-Disposition: attachment; filename=\"{$file['name']}\"", 'Content-Transfer-Encoding: base64', '', chunk_split(base64_encode(file_get_contents($file['tmp_name']))), "--{$boundary}--" ); $message = implode("\r\n", $message); } else { $headers = array( "From: {$email}" ); $message = &$_POST['message']; } //send email var_dump(mail($email, 'Contact form', $message, implode("\r\n", $headers))); echo $_FILES['filename']['name']; /*//redirect user header('Location: index.php?sent'); exit();*/ } } ?> <!doctype html> <html> <head> <title>A contact form</title> </head> <body> <?php if(isset($_GET['sent']) === true){ echo '<p>Thanks for contacting us</p>'; } else { if(empty($errors) === false){ echo '<ul>'; foreach($errors as $error){ echo '<li>', $error ,'</li>'; } echo '</ul>'; } ?> <form action="" method="post" enctype="multipart/form-data"> <p> <label for="name">Name:</label><br /> <input type="text" name="name" id="name" <?php if(isset($_POST['name']) === true){ echo 'value="', strip_tags($_POST['name']), '"';} ?>> </p> <p> <label for="email">Email:</label><br /> <input type="text" name="email" id="email" <?php if(isset($_POST['email']) === true){ echo 'value="', strip_tags($_POST['email']), '"';} ?>> </p> <p> <label for="message">Message:</label><br /> <textarea name="message" id="message"><?php if(isset($_POST['message']) === true){ echo strip_tags($_POST['message']); } ?></textarea> </p> <p> List of files allowed: .pdf, .odt, .doc(x), xls(x), ppt(x), xps, xml </p> <p> <input type="file" name="filename"> </p> <p> <input type="submit" value="submit"> </p> <?php } ?> </form> </body> </html> "If It Ain't Broke, Break it and build something Cooler!" Unknown
brunoais Posted January 9, 2013 at 09:12 AM Report #490278 Posted January 9, 2013 at 09:12 AM Bah... É preciso indicar qual era o problema, não? De qq modo, o problema estava relacionado com o mau uso do array $_FILES que provocava um output errado. Quando vi o código que aparece no 3º post não reparei, por causa dos alias, que o $_FILES estava a ser mal usado, então o e-mail estava a ser enviado sem o nome para o servidor de e-mail. Era o servidor de e-mail que estava a adivinhar o nome para alguns tipos de ficheiro. Esta aqui é uma boa solução para se pegar como alternativa a usar "coisas" como o PHPMailer. Pelo menos fica-se a perceber melhor como é que o e-mail funciona de mais baixo nível. "[Os jovens da actual geração]não lêem porque não envolve um telecomando que dê para mirar e atirar, não falam porque a trapalhice é rainha e o calão é rei" autor: thoga31 Life is a genetically transmitted disease, induced by sex, with death rate of 100%.
sEnte Posted January 9, 2013 at 10:39 AM Author Report #490290 Posted January 9, 2013 at 10:39 AM (edited) PHPMailer? nunca usei nada disso. ahah esqueci-me de indicar o problema xD De qualquer forma ainda fiz uma limpeza a esse código, porque tinhas coisas que não interessavam tal como o error_reporting, os var_dump e um echo que aí estava. Agora o mais importante será segurar os inputs para não haver SQL Injections nem nada do tipo. sei que posso usar mysql_real_escape string() htmlentities() trim() strip_tags() no entanto nunca percebi muito bem se o mysql_real_escape string() é só para quando há SQL envolvido ou não... A seguir vai ser feita uma verificação de extensões, e pode-se enviar apenas um determinado tipo de extensãoes tal como .pdf, .odt, .doc(x), xls(x), ppt(x), xps, xml Outra coisa que me lembrei de fazer por exemplo Se as extensões forem aquelas manda normalmente, se for outras chama a função ZipArchive() mas sinceramente aindanão sei bem Edited January 9, 2013 at 02:24 PM by sEnte "If It Ain't Broke, Break it and build something Cooler!" Unknown
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now