使用TCPDF发送电子邮件附件

Jos*_*uki 7 php tcpdf

PHP我有一个简单的课程申请表,填写完成后,会向申请人发送一封电子邮件,并附上他选择作为pdf附件的课程的费用报价.

我正在使用TCPDF并使用会话变量将数据从表单传递到库.内容采用html格式.

生成PDF并根据需要作为附件发送.问题是它是空白的..只有页眉和页脚在文档中.在linux中尤其如此.在Windows中,pdf文档在下载时按预期生成.但是,在下载文档之前单击"视图"时,只显示页眉和页脚.

这是我的代码.请有人帮忙.谢谢.

<?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->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Josiah Njuki');
$pdf->SetTitle('Quotation Request');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

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

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

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

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

//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);

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

// set default font subsetting mode
$pdf->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->SetFont('dejavusans', '', 14, '', true);

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

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

// Print text using writeHTMLCell()
$pdf->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->Output('example_001.pdf', 'I');
$doc = $pdf->Output('quotation.pdf', 'S');

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

$to = "$name <{$_SESSION['email']}>";

$from = "John-Smith ";

$subject = "Here is your attachment";

$fileatt = $pdf->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
//============================================================+
Run Code Online (Sandbox Code Playgroud)

Dec*_*bal 8

你可以这样做:

$fileatt = $pdf->Output('quotation.pdf', 'E');

选项E:将文档作为base64 mime多部分电子邮件附件(RFC 2045)返回,我发现此信息:tcpdf文档

之后你只需要这样做:

$data = chunk_split($fileatt);

并且您的文件已准备好附加到邮件,无需使用文件.只需保留标题和其他设置即可完成工作.

要么

2.您可以使用F将其保存在服务器上,然后获取如下数据:

$filename = location_on_server."quotation.pdf";

$fileatt = $pdf->Output($filename, 'F');

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

  • 不需要`$ data = chunk_split($ fileatt);`,因为TCPDF已经将数据拆分成块.也许过去并非如此.事实上,在它的输出上使用`chunk_split`现在搞砸了. (4认同)

Kar*_*eem 7

Output($name='yourfilename.pdf', $dest='E')
Run Code Online (Sandbox Code Playgroud)

E是你解决的问题的解决方案.这是$ dest字符串(Destination发送文档的位置)的可能值的lisst:

  • I:将文件内联发送到浏览器(默认).
  • D:发送到浏览器并强制使用名称给出的名称下载文件.
  • F:保存到名称为name的名称的本地服务器文件.
  • S:将文档作为字符串返回(名称被忽略).
  • FI:相当于F + I选项
  • FD:相当于F + D选项
  • E:将文档作为base64 mime多部分电子邮件附件(RFC 2045)返回


小智 6

我尝试了很多选择.有效的是当我使用tcpdf输出设置"F"设置保存到文件夹时.包含phpmailer文件并使用下面的代码.

$pdf->Output("folder/filename.pdf", "F"); //save the pdf to a folder setting `F`
require_once('phpmailer/class.phpmailer.php'); //where your phpmailer folder is
$mail = new PHPMailer();                    
$mail->From = "email.com";
$mail->FromName = "Your name";
$mail->AddAddress("email@yahoo.com");
$mail->AddReplyTo("email@gmail.com", "Your name");               
$mail->AddAttachment("folder/filename.pdf");      
// attach pdf that was saved in a folder
$mail->Subject = "Email Subject";                  
$mail->Body = "Email Body";
if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
   echo "Message sent";
} //`the end`
Run Code Online (Sandbox Code Playgroud)