langsung aja yah, tahap awal kita buat form untuk mengirim filenya. Kita fokuskan pada form input filenya saja, jika ingin menambahkan input text pada form silakan saja di modifikasi sendiri.
buka text editor anda, lalu buat contoh simple form sebagai berikut
1 | < form action = "sending.php" enctype = "multipart/form-data" method = "post" > |
2 | < input name = "attachment1" type = "file" > |
3 | < input name = "attachment2" type = "file" > |
4 | < input value = "submit" type = "submit" > |
jika ingin menambahkan form input file tinggal buat :
1 | < input name = "attachment3" type = "file" > |
sesuai keperluan saja, sejauh ini saya coba 3 file attachment ok ok aja..
kemudian save terserah mau dikasih nama apa aja, form_email.php juga ok.
lalu selanjutnya kita buat file php untuk mengesekusinya ( syeet dah bahasanya, kaya apa aja
)
ketik script berikut di text editor baru, kemudian save deangan nama file sending.php
03 | $to = "email_penerima@blabla.com" |
04 | $subject = "[ Email Attachment]" ; |
06 | $from = stripslashes ( 'NAMA PENGIRIM' ). "<" . stripslashes ( 'email_pngrm@blabla.com' ). ">" ; |
08 | $mime_boundary = "==Multipart_Boundary_x" .md5(mt_rand()). "x" ; |
10 | $headers = "From: $from\r\n" . |
11 | "MIME-Version: 1.0\r\n" . |
12 | "Content-Type: multipart/mixed;\r\n" . |
13 | " boundary=\"{$mime_boundary}\"" ; |
16 | $message = "Tulis isi email anda disini" ; |
18 | $message = "This is a multi-part message in MIME format.\n\n" . |
19 | "--{$mime_boundary}\n" . |
20 | "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . |
21 | "Content-Transfer-Encoding: 7bit\n\n" . |
25 | foreach ( $_FILES as $userfile ){ |
26 | $tmp_name = $userfile [ 'tmp_name' ]; |
27 | $type = $userfile [ 'type' ]; |
28 | $name = $userfile [ 'name' ]; |
29 | $size = $userfile [ 'size' ]; |
31 | if ( file_exists ( $tmp_name )){ |
32 | if ( is_uploaded_file ( $tmp_name )){ |
34 | $file = fopen ( $tmp_name , 'rb' ); |
36 | $data = fread ( $file , filesize ( $tmp_name )); |
40 | $data = chunk_split ( base64_encode ( $data )); |
42 | $message .= "--{$mime_boundary}\n" . |
43 | "Content-Type: {$type};\n" . |
44 | " name=\"{$name}\"\n" . |
45 | "Content-Disposition: attachment;\n" . |
46 | " filename=\"{$fileatt_name}\"\n" . |
47 | "Content-Transfer-Encoding: base64\n\n" . |
52 | $message .= "--{$mime_boundary}--\n" ; |
54 | if (@mail( $to , $subject , $message , $headers )) |
55 | echo "Document Telah Berhasil Dikirim." ; |
56 | else echo "Gagal Mengirim" ; |
saatnya mencoba, upload terlebih dahulu, karna kalau dilocalhost ga jalan.
Semoga berhasil….