sending an email attachment using TCPDF
<p>PHP
I have a simple course application form, which when filled, an email is sent to the applicant with a fees quotation for the courses he selected as a pdf attachment. </p>

<p>I am using TCPDF and am passing the data from the form to the library using session variables. The content is in html format.</p>

<p>The PDF is generated and sent as an attachment as required, Problem is that it is blank..only the header and footer is in the document.  This is particularly so in linux. In windows the pdf document is generated as expected when downloaded. However, when you click on "view" before downloading the document, only the header and footer shows.</p>

<p>Here is my code. Please someone help. Thank you.</p>

<pre><code>&lt;?php
session_start();
require_once('../config/lang/eng.php');
require_once('../tcpdf.php');

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf-&gt;SetCreator(PDF_CREATOR);
$pdf-&gt;SetAuthor('Josiah Njuki');
$pdf-&gt;SetTitle('Quotation Request');
$pdf-&gt;SetSubject('TCPDF Tutorial');
$pdf-&gt;SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf-&gt;SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, '', PDF_HEADER_STRING);

// set header and footer fonts
$pdf-&gt;setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf-&gt;setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf-&gt;SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf-&gt;SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf-&gt;SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf-&gt;SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf-&gt;SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf-&gt;setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf-&gt;setLanguageArray($l);

// ---------------------------------------------------------

// set default font subsetting mode
$pdf-&gt;setFontSubsetting(true);

// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf-&gt;SetFont('dejavusans', '', 14, '', true);

// Add a page
// This method has several options, check the source code documentation for more information.
$pdf-&gt;AddPage();

// Set some content to print
$html = '&lt;span style="font-size:7pt;"&gt;' . $_SESSION['content'] . '&lt;/span&gt;';
$html .= &lt;&lt;&lt;EOD
EOD;

// Print text using writeHTMLCell()
$pdf-&gt;writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);

// ---------------------------------------------------------

// Close and output PDF document
// This method has several options, check the source code documentation for more information.
//$pdf-&gt;Output('example_001.pdf', 'I');
$doc = $pdf-&gt;Output('quotation.pdf', 'S');

//define the receiver of the email
    $name = "Name goes here";
    $email = "jnjuki103@gmail.com";

    $to = "$name &lt;{$_SESSION['email']}&gt;";

    $from = "John-Smith ";

    $subject = "Here is your attachment";

$fileatt = $pdf-&gt;Output('quotation.pdf', 'S');
    //$fileatt = "./test.pdf";

    $fileatttype = "application/pdf";

    $fileattname = "newname.pdf";

    $headers = "From: $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}\"";

    $message = "This is a multi-part message in MIME format.\n\n" .

    "-{$mime_boundary}\n" .

    "Content-Type: text/plain; charset=\"iso-8859-1\n" .

    "Content-Transfer-Encoding: 7bit\n\n" .

    $message .= "\n\n";

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

    $message .= "–{$mime_boundary}\n" .

    "Content-Type: {$fileatttype};\n" .

    " name=\"{$fileattname}\"\n" .

    "Content-Disposition: attachment;\n" .

    " filename=\"{$fileattname}\"\n" .

    "Content-Transfer-Encoding: base64\n\n" .

    $data . "\n\n" .

    "-{$mime_boundary}-\n";

    if(mail($to, $subject, $message, $headers)) {

    echo "

    The email was sent.

    ";

    }
    else {

    echo "

    There was an error sending the mail.

    ";

    }
//============================================================+
// END OF FILE
//============================================================+
</code></pre>
