0

I'm trying to send an fpdf file to phpmailer while the hyperlink is clicked. Currently the hyperlink only opens the pdf file. How do I attach it with the phpmailer?

HYPERLINK THAT OPENS THE 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 FILE

<?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();
?>
Hans Solo
  • 55
  • 3
  • 16

1 Answers1

1

As per discussion in comments, before sending the file, the file must be created so you need to create the PDF File Change the code in FPDF FILE

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

Add this after

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

Also if you want to send link to download the pdf file here it is:

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

Name the above file as download.php

Your Link to send this in an email:

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

Your work is done.

Jaymin
  • 1,643
  • 1
  • 18
  • 39
  • should I replace the name "phpmailer.gif" with the name of the pdf file? – Hans Solo Jun 26 '17 at 09:26
  • Yes with complete path – Jaymin Jun 26 '17 at 09:27
  • And how do I bridge the gap between the hyperlink and phpmailer? – Hans Solo Jun 26 '17 at 09:29
  • Do You want to send PDF in a link format, likewise when a user clicks on link it will get his pdf? – Jaymin Jun 26 '17 at 09:30
  • Then I guess you need to create a another file and send the path to download in link rather than sending whole attachment, let me rewise the code give me a minute or two – Jaymin Jun 26 '17 at 09:35
  • Also I think your code is doing waht exactly you want, Isn't it downloading the file by clicking it on it? I prefer to send pdf file as an attachment. – Jaymin Jun 26 '17 at 09:37
  • No.. downloading is fine.. but I also want to send the pdf via email on hyperlink click.. are both the actions possible? – Hans Solo Jun 26 '17 at 09:40
  • Yes see I have updated both. It will increase your work only never mind I have update the code check it please :) – Jaymin Jun 26 '17 at 09:41
  • Thank you sir.. I'll get back to you after trying this code. – Hans Solo Jun 26 '17 at 09:45
  • Sure if you want to you can upvote it for my efforts :)\ – Jaymin Jun 26 '17 at 09:46
  • should I add the download.php code in the current code or create a different file? – Hans Solo Jun 26 '17 at 09:48
  • Different file :) – Jaymin Jun 26 '17 at 09:48
  • and what should I replace Test.pdf with? Like my current pdf file name is print_new.php. Should I put that there? – Hans Solo Jun 26 '17 at 09:53
  • Give here the path to Physical PDF File where it is been stored eg $filename = 'C://xamp/htdocs/Test.pdf'; Something like that – Jaymin Jun 26 '17 at 09:54
  • But sir.. the physical pdf file is not downloaded.. It just opens in a new tab – Hans Solo Jun 26 '17 at 09:57
  • Why don't you generate first and then send the pdf file. ? – Jaymin Jun 26 '17 at 09:58
  • You see FPDF file in this $pdf->Output(); you are generating the pdf file do this before and then execute the remaining file – Jaymin Jun 26 '17 at 10:00
  • Also add this in FPDF at last line : $filename= 'C://xamp/htdocs/Test.pdf'; $pdf->Output($filename,'F'); This will create your pdf file in this location Got it? – Jaymin Jun 26 '17 at 10:03
  • Actually sir. This is not my project, it is the company's project. So what they basically want is when I click the link, it should open in one tab and the pdf should be sent as an attachment to the concerned email id. – Hans Solo Jun 26 '17 at 10:04
  • Yes I do understand your functinality just tell me before sending the file it should be created first right? Thats what i am telling you to change LEt me edit the code again – Jaymin Jun 26 '17 at 10:06
  • Maybe the Port Issues while sending an email – Jaymin Jun 26 '17 at 11:08