Emailing A Dynamically Created PDF Through PHP

I've recently created an online template for creating job postings for our website. Everything is all done, it formats correctly in a browser, automatically posts to our website, bla bla bla.

The last piece I'm creating is to give the administrator a few options for distributing the posting to various places (via email) in a consistent, convenient way. I've created a PHP page that creates a PDF doc on the fly, using the TCPDF library. When loading pdf.php?id=X, the page displays a PDF with the content of job posting X. This means I'm never saving the PDF file to the server, just creating it on the fly each time it's called.

But I want to attach this PDF to an email, and send it to various colleges, and internal mailing lists, etc. If I attach the pdf.php?id=x to the email, it doesn't attach the PDF, it attaches what appears to be a blank file, with the above name.

Is it possible to attach this to the email without saving it to the server?


Below added based on JM4's response for further trouble shooting. I have put the PDF file creation into a function, and put it into an include file, just to keep things easier to manage.

// random hash necessary to send mixed content
$separator = md5(time());

$eol = PHP_EOL;

// attachment name
$filename = "_Desiredfilename.pdf";

include_once('pdf.php');
// encode data (puts attachment in proper format)
$pdfdoc = job_posting_to_pdf($posting_id);
$attachment = chunk_split(base64_encode($pdfdoc));

///////////HEADERS INFORMATION////////////
// main header (multipart mandatory) message
$headers  = "From: Sender_Name<valid_email@mydomain.com>".$eol;
//$headers .= "Bcc: email@domain.com".$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;

// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;

// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";

//Email message
if(mail('valid_email@mydomain.com', 'test job posting', 'message body goes here', $headers)) {
    echo 'mail sent';
} else {
    echo 'error in email';
}

Here is a stripped down version of pdf.php:

function job_posting_to_pdf($job_id) {
    require_once(ROOT . 'assets/libs/tcpdf/config/lang/eng.php');
    require_once(ROOT . 'assets/libs/tcpdf/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->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('');
    $pdf->SetTitle('OPL Job Posting');
    $pdf->SetSubject('Job Posting');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

    // remove default header/footer
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);

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

    //set margins
    $pdf->SetMargins(11, PDF_MARGIN_TOP, 11);

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

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

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

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

    $pdf->SetFont('times', 'I', 9);
    $pdf->AddPage();

    $left_cell_width = 60;
    $row_height = 6;

    $pdf->Image(ROOT . 'assets/gfx/logos/OPL-Logo.jpg', 0, 5, null, 16, null, null, 'N', false, null,'R');
    $pdf->Ln('3');

    if(!$row['internal']) {
        $pdf->Cell(0,0,'This position will be posted internally and externally, concurrently.',0,2,'C');
    } else {
        $pdf->Cell(0,0,'Internal posting only.',0,2,'C');
    }

    //Remainder of actual PDF creation removed to keep things simple


    return $pdf->Output("", "S");
}
7
задан Cory Dee 3 September 2010 в 13:02
поделиться

3 ответа

Взгляните на эту страницу, на которой обсуждается расширенная электронная почта в PHP.

http://articles.sitepoint.com/article/advanced-email-php/5

Они берут загруженный файл и загружают двоичные данные в $ data, но вы можете просто начать оттуда.

0
ответ дан 7 December 2019 в 09:55
поделиться

Вы также можете попробовать отправить его в виде вложения через PEAR Mail_Mime. Он может принимать вложение в виде строки данных.

Похоже, что пакет RMail будет делать то же самое через класс stringAttachment. Вам придется искать его в Google, потому что я новый пользователь и поэтому могу публиковать только одну ссылку за раз.

0
ответ дан 7 December 2019 в 09:55
поделиться

Если я полностью понимаю, о чем вы спрашиваете, это довольно просто. Я предполагаю, что у вас уже есть PDF-файл, созданный с использованием чего-то вроде fdpf или tcpdf. В этом случае просто используйте следующий код:

<?php
    // random hash necessary to send mixed content
    $separator = md5(time());

    $eol = PHP_EOL;

    // attachment name
    $filename = "_Desiredfilename.pdf";

    // encode data (puts attachment in proper format)
    $pdfdoc = $pdf->Output("", "S");
    $attachment = chunk_split(base64_encode($pdfdoc));

    ///////////HEADERS INFORMATION////////////
    // main header (multipart mandatory) message
    $headers  = "From: Sender_Name<sender@domain.com>".$eol;
    $headers .= "Bcc: email@domain.com".$eol;
    $headers .= "MIME-Version: 1.0".$eol; 
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
    $headers .= "Content-Transfer-Encoding: 7bit".$eol;
    $headers .= "This is a MIME encoded message.".$eol.$eol;

    // message
    $headers .= "--".$separator.$eol;
    $headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $headers .= $message.$eol.$eol;

    // attachment
    $headers .= "--".$separator.$eol;
    $headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
    $headers .= "Content-Transfer-Encoding: base64".$eol;
    $headers .= "Content-Disposition: attachment".$eol.$eol;
    $headers .= $attachment.$eol.$eol;
    $headers .= "--".$separator."--";


    //Email message
    mail($emailto, $emailsubject, $emailbody, $headers);

    ?>
5
ответ дан 7 December 2019 в 09:55
поделиться
Другие вопросы по тегам:

Похожие вопросы: