如何使用phpmailer发送fpdf文件?

时间:2017-06-26 09:19:35

标签: php html phpmailer fpdf

我正在尝试在单击超链接时将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 CODE

<?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();
?>

1 个答案:

答案 0 :(得分:1)

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

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

你的工作已经完成。