【问题标题】:How to send an fpdf file with phpmailer?如何使用 phpmailer 发送 fpdf 文件?
【发布时间】:2017-06-26 09:19:34
【问题描述】:

我正在尝试在单击超链接时将 fpdf 文件发送到 phpmailer。目前,超链接仅打开 pdf 文件。如何使用 phpmailer 附加它?

打开 PDF 的超链接

<a target="_blank" href="<?php echo SITEURL; ?>/mod/orders/x-SOprint_new.php?id=<?php echo $d['orderID']; ?>"><?php echo $d['orderID']; ?></a> 

PHPMAILER 代码

<?php
require 'phpmailer/PHPMailerAutoload.php';
session_start();
$orderID=$_SESSION['orderID'];
$tomailID=$_POST['tmail'];
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "";
$mail->Password = "Password";
$mail->SetFrom("");
$mail->Subject = "mail test for last time";
$mail->Body = "Respected Sir/Madam"
        . "Thanks for your Order"
        . "The Sales  Order Number:"+$orderID+" Date : ,"
        . "Sales order Copy Atached in PDF format"
        . "\n"
        . '\n'
        . "\n"
        . "This is computer generated Letter , Please do not reply on this Address";
$mail->AddAddress("$tomailID");

if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
    echo "Message has been sent"+$tomailID;
 }
?>

FPDF 文件

<?php
$pdf = new FPDF();
global $DB;
$view=$_REQUEST['vbeln'];
require("fpdf/fpdf.php");
$sql = "SELECT * FROM `" . $DB->pre . "party_outstanding_history` WHERE BELNR= '". $view ."'"; 
$rows = $DB->dbRow($sql);
$custCode= $rows['KUNNR'];
$docDate= $rows['AUGDT'];
$sql1 ="SELECT * FROM `" . $DB->pre . "party_master` WHERE KUNNR = '". $custCode."'";
$rows1 = $DB->dbRow($sql1);
$partyname = $rows1['NAME1'];
$pdf->AddPage();
$pdf->Rect(5, 5, 200, 287);
$pdf->SetFont('helvetica','B',12);
$pdf->Text(90,15,'JHAMPSTEAD DIVISION',1,2,'C');
$pdf->Text(70,20,'DEBIT/CREDIT NOTE',1,2,'C');
$pdf->SetFont('helvetica','',10);
$pdf->Text(10,35,'Doc No :',1,2,'C');
$pdf->Text(120,35,'Doc Date :',1,2,'C');
$pdf->Text(10,40,'Customer :',1,2,'C');
$pdf->Text(10,45,'Customer Name :',1,2,'C');
$pdf-> Ln();
$pdf->Text(10,60,'Agent :',1,2,'C');
$pdf->Text(10,65,'Reference :',1,2,'C');
$pdf->Text(10,70,'Text :',1,2,'C');
$pdf-> Ln();
$pdf->SetFont('Arial','B',9);
$pdf->SetXY(11,103);
$pdf->Cell(25,7,'BILL NO.',1,0,'L',0);
$pdf->Cell(22,7,'DATE',1,0,'L',0);
$pdf->Cell(22,7,'AMOUNT',1,0,'L',0);
$pdf->Cell(18,7,'LDAYS',1,0,'L',0);
$pdf->Cell(18,7,'EP1',1,0,'L',0);
$pdf->Cell(18,7,'EP2',1,0,'L',0);
$pdf->Cell(22,7,'INTDR1',1,0,'L',0);
$pdf->Cell(22,7,'INTAMT',1,0,'L',0);
$pdf->Cell(18,7,'NET',1,0,'L',0);
$pdf-> Ln();
$pdf-> Ln();
$pdf->SetX(11);
$pdf->Cell(47,7,'TOTAL',1,0,'L',0);
$pdf->Cell(22,7,'',1,0,'L',0);
$pdf->Cell(18,7,'',1,0,'L',0);
$pdf->Cell(18,7,'',1,0,'L',0);
$pdf->Cell(18,7,'',1,0,'L',0);
$pdf->Cell(22,7,'',1,0,'L',0);
$pdf->Cell(22,7,'',1,0,'L',0);
$pdf->Cell(18,7,'',1,0,'L',0);
$pdf->Ln();
$pdf->SetX(156);
$pdf->Cell(22,7,'Net Amt.',1,0,'L',0);
$pdf->Cell(18,7,'',1,0,'L',0);
$pdf->SetFont("Arial","B","8");
$pdf->SetXY (5,271);
$pdf->Output();
?>

【问题讨论】:

  • 也就是说你想通过电子邮件发送PDF文件对吧?
  • 是的,没错
  • 我链接到的重复问题显示了如何将 fpdf 生成与通过 PHPMailer 发送集成,它不需要使用外部文件或辅助 HTTP 请求。

标签: php html phpmailer fpdf


【解决方案1】:

根据 cmets 中的讨论,在发送文件之前,必须创建文件,因此您需要创建 PDF 文件 更改 FPDF FILE 中的代码

$pdf->SetXY (5,271);
$filename="C:xampp/htdocs/foldername/invoice.pdf";
$pdf->Output($filename,'F');

在后面加上这个

$mail->Subject = "mail test for last time";
$mail->AddAttachment("Path to PDF file the above one mentioned");

另外,如果您想在此处发送下载 pdf 文件的链接:

$filename = 'Test.pdf'; // of course find the exact filename....        
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browsers 
header('Content-Type: application/pdf');

header('Content-Disposition: attachment; filename="'. basename($filename) . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));

readfile($filename);

exit;

将上述文件命名为download.php

您在电子邮件中发送此内容的链接:

<a href="download.php">Test.pdf</a>

你的工作已经完成了。

【讨论】:

  • 我应该将名称“phpmailer.gif”替换为pdf文件的名称吗?
  • 是的,路径完整
  • 我该如何弥补超链接和phpmailer之间的差距?
  • 你想以链接格式发送 PDF,同样当用户点击链接时它会得到他的 pdf?
  • 那我猜你需要创建另一个文件并在链接中发送下载路径而不是发送整个附件,让我重新编写代码给我一两分钟
猜你喜欢
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-29
  • 2020-04-05
  • 2019-11-24
  • 1970-01-01
相关资源
最近更新 更多