Showing posts with label Send pdf file as attachment in email php. Show all posts
Showing posts with label Send pdf file as attachment in email php. Show all posts

How to create and download a csv file from php script?

How to create and download a csv file from php script?

Today discussed just core php while loop some data stored and display more then data just export download csv or pdf other format document. Here i try this code. this example code format csv export data.
How to create and download a csv file from php script?
How to create and download a csv file from php script?

$f = fopen("tmp.csv", "w");
foreach ($array as $line) {
    fputcsv($f, $line)
}
header dialog you will have to  send http header like this example header

header('Content-Disposition: attach
function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
    // open raw memory as file so no temp files needed, you might run out of memory though
    $f = fopen('php://memory', 'w'); 
    // loop over the input array
    foreach ($array as $line) { 
        // generate csv lines from the inner arrays
        fputcsv($f, $line, $delimiter); 
    }
    // reset the file pointer to the start of the file
    fseek($f, 0);
    // tell the browser it's going to be a csv file
    header('Content-Type: application/csv');
    // tell the browser we want to save it instead of displaying it
    header('Content-Disposition: attachment; filename="'.$filename.'";');
    // make php send the generated csv lines to the browser
    fpassthru($f);
}
array_to_csv_download(array(
  array(1,2,3,4), // this array is going to be the first row
  array(1,2,3,4)), // this array is going to be the second row
  "numbers.csv"
);


Instead of the php://memory you can  also use the php://output for the file descriptor and do away with the seeking and such
function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename="'.$filename.'";');

    // open the "output" stream
    // see http://www.php.net/manual/en/wrappers.php.php#refsect2-wrappers.php-unknown-unknown-unknown-descriptioq
    $f = fopen('php://output', 'w');

    foreach ($array as $line) {
        fputcsv($f, $line, $delimiter);
    }
}   

PHP Send pdf file as attachment in email


PHP Send pdf file as attachment in email

I am trying to send a pdf file as attchment in my email.

 Using following code.

send pdf file as attachment in email php
Send pdf file as attachment in email php

PHP send pdf file extension mpdf download and include this mpdf.php file in your code. Send pdf file as attachment in email php

 
    include("MPDF53/mpdf.php");
    $mpdf = new mPDF('c', 'A4-L', '', '', 0, 0, 0, 0, 0, 0);
    $mpdf->SetDisplayMode('fullpage');
    $mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list
    $filename = "Jims_tradenet_form.txt";

content.='<html lang="en-AU"><head><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css"><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
   <style type="text/css">          
            .form-group { padding-bottom: 5px; }
            label { font-size:12px; }          
        </style>      
    </head>
    <body>
        <div class="container">
            <section class="header" style="border-bottom:1px solid #000;">
                <div class="row">
                    <div class="col-md-3" style="min-height: 70px; max-height: 150px;">
                        <img src="https://www.nbaysitsolutions.com.au/images/logo.png" class="pull-left" style="max-width: 300px; opacity: .7;" />
                    </div>
                    <div class="col-md-6 text-center">
                        <h3 class="center" style="opacity:0.7; font-family: helvetica; font-weight: bold;">MY FIRST PDF FILE SEND MAIL</h3>
                    </div>
                    <div class="col-md-3 center">
                        <img src="https://www.jimstradenet.com/basis/images/tradex-logo.png" class="pull-right" style="max-width: 300px; opacity: .7; max-height: 60px;" />
                    </div>
                </div>
            </section>'
 $file = fopen($filename, "w");
    fwrite($file, $content);
    $mpdf->WriteHTML(file_get_contents($filename));
    $m = time();
    $n = $m . '.pdf';
    $m = 'files/' . $m . '.pdf';
    $mpdf->Output($m, 'F');
    $_SESSION['file'] = $m;

//$mpdf->Output();

    $emailBody .= '';

    $fileatt = $m; // Path to the file                
    $fileatt_type = "application/pdf"; // File Type
    $fileatt_name = $n; // Filename that will be used for the file as the attachment

    $email_from = "dev@gmail.com"; // Who the email is from
    $email_subject = "Attached File"; // The Subject of the email
    $email_message = $emailBody;

    $email_to = 'dev2tricks@gmail.com'; // Who the email is to

    $headers = "From: " . $email_from;

    $file = fopen($fileatt, 'rb');
    $data = fread($file, filesize($fileatt));
    fclose($file);

    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    $headers.= "\nMIME-Version: 1.0\n" .
            "Content-Type: multipart/mixed;\n" .
            " boundary=\"{$mime_boundary}\"";

    $email_message.= "This is a multi-part message in MIME format.\n\n" .
            "--{$mime_boundary}\n" .
            "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\n\n" .
            $email_message.= "\n\n";

    $data = chunk_split(base64_encode($data));

    $email_message.= "--{$mime_boundary}\n" .
            "Content-Type: {$fileatt_type};\n" .
            " name=\"{$fileatt_name}\"\n" .
            //"Content-Disposition: attachment;\n" .
            //" filename=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
            $data.= "\n\n" .
            "--{$mime_boundary}--\n";
    mail($email_to, $email_subject, $email_message, $headers);
    $msg = "Successfully Inserted";