[Ask] Send File Attachment PHP

Discussion in 'Desain Web dan Programming' started by xpreborn, 17 Oct 2016.

Thread Status:
Not open for further replies.
  1. xpreborn

    xpreborn Poster 2.0

    Messages:
    183
    Likes Received:
    4
    Trophy Points:
    18
    Selamat malam Om,
    Izin bertanya lagi ,
    Ceritanya gini,
    Saya punya satu form yg mana ada field upload file potongannya :
    Code:
            <div class="form-group">
            <label class="col-sm-2 control-label">Upload Gambar</label>
            <div class="col-sm-8 input-group">
            <input name="gambar" type="file" id="gambar" accept="image/*" multiple>
            </div>
            </div>
    Dan potongan actionnya bagian upload gambar yaitu :
    Code:
                    $imgFile = $_FILES['gambar']['name'];
                    $tmp_dir = $_FILES['gambar']['tmp_name'];
                    $imgSize = $_FILES['gambar']['size'];
                    if(empty($imgFile)){
                            $pesan = "Silahkan pilih gambar.";
                    }
                    else
                    {
                            $folder = 'img/';
                            $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
                            $valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
                            $gambar1 = rand(1000,1000000).".".$imgExt;
    
                            // allow valid image file formats
                            if(in_array($imgExt, $valid_extensions)){
                                    // Check file size '5MB'
                                    if($imgSize < 5000000)                          {
                                            move_uploaded_file($tmp_dir,$folder.$gambar1);
                                    }
                                    else{
                                            $pesan = "Sorry, file kebesaran.";
                                    }
                            }
                            else{
                                    $pesan = "Sorry, Hanya JPG, JPEG, PNG & GIF yang diizinkan.";
                            }
                    }
    Nah setelah diproses sudah berhasil upload file gambar di folder img,
    Saya, ingin ketika selesai diupload ke folder img, maka bersamaan gambar tersebut langsung dikirim ke email jg dalam bentuk attachment,
    Mohon bantuannya Om, agak kurang paham saya bagaimana caranya agar bisa terkirim ke email jg dalam bentuk attachment,

    Terimakasih banyak sebelumnya
     
  2. masiqbal

    masiqbal Expert 2.0

    Messages:
    863
    Likes Received:
    249
    Trophy Points:
    43
    xpreborn likes this.
  3. hendranata

    hendranata Hosting Guru

    Messages:
    1,097
    Likes Received:
    84
    Trophy Points:
    48
    Pake phpmailer aja via smtp
     
    xpreborn likes this.
  4. xpreborn

    xpreborn Poster 2.0

    Messages:
    183
    Likes Received:
    4
    Trophy Points:
    18
    Banyak jg yach om dan harus install,
    Kira2 jika hanya dgn perintah kirim email seperti biasa lalu diselipin "perintah" untuk bawa attachmentnya bisa ga yach Om ?

    Bs kasih contoh Om? kalo kirim email biasa sich bs, tp cara ikutkan attachmentnya ambil dari hasil upload td blm ada gambaran saya Om,

    Sebelumnya terimakasih banyak atas jawabannya
     
  5. Galaxy Hosting

    Galaxy Hosting Apprentice 1.0

    Messages:
    387
    Likes Received:
    57
    Trophy Points:
    28
    xpreborn likes this.
  6. xpreborn

    xpreborn Poster 2.0

    Messages:
    183
    Likes Received:
    4
    Trophy Points:
    18
    Selamat siang Om,
    Terimakasih banyak atas masukannya,
    Berikut full file 123.php yang merupakan action dari formnya :

    Code:
    <?php
    require('WriteHTML.php');
    $pdf=new PDF_HTML();
    $pdf->AliasNbPages();
    $pdf->SetAutoPageBreak(true, 15);
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',11);
    $pdf->WriteHTML('<para><h1>Percobaan</h1>');
    
    $namalengkap=$_POST['namalengkap'];
    
                    $imgFile = $_FILES['gambar']['name'];
                    $tmp_dir = $_FILES['gambar']['tmp_name'];
                    $imgSize = $_FILES['gambar']['size'];
                    if(empty($imgFile)){
                            $pesan = "Silahkan Pilih Gambar.";
                    }
                    else
                    {
                            $folder = 'img/';
                            $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
                            // valid image extensions
                            $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
                            // rename uploading image
                            $filename1 = rand(1000,1000000).".".$imgExt;
                            // allow valid image file formats
                            if(in_array($imgExt, $valid_extensions)){
                                    // Check file size '5MB'
                                    if($imgSize < 5000000)                          {
                                            move_uploaded_file($tmp_dir,$folder.$filename1);
                                    }
                                    else{
                                            $pesan = "Sorry, file anda kebesaran.";
                                    }
                            }
                            else{
                                    $pesan = "Sorry, Hanya JPG, JPEG, PNG & GIF yang diizinkan.";
                            }
                    }
    
    $pdf->SetFont('Arial','B',8);
    $htmlTable='
    <TD>1. Nama Lengkap : '.$_POST['namalengkap'].'</TD><br>
    <TD>2. Jenis Kelamin : '.$_POST['jeniskelamin'].'</TD><br>
    <TD>3. Tempat Lahir : '.$_POST['tempatlahir'].'</TD><br>
    <TD>4. Tanggal Lahir : '.$_POST['tanggallahir'].'</TD><br>
    <TD>5. Agama : '.$_POST['agama'].'</TD><br>
    <TD>6. Pekerjaan : '.$_POST['pekerjaan'].'</TD><br>
    <TD>7. Alamat : '.$_POST['alamat'].'</TD><br>
    <TD>8. No Telp : '.$_POST['notelpon'].'</TD><br>
    ';
    $pdf->WriteHTML2("<br><br>$htmlTable");
    $pdf->SetFont('Arial','B',8);
    $pdf->Output();
    $waktu = date('Y-m-d H:i:s');
    $to = "email@tujuan.com";
    $from = "email@pengirim.com";
    $subject = "E-Mail  $waktu";
    $message = "<p>Untuk Melihat silahkan Buka File PDF di Attachment</p>";
    
    // a random hash will be necessary to send mixed content
    $separator = md5(time());
    
    // carriage return type (we use a PHP end of line constant)
    $eol = PHP_EOL;
    
    // attachment name
    $filename = "Percobaan $namalengkap-$waktu.pdf";
    
    // encode data (puts attachment in proper format)
    $pdfdoc = $pdf->Output("", "S");
    $attachment = chunk_split(base64_encode($pdfdoc));
    
    $file = $folder.$filename1;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    
    // main header
    $headers  = "From: ".$from.$eol;
    $headers .= "MIME-Version: 1.0".$eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
    
    // no more headers after this, we start the body! //
    $body = "--".$separator.$eol;
    $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
    $body .= "This is a MIME encoded message.".$eol;
    
    // message
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $body .= $message.$eol;
    
    // attachment
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
    $body .= "Content-Transfer-Encoding: base64".$eol;
    $body .= "Content-Disposition: attachment".$eol.$eol;
    $body .= $attachment.$eol;
    $body .= $content.$eol;
    $body .= "--".$separator."--";
    mail($to, $subject, $body, $headers);
    ?>
    Hasilnya :
    1. Untuk file attachment yang dalam bentuk PDF, terkirim emailnya dan file attachmentnya ada,
    2. Untuk file gambar yang diupload dari formnya tidak termasuk ke dalam attachment,
    3. Saya inginnnya di dalam email itu nanti ada 2 file attachment, satu file pdf dan satu lg file gambar yg diupload dari form tadi, agar bs seperti itu kira2 apa yg perlu ditambahkan yach om pada script di atas,

    Terimakasih banyak sebelumnya
     
  7. xpreborn

    xpreborn Poster 2.0

    Messages:
    183
    Likes Received:
    4
    Trophy Points:
    18
    Pagi Om,
    Secara email sdh bisa terkirim namun ada sedikit kendala ini Om,
    Code:
    <?php
    require('WriteHTML.php');
    $pdf=new PDF_HTML();
    $pdf->AliasNbPages();
    $pdf->SetAutoPageBreak(true, 15);
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',11);
    $pdf->WriteHTML('<para><h1>Percobaan</h1>');
    
    $namalengkap=$_POST['namalengkap'];
    
    $pdf->SetFont('Arial','B',8);
    $htmlTable='
    <TD>1. Nama Lengkap : '.$_POST['namalengkap'].'</TD><br>
    <TD>2. Jenis Kelamin : '.$_POST['jeniskelamin'].'</TD><br>
    <TD>3. Tempat Lahir : '.$_POST['tempatlahir'].'</TD><br>
    <TD>4. Tanggal Lahir : '.$_POST['tanggallahir'].'</TD><br>
    <TD>5. Agama : '.$_POST['agama'].'</TD><br>
    <TD>6. Pekerjaan : '.$_POST['pekerjaan'].'</TD><br>
    <TD>7. Alamat : '.$_POST['alamat'].'</TD><br>
    <TD>8. No Telp : '.$_POST['notelpon'].'</TD><br>
    ';
    $pdf->WriteHTML2("<br><br>$htmlTable");
    $pdf->SetFont('Arial','B',8);
    $pdf->Output();
    $waktu = date('Y-m-d H:i:s');
    $to = "email@tujuan.com";
    $from = "email@pengirim.com";
    $subject = "E-Mail  $waktu";
    $message = "<p>Contoh File Gambar dan PDF di Attachment</p>";
    
    // a random hash will be necessary to send mixed content
    $separator = md5(time());
    
    // carriage return type (we use a PHP end of line constant)
    $eol = PHP_EOL;
    
    // attachment name
    $filename = "Percobaan $namalengkap-$waktu.pdf";
    
    // encode data (puts attachment in proper format)
    $pdfdoc = $pdf->Output("", "S");
    $attachment = chunk_split(base64_encode($pdfdoc));
    
    $images_arr = array();
    foreach($_FILES['gambar']['name'] as $multi=>$images_arr)
     {
    $imgFile = $_FILES['gambar']['name'][$multi];
    $tmp_dir = $_FILES['gambar']['tmp_name'][$multi];
    $imgSize = $_FILES['gambar']['size'][$multi];
    if(empty($imgFile)){
        $pesan = "Silahkan Pilih Gambar.";
    }
         else
    {
        $folder = 'img/';
        $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
        $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
        $filename1 = rand(1000,1000000).".".$imgExt;
        if(in_array($imgExt, $valid_extensions)){
        if($imgSize < 5000000)                          {
        move_uploaded_file($tmp_dir,$folder.$filename1);
    $file = $folder.$filename1;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $attachment1 = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    }
        else{
        $pesan = "Sorry, file anda kebesaran.";
    }
       }
        else{
        $pesan = "Sorry, Hanya JPG, JPEG, PNG & GIF yang diizinkan.";
       }
    }
    }
    // main header
    $headers  = "From: ".$from.$eol;
    $headers .= "MIME-Version: 1.0".$eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
    
    // no more headers after this, we start the body! //
    $body = "--".$separator.$eol;
    $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
    $body .= "This is a MIME encoded message.".$eol;
    
    // message
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $body .= $message.$eol;
    
    // attachment
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
    $body .= "Content-Transfer-Encoding: base64".$eol;
    $body .= "Content-Disposition: attachment".$eol.$eol;
    $body .= $attachment.$eol;
    
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: image/png; name=\"".$filename1."\"".$eol;
    $body .= "Content-Transfer-Encoding: base64".$eol;
    $body .= "Content-Disposition: attachment".$eol.$eol;
    $body .= $attachment1.$eol;
    $body .= "--".$separator."--";
    
    mail($to, $subject, $body, $headers);
    
    ?>
    Hasilnya :
    1. Jumlah file yang terupload di folder img bisa multi, misalnya di form saya pilih 3 gambar maka yg terupload di folder img ada 3 juga,
    2. Namun yang terkirim di attachment tetap satu gambar saja + file pdfnya
    Kira2 apa yg kurang yah Om agar yg terkirim di attachment jg 3 gambar,

    Terimakasih banyak sebelumnya
     
  8. xpreborn

    xpreborn Poster 2.0

    Messages:
    183
    Likes Received:
    4
    Trophy Points:
    18
    Masih belum fix yach om, jadi bagi yg berminat bantu masih diterima :D
    Sudah saya coba2 malah makin aneh hasilnya kadang tdk muncul gambarnya di attachment dan kadang hanya nama filenya saja yg ikut gambarnya tdk :frusty:
    Jadi, sementara saya masih pakai persis yg seperti di port #7 di atas
     
  9. xpreborn

    xpreborn Poster 2.0

    Messages:
    183
    Likes Received:
    4
    Trophy Points:
    18
    Case closed yach
    Pakai PHPMailer seperti saran Om2 di atas dan alur scriptnya dibantu beberapa Om2 sebelah
     
  10. masiqbal

    masiqbal Expert 2.0

    Messages:
    863
    Likes Received:
    249
    Trophy Points:
    43
    :113::113::113:
     
    xpreborn likes this.
Loading...
Thread Status:
Not open for further replies.

Share This Page

Loading...