gpt4 book ai didi

phpmailer - 使用 phpmailer 将 tcpdf 作为附件发送

转载 作者:行者123 更新时间:2023-12-02 21:28:11 27 4
gpt4 key购买 nike

我可以生成 pdf 以便轻松查看和下载。我正在尝试发送一封附有 pdf 的电子邮件。我已经连续几天浏览谷歌了几个小时,没有什么比这个链接更让我更接近了,sending an email attachment using TCPDF 。我在链接问题上遇到了与提问者相同的问题。带有附件的电子邮件发送得很好,但 pdf 是空的,并且根据 adobe 的说法“似乎已损坏”。

共识似乎是 phpmailer 是最好的方法,所以我安装了 phpmailer 并且有以下代码。现在我收到此错误, fatal error :在第 122 行的非对象上调用成员函数 Output()。如果有人可以帮助我,我将不胜感激!



<?php
include ('../../include/connect.php');
$action=$_GET['a'];
/*----------*/

// Include the main TCPDF library (search for installation path).
require_once('tcpdf_include.php');


// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {

//Page header
public function Header() {
// Logo
//$image_file = K_PATH_IMAGES.'name_bar.gif';
//$this->Image($image_file, 30, 5, 150, '', 'GIF', '', 'T', false, 300, '', false, false, 0, false, false, false);
// Set font
$this->SetFont('helvetica', 'B', 20);
// Title
//$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
}

// Page footer
public function Footer() {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont('helvetica', '', 8);
// Page number
$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
}
}

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('TEST');
$pdf->SetTitle($title);

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, 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(10, 10, 10);
$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 (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}

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

// set font
$pdf->SetFont('helvetica', 'B', 20);

// add a page
$pdf->AddPage();

// set JPEG quality
$pdf->setJPEGQuality(300);

// Image method signature:
// Image($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox=false, $hidden=false, $fitonpage=false)

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

// Example of Image from data stream ('')
$imgdata = base64_decode('iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABlBMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDrEX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==');

// The '@' character is used to indicate that follows an image data stream and not an image file name
//$pdf->Image('@'.$imgdata);

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

// Image example with resizing

//$pdf->Write(0, 'Example of HTML tables', '', 0, 'L', true, 0, false, false, 0);

$pdf->SetFont('helvetica', '', 8);

// -----------------------------------------------------------------------------
$tbl = <<<EOD

test

EOD;

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

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

//Close and output PDF document
if($action=='I'){
$pdf->Output($title, 'I');
}elseif($action=='D'){
$pdf->Output($title, 'D');
}

//============================================================+
// END OF FILE
//============================================================+
if($action=='E'){
$attachment = $makepdf->Output($title, 'E');
SENDmail($attachment);

function SENDmail($pdf) {
require_once('../../include/class.phpmailer.php');
$mailer = new PHPMailer();

$mailer->AddReplyTo('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f68493869a8fb68299d89597" rel="noreferrer noopener nofollow">[email protected]</a>', 'Reply To');
$mailer->SetFrom('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d2e3833291d3b2f3230733e3c" rel="noreferrer noopener nofollow">[email protected]</a>', 'Sent From');
$mailer->AddReplyTo('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="087a6d786471487c67266b69" rel="noreferrer noopener nofollow">[email protected]</a>', 'Reply To');
$mailer->AddAddress('email', 'Send To');
$mailer->Subject = 'Message with PDF';
$mailer->AltBody = "To view the message, please use an HTML compatible email viewer";
$mailer->MsgHTML('<p>Message contents</p>');
if ($pdf) {$mailer->AddStringAttachment($pdf, $title);}

$mailer->Send();
}

}
?>

最佳答案

您可以将 PDF 存储为字符串:

$attachment = $pdf->Output('hotel4cast.pdf', 'S');

然后通过 PHPMailer 将其附加:

$mail->AddStringAttachment($attachment, 'filename.pdf', 'base64', 'application/pdf');

关于phpmailer - 使用 phpmailer 将 tcpdf 作为附件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23003431/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com