使用 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 = "[email protected]";
$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
//============================================================+
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.
I am using TCPDF and am passing the data from the form to the library using session variables. The content is in html format.
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.
Here is my code. Please someone help. Thank you.
<?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 = "[email protected]";
$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
//============================================================+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
有来自
tcpdf.php
的代码。我们可以看到Output
函数中的E
选项做了什么。它使用base64_encode
&chunk_split
并添加headers
,因此无需再次添加标头。所以在上面的问题中
There is code from
tcpdf.php
. we can see that what doesE
option do inOutput
function. it usesbase64_encode
&chunk_split
and also addheaders
with it so there is no need to add headers again.So in above question
按照我的方式做这件事需要做很多工作,但我想为用户提供输出选项 D:发送到浏览器并强制使用名称给定的名称下载文件,或立即将其保存为可供编辑的 PDF 文件。
示例_054.php。
然后我使用 DOMPDF 从 TCPDF 创建了一个相同的表单。
提交表单后,表单将调用我的提交脚本,提交脚本将访问填充的值,然后填充我相同的 DOMPDF 文档。然后,我使用新的填充值保存 DOMPDF 文档。保存的 DOMPDF 文件现在可以通过 PHPMailer 作为完全填充的 PDF 文件电子邮件附件通过电子邮件发送。
通过 TCPDF 本身可能有一种更简单的方法来做到这一点,但这完美地满足了我的要求。我同时学习了 DOMPDF 和 TCPDF。
Its a lot more work to do it my way but I wanted to give the user the Output option D: send to the browser and force a file download with the name given by name or save it immediately as a PDF File ready for editing.
Example_054.php.
Then I created a identical form from TCPDF with DOMPDF.
Once the form is submitted the form will call my submit script and the submit script will access the populated values and in turn populate my identical DOMPDF doc. I then save my DOMPDF doc with its new polulated values. The saved DOMPDF file can Now be emailed via PHPMailer as a fully populated PDF file email attachment.
There was probably an easier way to do this via TCPDF itself but this satisfied my requirements perfectly. And I got to learn DOMPDF along with TCPDF at the same time.
作为我上一篇文章的修订,这仅适用于用户选择从浏览器访问 PDF 文件的情况,输出选项 D:Example_054.php。我没有看到任何其他方式以在浏览器上创建的 PDF 形式通过电子邮件发送并将其作为实际的 PDF 文件电子邮件附件发送,而无需使用 DOMPDF 重新创建 PDF 并保存填充的 PDF,以便可以将其作为电子邮件附件发送。如果用户选择在填充之前保存 PDF,则这不是问题,因为填充的文件随后将存在于附件中。如果有更简单的方法来处理这种情况,请告诉我!
As an ammendment to my last post this ONLY applies if the user elects to access the PDF file from the browser, Output Option D: Example_054.php. I did NOT see any other way of emailing as a PDF created on the browser and send that as the actual PDF file email attachment without re-creating the PDF with DOMPDF and saving the populated PDF so it could be sent then as the email attachment. If the user elects to save the PDF before populating then this is a non-issue as the populated file will then exist for the attachment. If there is a easier way to handle this scenario please let me know!!
使用
CodeIgniter
和TCPDF
,这对我有用。第二步:获取文件路径,并将文件路径发送给发送邮件的函数。
<前><代码>SetCreator(PDF_CREATOR);
$pdf->SetAuthor('作者姓名');
$pdf->SetTitle('报价请求');
$pdf->SetSubject('TCPDF 教程');
$pdf->SetKeywords('TCPDF, PDF, 示例, 测试, 指南');
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, '', PDF_HEADER_STRING);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setLanguageArray($l);
$pdf->setFontSubsetting(true);
$pdf->SetFont('dejavusans', '', 14, '', true);
$pdf->AddPage();
$html = '' 。 $_SESSION['内容'] 。 '';writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=真,$align='',$autopadding=true);
$html .= <<
//根据您希望文档上传到的位置更改此设置
$LocationOnServer = 'C:\wamp64\www\projectname\uploads\invoices/\/';
$FileNamePath = $LocationOnServer.'quotation.pdf';
//保存文档到服务器
$QuotationAttachment = $pdf->Output($FileNamePath, 'F');
$EmailAddress = $_SESSION['电子邮件'];
if(!empty($FileNamePath)){
$this->SendQuotation($EmailAddress,$FileNamePath);
}
别的{
print_r('无法跟踪文件路径');
}
}
函数 SendQuotation($EmailAddress,$FileNamePath){
$Subject = '这里是我的主题';
$Message = '这里是我的电子邮件正文';
$此->电子邮件
->from('[电子邮件受保护]', '来自谁' )
->to($EmailAddress)
->主题($主题)
-> 消息($Message);
$this->电子邮件->attach($FileNamePath);
if($this->电子邮件->send()){
print_r('电子邮件已发送');
}别的{
print_r($this->电子邮件->print_debugger());
}
}
}
?>
调用
GenerateQuotation()
函数生成报价单并通过电子邮件发送。Using
CodeIgniter
andTCPDF
, this is what worked for me.Step 2: Get the file path and send the file path to the function for sending the email.
Invoke
GenerateQuotation()
function to generate the quotation and email it.1.您可以使用:
$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)) );
1.you can do that using:
$fileatt = $pdf->Output('quotation.pdf', 'E');
the option E: return the document as base64 mime multi-part email attachment (RFC 2045) , i found this info on: tcpdf documentation
after that you only need to do:
$data = chunk_split($fileatt);
and your file is ready to be attached to mail, there is no need to work with files. Just keep your headers and other settings and it will do the work.
or
2.you can use F to save it on your server, then get data like this:
$filename = location_on_server."quotation.pdf";
$fileatt = $pdf->Output($filename, 'F');
$data = chunk_split( base64_encode(file_get_contents($filename)) );
E
是您遇到的问题的解决方案。以下是 $dest 字符串(发送文档的目标位置)的可能值的列表:I
:将文件内联发送到浏览器(默认)。D
:发送到浏览器并强制下载指定名称的文件。F
:保存到本地服务器文件,名称为name。S
:以字符串形式返回文档(名称被忽略)。FI
:相当于 F + I 选项FD
:相当于 F + D 选项E
:将文档作为 base64 mime 多部分电子邮件附件返回(RFC 2045)E
is the solution for the problem u have. Here is a lisst of the possible values of the $dest string (Destination where to send the document):I
: send the file inline to the browser (default).D
: send to the browser and force a file download with the name given by name.F
: save to a local server file with the name given by name.S
: return the document as a string (name is ignored).FI
: equivalent to F + I optionFD
: equivalent to F + D optionE
: return the document as base64 mime multi-part email attachment (RFC 2045)我尝试过很多选择。有效的方法是当我使用 tcpdf 输出设置“F”设置保存到文件夹时。包含 phpmailer 文件并使用下面的代码。
I have tried many options. The one that worked was when I used the tcpdf output setting "F" setting to save to a folder. Included the phpmailer files and used the code below.
我知道这是一个老问题(把它放在老问题上!)。但我设法这样做。
I know this is kind of old question (put old on it!). But I manage to do it like this.
由于使用
'S'
或'E'
的官方记录方法为我返回了null
我继续使用经过尝试和测试的输出缓冲区技巧,例如:Since the officially documented method using
'S'
or'E'
returnednull
for me I went ahead and used the tried and tested output buffer trick, e.g.: