我最近创建了一个用于为我们的网站创建招聘发布的在线模板。一切都完成了,它在浏览器中正确格式化,自动发布到我们的网站,bla bla bla。通过PHP向动态创建的PDF发送电子邮件
我正在创建的最后一部分是为管理员提供一些选项,以一致,便捷的方式将发布内容分发到各个地方(通过电子邮件)。我创建了一个使用TCPDF库即时创建PDF文档的PHP页面。当加载pdf.php?id = X时,页面会显示一个包含作业发布X内容的PDF。这意味着我从不将PDF文件保存到服务器,只需在每次调用时即可创建它。
但是我想把这个PDF附加到电子邮件中,并发送给各个学院和内部邮件列表等。如果我将pdf.php?id = x附加到电子邮件中,它不会附加PDF,它附加了上面的名字,似乎是一个空白文件。
是否可以将其附加到电子邮件而不保存到服务器?
下面根据JM4的回应进一步解决问题添加下面。我已将PDF文件创建功能放入函数中,并将其放入包含文件中,以便更容易管理。
// 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<[email protected]>".$eol;
//$headers .= "Bcc: [email protected]".$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('[email protected]', 'test job posting', 'message body goes here', $headers)) {
echo 'mail sent';
} else {
echo 'error in email';
}
这里是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");
}
我们展示的代码(或最好是很砍倒骨架)。这听起来像你用来将URL转换为文件附件的方法是错误的。为什么你非常担心不要临时写文件? – symcbean 2010-08-19 21:51:14
http://api.joomla.org/com-tecnick-tcpdf/TCPDF.html#methodOutput 我对dest使用'I'选项。我想知道如果S选项是我应该使用的。 – 2010-08-20 13:04:33
做了任何答案在这里帮助你? – JM4 2010-09-02 16:30:26