使用TCPDF发送电子邮件附件

时间:2022-10-23 19:10:08

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.

我有一份简单的课程申请表,填写后会发邮件给申请人,并附上他所选课程的费用报价作为pdf附件。

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

我使用TCPDF并使用会话变量将数据从表单传递到库。内容是html格式的。

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.

PDF是按要求生成并作为附件发送的,问题是它是空的。只有页眉和页脚在文档中。在linux中尤其如此。在windows中,pdf文档在下载时按预期生成。但是,在下载文档之前单击“view”时,只显示页眉和页脚。

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 = "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
//============================================================+

9 个解决方案

#1


8  

1.you can do that using:

1。你可以这样做:

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

fileatt = pdf - >输出(美元的报价。pdf”、“E”);

the option E: return the document as base64 mime multi-part email attachment (RFC 2045) , i found this info on: tcpdf documentation

选项E:返回作为base64 mime多部分电子邮件附件(RFC 2045)的文档,我找到了关于:tcpdf文档的信息

after that you only need to do:

之后你只需要做:

$data = chunk_split($fileatt);

(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:

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

$filename = location_on_server."quotation.pdf";

文件名= location_on_server美元。“quotation.pdf”;

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

fileatt = pdf - >输出美元($ filename ' F ');

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

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

#2


6  

Output($name='yourfilename.pdf', $dest='E')

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):

E是你的问题的解。以下是$dest字符串(发送文档的目的地)的可能值的一个lisst:

  • I: send the file inline to the browser (default).
  • I:将文件内联发送到浏览器(默认)。
  • D: send to the browser and force a file download with the name given by name.
  • D:发送到浏览器并强制下载一个文件名指定的文件。
  • F: save to a local server file with the name given by name.
  • F:保存到本地服务器文件中,文件名由名称指定。
  • S: return the document as a string (name is ignored).
  • S:以字符串形式返回文档(名称被忽略)。
  • FI: equivalent to F + I option
  • FI:相当于F + I选项。
  • FD: equivalent to F + D option
  • FD:相当于F + D选项
  • E: return the document as base64 mime multi-part email attachment (RFC 2045)
  • E:将文档作为base64 mime多部分电子邮件附件返回(RFC 2045)

#3


5  

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.

我尝试过很多选择。我使用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`

#4


2  

First generate the PDF using tcpdf library.Here is the example for generate PDF:

<?php
    ini_set("display_errors", 1);

    require_once('./TCPDF/tcpdf.php');

    class MYPDF extends TCPDF {
        public function Footer() {
            $this->SetY(-15);
            $this->SetFont('helvetica', 'I', 8);
            $this->Cell(0, 0, 'Product Company - Spider india, Phone : +91 9940179997, TIC : New No.7, Old No.147,Mount Road, Little Mount,Saidapet, Chennai, Tamilnadu,Pin - 600015, India.', 0, 0, 'C');
            $this->Ln();
            $this->Cell(0,0,'www.spiderindia.com - TEL : +91-44-42305023, 42305337  - E : marketing@spiderindia.com', 0, false, 'C', 0, '', 0, false, 'T', 'M');
            $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
        }    
    }

    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Manikandan');
    $pdf->SetTitle('Quote');
    $pdf->SetSubject('Quote');
    $pdf->SetKeywords('PDF, Quote');

    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

    $pdf->SetFont('times', '', 15); 

    $pdf->AddPage();

    // create some HTML content
    $now = date("j F, Y");
    $company_name = 'ABC test company';

    $user_name = 'Mr. Manikandan';
    $invoice_ref_id = '2013/12/03';


    $html = '';
    $html .= '<table style="padding-top:25px;">
                <tr>
                    <td colspan="2" align="left"><h4>Kannama & CO</h4></td>
                    <td colspan="2" align="right"><h1 style="color:#6C8BB5;">Quote</h1></td>
                </tr>

                <tr>
                    <td colspan="2" align="left"><table><tr><td>'.$user_name.'</td></tr><tr><td>Mob NO : 9791564278</td></tr></table></td>
                    <td colspan="2" align="right"><table border="1" align="center"><tr><td>Date</td><td>'.$now.'</td></tr><tr><td>Quote</td><td>#0041</td></tr></table></td>
                </tr>

                <tr>
                    <td colspan="2" align="left"><table><tr><td style="background-color: #2B7DB9;color:white;width:75%;">Customer</td></tr><tr><td>Dear senthil kumar,</td></tr><tr><td>39,Jawahar sangam street,Chockalingapuram,Aruppukottai.</td></tr><tr><td><b>Mob no : </b> 9791564821</td></tr></table></td>
                </tr>


             </table>';

    $html .= '<br><br>
              <table border="1" cellpadding="5">
                <tr style="background-color: #2B7DB9;color:white;">
                    <td colspan="4">Quote # Quote 001</td>            
                </tr>
                <tr>
                    <td><b>Product</b></td>
                    <td><b>Quantity</b></td>
                    <td><b>Subtotal</b></td>
                    <td align="right"><b>Amount (Rs.)</b></td>
                </tr>
                <tr style="background-color: #C1E1F8;">
                    <td>Product 1</td>
                    <td>30</td>
                    <td>10</td>
                    <td align="right">300</td>
                </tr>
                <tr>
                    <td>Product 3</td>
                    <td>15</td>
                    <td>3</td>
                    <td align="right">75</td>
                </tr>
                <tr style="background-color: #C0E1F8;">
                    <td>Product 2</td>
                    <td>15</td>
                    <td>3</td>
                    <td align="right">75</td>
                </tr>
                <tr>
                    <td colspan="3" align="right"><b>Sub Total:</b></td>
                    <td align="right"><b> 375</b></td>
                </tr>
                <tr>
                    <td colspan="3" align="right"><b>GST:</b></td>
                    <td align="right"><b> 5</b></td>
                </tr>
                <tr>
                    <td colspan="3" align="right"><b>CGST:</b></td>
                    <td align="right"><b> 10</b></td>
                </tr>
                <tr>
                    <td colspan="3" align="right"><b>Delivery:</b></td>
                    <td align="right"><b> 100</b></td>
                </tr>
                <tr>
                    <td colspan="3" align="right"><b>Total:</b></td>
                    <td align="right"><b> 1000</b></td>
                </tr>
             </table>';

    $html = str_replace('{now}',$now, $html);
    $html = str_replace('{company_name}',$company_name, $html);
    $html = str_replace('{user_name}',$user_name, $html);
    $html = str_replace('{invoice_ref_id}',$invoice_ref_id, $html);

    $pdf->writeHTML($html, true, false, true, false, '');

    $pdf->lastPage();

    $fileName = time() . '.pdf';
    ob_clean();
    $pdf->Output(FCPATH . 'files/' . $fileName, 'F');

    if (file_exists(FCPATH . 'files/' . $fileName)) {
        if (!empty($data->email)) {
            $status = $this->sendReceiptEmail($fileName, $data->email);
            return $status;   
        } else {
            return false;
        }
    } else {
        return false;
    }

    Use PHP Mailer library to send email. Here is the example to send mail with attachment.

    function sendReceiptEmail($fileName, $mailto)
    {
        $file = FCPATH . 'files/' . $fileName;
        date_default_timezone_set('Asia/Kolkata');

        require __DIR__.'/../libraries/phpmailer/PHPMailerAutoload.php';

        $mail = new PHPMailer;

        $mail->SMTPDebug = 0;
        $mail->Debugoutput = 'html';
        $mail->IsSMTP();
        $mail->Host = "xxx.xxx.com";
        $mail->Port = 587;
        $mail->SMTPAuth = true;
        $mail->Username = "info@xxx.com";
        $mail->Password = "xxxxx";
        $mail->setFrom('info@xxx.com', 'Company Quote');
        $mail->addReplyTo('info@xxx.com');
        $mail->addAddress($mailto);
        $mail->Subject = 'Quote';
        $mail->Body = 'Here we attached quote detail.Please find the attachment.';

        $mail->addAttachment($file);

        //send the message, check for errors
        if (!$mail->send()) {
            return false;
        } else {
            $file = FCPATH . 'files/' . $fileName;
            if (file_exists($file)) {
                unlink($file);
            }
            return true;
        }
    }

#5


1  

Since the officially documented method using 'S' or 'E' returned null for me I went ahead and used the tried and tested output buffer trick, e.g.:

由于使用'S'或'E'的正式文档化方法为我返回null,所以我继续使用经过测试的输出缓冲区技巧,例如:

protected function PDF_string($pdf, $base64 = false) {
    ob_start();
    $pdf->Output('file.pdf', 'I');
    $pdf_data = ob_get_contents();
    ob_end_clean();

    return ($base64) ? base64_encode($pdf_data) : $pdf_data;
}

#6


0  

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.

按我的方式来做要做的工作要多得多,但我想给用户一个输出选项D:发送到浏览器,强制以名称命名的文件下载,或者立即将其保存为PDF文件,以便编辑。

Example_054.php.

Example_054.php。

Then I created a identical form from TCPDF with DOMPDF.

然后我用DOMPDF从TCPDF创建了一个相同的表单。

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.

提交表单后,表单将调用我的提交脚本,提交脚本将访问填充的值,然后填充相同的DOMPDF文档。然后,我将DOMPDF文档保存为新的极化值。保存的DOMPDF文件现在可以通过PHPMailer以完全填充的PDF文件电子邮件附件的形式发送。

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.

可能有一种更简单的方法可以通过TCPDF来实现这一点,但是这完全满足了我的需求。同时我还和TCPDF一起学习了DOMPDF。

#7


0  

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!!

作为我上一篇文章的补充,这只适用于用户选择从浏览器访问PDF文件时,输出选项D: Example_054.php。我没有看到任何其他方式的电子邮件作为一个PDF创建在浏览器,并发送作为实际的PDF文件电子邮件附件,没有重新创建PDF与DOMPDF,并保存已填充的PDF,以便它可以发送作为电子邮件附件。如果用户选择在填充之前保存PDF,那么这就不是问题,因为填充后的文件将存在于附件中。如果有更简单的方法来处理这个场景,请让我知道!!

#8


0  

There is code from tcpdf.php. we can see that what does E option do in Output function. it uses base64_encode & chunk_split and also add headers with it so there is no need to add headers again.

有来自tcpdf.php的代码。我们可以看到E选项在输出函数中的作用。它使用base64_encode & chunk_split并添加了标题,因此不需要再次添加标题。

case 'E': {
            // return PDF as base64 mime multi-part email attachment (RFC 2045)
            $retval = 'Content-Type: application/pdf;'."\r\n";
            $retval .= ' name="'.$name.'"'."\r\n";
            $retval .= 'Content-Transfer-Encoding: base64'."\r\n";
            $retval .= 'Content-Disposition: attachment;'."\r\n";
            $retval .= ' filename="'.$name.'"'."\r\n\r\n";
            $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
            return $retval;
        }

So in above question

所以在上面的问题

$attachFile = $pdf->Output('quotation.pdf', 'E');
$html_message = '<p> Your HTML/ Plain message Here </p>' ;
$uid = md5() ;

$header = "From: <user@example.com>\r\n";
$header .= "Reply-to: <user@example.com>\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

// message & attachment
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-type:text/html ;charset=UTF-8\r\n";
$nmessage .= $html_message."\r\n\r\n"; //Your message

$nmessage .= "--".$uid."\r\n";
$nmessage .= $attachFile."\r\n\r\n"; //adding Your PDF file
$nmessage .= "--".$uid."--" ;

#9


0  

Using CodeIgniter and TCPDF, this is what worked for me.

使用CodeIgniter和TCPDF,这对我很有用。

  1. Step 1: Save the document in the server.
  2. 步骤1:将文档保存在服务器中。
  3. Step 2: Get the file path and send the file path to the function for sending the email.

    步骤2:获取文件路径并将文件路径发送到发送电子邮件的函数。

    <?php
    
    function GenerateQuotation(){
        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('Name of Author');
    
        $pdf->SetTitle('Quotation Request');
    
        $pdf->SetSubject('TCPDF Tutorial');
    
        $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
    
        $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 = '<span style="font-size:7pt;">' . $_SESSION['content'] . '</span>';
        $html .= <<<EOD
        EOD;
    
        $pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);       
    
        //Change this according to where you want your document to be uploaded to
        $LocationOnServer = 'C:\wamp64\www\projectname\uploads\invoices/\/'; 
    
        $FileNamePath = $LocationOnServer.'quotation.pdf';
    
        //Save the document to the server
        $QuotationAttachment = $pdf->Output($FileNamePath, 'F');
    
        $EmailAddress = $_SESSION['email'];       
    
        if(!empty($FileNamePath)){
            $this->SendQuotation($EmailAddress,$FileNamePath);
        }
        else{
            print_r('Could not trace file path');
        }             
    
    }
    
    function SendQuotation($EmailAddress,$FileNamePath){
        $Subject = 'My subject here';
        $Message = 'My Email body message here';
    
        $this->email
            ->from('xxxxx@xxxx.com', 'From Who')    
            ->to($EmailAddress) 
            ->subject($Subject)
            ->message($Message);
    
        $this->email->attach($FileNamePath);
        if($this->email->send()){
            print_r('Email Sent');
    
        }else{
            print_r($this->email->print_debugger());
        }
    
    }
    
    
    }
    ?>
    

Invoke GenerateQuotation() function to generate the quotation and email it.

调用GenerateQuotation()函数来生成报价并发送电子邮件。

#1


8  

1.you can do that using:

1。你可以这样做:

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

fileatt = pdf - >输出(美元的报价。pdf”、“E”);

the option E: return the document as base64 mime multi-part email attachment (RFC 2045) , i found this info on: tcpdf documentation

选项E:返回作为base64 mime多部分电子邮件附件(RFC 2045)的文档,我找到了关于:tcpdf文档的信息

after that you only need to do:

之后你只需要做:

$data = chunk_split($fileatt);

(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:

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

$filename = location_on_server."quotation.pdf";

文件名= location_on_server美元。“quotation.pdf”;

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

fileatt = pdf - >输出美元($ filename ' F ');

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

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

#2


6  

Output($name='yourfilename.pdf', $dest='E')

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):

E是你的问题的解。以下是$dest字符串(发送文档的目的地)的可能值的一个lisst:

  • I: send the file inline to the browser (default).
  • I:将文件内联发送到浏览器(默认)。
  • D: send to the browser and force a file download with the name given by name.
  • D:发送到浏览器并强制下载一个文件名指定的文件。
  • F: save to a local server file with the name given by name.
  • F:保存到本地服务器文件中,文件名由名称指定。
  • S: return the document as a string (name is ignored).
  • S:以字符串形式返回文档(名称被忽略)。
  • FI: equivalent to F + I option
  • FI:相当于F + I选项。
  • FD: equivalent to F + D option
  • FD:相当于F + D选项
  • E: return the document as base64 mime multi-part email attachment (RFC 2045)
  • E:将文档作为base64 mime多部分电子邮件附件返回(RFC 2045)

#3


5  

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.

我尝试过很多选择。我使用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`

#4


2  

First generate the PDF using tcpdf library.Here is the example for generate PDF:

<?php
    ini_set("display_errors", 1);

    require_once('./TCPDF/tcpdf.php');

    class MYPDF extends TCPDF {
        public function Footer() {
            $this->SetY(-15);
            $this->SetFont('helvetica', 'I', 8);
            $this->Cell(0, 0, 'Product Company - Spider india, Phone : +91 9940179997, TIC : New No.7, Old No.147,Mount Road, Little Mount,Saidapet, Chennai, Tamilnadu,Pin - 600015, India.', 0, 0, 'C');
            $this->Ln();
            $this->Cell(0,0,'www.spiderindia.com - TEL : +91-44-42305023, 42305337  - E : marketing@spiderindia.com', 0, false, 'C', 0, '', 0, false, 'T', 'M');
            $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
        }    
    }

    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Manikandan');
    $pdf->SetTitle('Quote');
    $pdf->SetSubject('Quote');
    $pdf->SetKeywords('PDF, Quote');

    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

    $pdf->SetFont('times', '', 15); 

    $pdf->AddPage();

    // create some HTML content
    $now = date("j F, Y");
    $company_name = 'ABC test company';

    $user_name = 'Mr. Manikandan';
    $invoice_ref_id = '2013/12/03';


    $html = '';
    $html .= '<table style="padding-top:25px;">
                <tr>
                    <td colspan="2" align="left"><h4>Kannama & CO</h4></td>
                    <td colspan="2" align="right"><h1 style="color:#6C8BB5;">Quote</h1></td>
                </tr>

                <tr>
                    <td colspan="2" align="left"><table><tr><td>'.$user_name.'</td></tr><tr><td>Mob NO : 9791564278</td></tr></table></td>
                    <td colspan="2" align="right"><table border="1" align="center"><tr><td>Date</td><td>'.$now.'</td></tr><tr><td>Quote</td><td>#0041</td></tr></table></td>
                </tr>

                <tr>
                    <td colspan="2" align="left"><table><tr><td style="background-color: #2B7DB9;color:white;width:75%;">Customer</td></tr><tr><td>Dear senthil kumar,</td></tr><tr><td>39,Jawahar sangam street,Chockalingapuram,Aruppukottai.</td></tr><tr><td><b>Mob no : </b> 9791564821</td></tr></table></td>
                </tr>


             </table>';

    $html .= '<br><br>
              <table border="1" cellpadding="5">
                <tr style="background-color: #2B7DB9;color:white;">
                    <td colspan="4">Quote # Quote 001</td>            
                </tr>
                <tr>
                    <td><b>Product</b></td>
                    <td><b>Quantity</b></td>
                    <td><b>Subtotal</b></td>
                    <td align="right"><b>Amount (Rs.)</b></td>
                </tr>
                <tr style="background-color: #C1E1F8;">
                    <td>Product 1</td>
                    <td>30</td>
                    <td>10</td>
                    <td align="right">300</td>
                </tr>
                <tr>
                    <td>Product 3</td>
                    <td>15</td>
                    <td>3</td>
                    <td align="right">75</td>
                </tr>
                <tr style="background-color: #C0E1F8;">
                    <td>Product 2</td>
                    <td>15</td>
                    <td>3</td>
                    <td align="right">75</td>
                </tr>
                <tr>
                    <td colspan="3" align="right"><b>Sub Total:</b></td>
                    <td align="right"><b> 375</b></td>
                </tr>
                <tr>
                    <td colspan="3" align="right"><b>GST:</b></td>
                    <td align="right"><b> 5</b></td>
                </tr>
                <tr>
                    <td colspan="3" align="right"><b>CGST:</b></td>
                    <td align="right"><b> 10</b></td>
                </tr>
                <tr>
                    <td colspan="3" align="right"><b>Delivery:</b></td>
                    <td align="right"><b> 100</b></td>
                </tr>
                <tr>
                    <td colspan="3" align="right"><b>Total:</b></td>
                    <td align="right"><b> 1000</b></td>
                </tr>
             </table>';

    $html = str_replace('{now}',$now, $html);
    $html = str_replace('{company_name}',$company_name, $html);
    $html = str_replace('{user_name}',$user_name, $html);
    $html = str_replace('{invoice_ref_id}',$invoice_ref_id, $html);

    $pdf->writeHTML($html, true, false, true, false, '');

    $pdf->lastPage();

    $fileName = time() . '.pdf';
    ob_clean();
    $pdf->Output(FCPATH . 'files/' . $fileName, 'F');

    if (file_exists(FCPATH . 'files/' . $fileName)) {
        if (!empty($data->email)) {
            $status = $this->sendReceiptEmail($fileName, $data->email);
            return $status;   
        } else {
            return false;
        }
    } else {
        return false;
    }

    Use PHP Mailer library to send email. Here is the example to send mail with attachment.

    function sendReceiptEmail($fileName, $mailto)
    {
        $file = FCPATH . 'files/' . $fileName;
        date_default_timezone_set('Asia/Kolkata');

        require __DIR__.'/../libraries/phpmailer/PHPMailerAutoload.php';

        $mail = new PHPMailer;

        $mail->SMTPDebug = 0;
        $mail->Debugoutput = 'html';
        $mail->IsSMTP();
        $mail->Host = "xxx.xxx.com";
        $mail->Port = 587;
        $mail->SMTPAuth = true;
        $mail->Username = "info@xxx.com";
        $mail->Password = "xxxxx";
        $mail->setFrom('info@xxx.com', 'Company Quote');
        $mail->addReplyTo('info@xxx.com');
        $mail->addAddress($mailto);
        $mail->Subject = 'Quote';
        $mail->Body = 'Here we attached quote detail.Please find the attachment.';

        $mail->addAttachment($file);

        //send the message, check for errors
        if (!$mail->send()) {
            return false;
        } else {
            $file = FCPATH . 'files/' . $fileName;
            if (file_exists($file)) {
                unlink($file);
            }
            return true;
        }
    }

#5


1  

Since the officially documented method using 'S' or 'E' returned null for me I went ahead and used the tried and tested output buffer trick, e.g.:

由于使用'S'或'E'的正式文档化方法为我返回null,所以我继续使用经过测试的输出缓冲区技巧,例如:

protected function PDF_string($pdf, $base64 = false) {
    ob_start();
    $pdf->Output('file.pdf', 'I');
    $pdf_data = ob_get_contents();
    ob_end_clean();

    return ($base64) ? base64_encode($pdf_data) : $pdf_data;
}

#6


0  

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.

按我的方式来做要做的工作要多得多,但我想给用户一个输出选项D:发送到浏览器,强制以名称命名的文件下载,或者立即将其保存为PDF文件,以便编辑。

Example_054.php.

Example_054.php。

Then I created a identical form from TCPDF with DOMPDF.

然后我用DOMPDF从TCPDF创建了一个相同的表单。

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.

提交表单后,表单将调用我的提交脚本,提交脚本将访问填充的值,然后填充相同的DOMPDF文档。然后,我将DOMPDF文档保存为新的极化值。保存的DOMPDF文件现在可以通过PHPMailer以完全填充的PDF文件电子邮件附件的形式发送。

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.

可能有一种更简单的方法可以通过TCPDF来实现这一点,但是这完全满足了我的需求。同时我还和TCPDF一起学习了DOMPDF。

#7


0  

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!!

作为我上一篇文章的补充,这只适用于用户选择从浏览器访问PDF文件时,输出选项D: Example_054.php。我没有看到任何其他方式的电子邮件作为一个PDF创建在浏览器,并发送作为实际的PDF文件电子邮件附件,没有重新创建PDF与DOMPDF,并保存已填充的PDF,以便它可以发送作为电子邮件附件。如果用户选择在填充之前保存PDF,那么这就不是问题,因为填充后的文件将存在于附件中。如果有更简单的方法来处理这个场景,请让我知道!!

#8


0  

There is code from tcpdf.php. we can see that what does E option do in Output function. it uses base64_encode & chunk_split and also add headers with it so there is no need to add headers again.

有来自tcpdf.php的代码。我们可以看到E选项在输出函数中的作用。它使用base64_encode & chunk_split并添加了标题,因此不需要再次添加标题。

case 'E': {
            // return PDF as base64 mime multi-part email attachment (RFC 2045)
            $retval = 'Content-Type: application/pdf;'."\r\n";
            $retval .= ' name="'.$name.'"'."\r\n";
            $retval .= 'Content-Transfer-Encoding: base64'."\r\n";
            $retval .= 'Content-Disposition: attachment;'."\r\n";
            $retval .= ' filename="'.$name.'"'."\r\n\r\n";
            $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
            return $retval;
        }

So in above question

所以在上面的问题

$attachFile = $pdf->Output('quotation.pdf', 'E');
$html_message = '<p> Your HTML/ Plain message Here </p>' ;
$uid = md5() ;

$header = "From: <user@example.com>\r\n";
$header .= "Reply-to: <user@example.com>\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

// message & attachment
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-type:text/html ;charset=UTF-8\r\n";
$nmessage .= $html_message."\r\n\r\n"; //Your message

$nmessage .= "--".$uid."\r\n";
$nmessage .= $attachFile."\r\n\r\n"; //adding Your PDF file
$nmessage .= "--".$uid."--" ;

#9


0  

Using CodeIgniter and TCPDF, this is what worked for me.

使用CodeIgniter和TCPDF,这对我很有用。

  1. Step 1: Save the document in the server.
  2. 步骤1:将文档保存在服务器中。
  3. Step 2: Get the file path and send the file path to the function for sending the email.

    步骤2:获取文件路径并将文件路径发送到发送电子邮件的函数。

    <?php
    
    function GenerateQuotation(){
        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('Name of Author');
    
        $pdf->SetTitle('Quotation Request');
    
        $pdf->SetSubject('TCPDF Tutorial');
    
        $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
    
        $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 = '<span style="font-size:7pt;">' . $_SESSION['content'] . '</span>';
        $html .= <<<EOD
        EOD;
    
        $pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);       
    
        //Change this according to where you want your document to be uploaded to
        $LocationOnServer = 'C:\wamp64\www\projectname\uploads\invoices/\/'; 
    
        $FileNamePath = $LocationOnServer.'quotation.pdf';
    
        //Save the document to the server
        $QuotationAttachment = $pdf->Output($FileNamePath, 'F');
    
        $EmailAddress = $_SESSION['email'];       
    
        if(!empty($FileNamePath)){
            $this->SendQuotation($EmailAddress,$FileNamePath);
        }
        else{
            print_r('Could not trace file path');
        }             
    
    }
    
    function SendQuotation($EmailAddress,$FileNamePath){
        $Subject = 'My subject here';
        $Message = 'My Email body message here';
    
        $this->email
            ->from('xxxxx@xxxx.com', 'From Who')    
            ->to($EmailAddress) 
            ->subject($Subject)
            ->message($Message);
    
        $this->email->attach($FileNamePath);
        if($this->email->send()){
            print_r('Email Sent');
    
        }else{
            print_r($this->email->print_debugger());
        }
    
    }
    
    
    }
    ?>
    

Invoke GenerateQuotation() function to generate the quotation and email it.

调用GenerateQuotation()函数来生成报价并发送电子邮件。