Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] mail z załącznikiem
Forum PHP.pl > Forum > Przedszkole
cornholio666
Witam,

znalazłem kod w necie:

  1. <form name="form1" method="post" action="sendmail.php" enctype="multipart/form-data">
  2. <table width="300" border="0" cellspacing="0" cellpadding="0">
  3. <tr><td>subject</td><td><input type="text" name="subject"></td></tr>
  4. <tr><td>replayto</td><td><input type="text" name="replayto"></td></tr>
  5. <tr><td>from</td><td><input type="text" name="from"></td></tr>
  6. <tr><td>sendto</td><td><input type="text" name="sendto"></td></tr>
  7. <tr><td>message</td><td><textarea name="message" wrap="VIRTUAL"></textarea></td></tr>
  8. <tr><td>filename</td><td><input type="file" name="filename"></td></tr>
  9. <tr><td colspan="2"><input type="submit" name="Submit" value="Submit"></td></tr>
  10. </form>



  1. <?php
  2.  
  3.  $replyto = $_POST['replyto'];
  4.  $subject = $_POST['subject'];
  5.  $from = $_POST['from'];
  6.  $sendto = $_POST['sendto'];
  7.  $message = $_POST['message'];
  8.  $filename_name = $_FILES['filename']['name'];
  9.  $filename_type = $_FILES['filename']['type'];
  10.  
  11. $boundary = "-->===_54654747_===<---->>4255==_";
  12.  
  13. // Zmiennych $subject i $sendto nie musimy w żaden sposób przerabiać.
  14.  
  15. // Teraz trzeba sformatować nagłówek.
  16.  
  17. $head = "From: $fromn";
  18. $head = $head . "Reply-To: $replyton";
  19. $head = $head . "X-Mailer: PHPn";
  20. $head = $head . "X-Sender: $fromn";
  21. $head = $head . "MIME-version: 1.0n";
  22. $head = $head . "Content-type: multipart/mixed; ";
  23. $head = $head . "boundary="$boundary"n";
  24. $head = $head . "Content-transfer-encoding: 7BITn";
  25. $head = $head . "X-attachments: $filename_name;nn";
  26.  
  27. // Jeszcze tylko treść wiadomości i możemy słać pocztę 
  28.  
  29. $mesg = "--" . $boundary . "n"; //pamiętamy dwa minusy na początku
  30. $mesg = $mesg . "Content-Type: text/plain; charset="us-ascii"nn";
  31. $mesg = $mesg . $message . "n";
  32. $mesg = $mesg . "--" . $boundary . "n"; //pamiętamy dwa minusy na początku
  33.  $mesg = $mesg . "Content-type: " . $filename_type . "; name="$filename_name";n";
  34. $mesg = $mesg . "Content-Transfer-Encoding: base64n";
  35. $mesg = $mesg . "Content-disposition: attachment; filename= "$filename_name"nn";
  36. $mesg = $mesg . fileopenanddecode($filename_name) . "n";  //czytamy plik i go kodujemy
  37. $mesg = $mesg . "--" . $boundary . "-- n"; //pamiętamy dwa minusy na początku i na końcu
  38.  
  39.  
  40.  
  41. //funkcja odczytująca plik i kodująca go d o formatu base64 zgodnie z RFC 2045
  42. function fileopenanddecode($file) {
  43. if (is_readable($file)) {
  44.  $fd = fopen($file, "r");
  45.  $plik = fread($fd, filesize($file));
  46.  $encoded = chunk_split(base64_encode($plik));
  47.  fclose($fd);
  48. }
  49. return $encoded;
  50. }
  51.  
  52. // i to wszystko teraz tylko 
  53. mail($sendto, $subject, $mesg, $head); 
  54. //i poszło
  55. ?>


Załącznik dochodzi ale ma 0 kb. Wie ktoś dlaczego?

Czy plik muszę wrzucić pierwsze na serwer?
xian.pl
Tak wygląda na to, że plik musi być w tym samym katalogu co skrypt.
Można to łatwo poprawić jeżeli wczytamy nasz plik o tymczasowej nazwie.

Manual w języku ang. : http://php.net.pl/manual/pl/features.file-upload.php
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.