Solved

Create PDF using fpdf, then send email with pdf as attachment

Posted on 2012-04-03
3
3,076 Views
Last Modified: 2012-04-03
Can you please look at the below code which should create a PDF using fpdf and send as an attachment.

However the email is not getting sent. Can you please let me know what I am doing wrong?

<?php 

require('fpdf/fpdf.php');

class PDF_reciept extends FPDF {
	function __construct ($orientation = 'P', $unit = 'pt', $format = 'Letter', $margin = 40) {
		$this->FPDF($orientation, $unit, $format);
		$this->SetTopMargin($margin);
		$this->SetLeftMargin($margin);
		$this->SetRightMargin($margin);
		
		$this->SetAutoPageBreak(true, $margin);
	}
	
	function Header () {
		$this->SetFont('Arial', 'B', 20);
		$this->SetFillColor(36, 96, 84);
		$this->SetTextColor(225);
		$this->Cell(0, 30, "Nettuts+ Online Store", 0, 1, 'C', true);
	}
	
	function Footer () {
		$this->SetFont('Arial', '', 12);
		$this->SetTextColor(0);
		$this->SetXY(40, -60);
		$this->Cell(0, 20, "Thank you for shopping at Nettuts+", 'T', 0, 'C');
	}
	
function PriceTable($products, $prices) {
	$this->SetFont('Arial', 'B', 12);
	$this->SetTextColor(0);
	$this->SetFillColor(36, 140, 129);
	$this->SetLineWidth(1);
	$this->Cell(427, 25, "Item Description", 'LTR', 0, 'C', true);
	$this->Cell(100, 25, "Price", 'LTR', 1, 'C', true);
	 
	$this->SetFont('Arial', '');
	$this->SetFillColor(238);
	$this->SetLineWidth(0.2);
	$fill = false;
	
	for ($i = 0; $i < count($products); $i++) {
		$this->Cell(427, 20, $products[$i], 1, 0, 'L', $fill);
		$this->Cell(100, 20, '$' . $prices[$i], 1, 1, 'R', $fill);
		$fill = !$fill;
	}
	$this->SetX(367);
	$this->Cell(100, 20, "Total", 1);
	$this->Cell(100, 20, '$' . array_sum($prices), 1, 1, 'R');
}
	
	
	
	
	
	
}

$pdf = new PDF_reciept();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 12);
$pdf->SetY(100);

$pdf->Cell(100, 13, "Ordered By");
$pdf->SetFont('Arial', '');

$pdf->Cell(100, 13, $_POST['name']);

$pdf->SetFont('Arial', 'B');
$pdf->Cell(50, 13, "Date:");
$pdf->SetFont('Arial', '');
$pdf->Cell(100, 13, date('F j, Y'), 0, 1);

$pdf->SetFont('Arial', 'I');
$pdf->SetX(140);
$pdf->Cell(200, 15, $_POST['address'], 0, 2);
$pdf->Cell(200, 15, $_POST['city'] . ',' . $_POST['province'] , 0, 2);
$pdf->Cell(200, 15, $_POST['postal_code'] . ' ' . $_POST['country'], 0, 2);

$pdf->Ln(100);

$pdf->PriceTable($_POST['product'], $_POST['price']);

$pdf->Ln(50);

$message = "Thank you for ordering at the Nettuts+ online store. Our policy is to ship your materials within two busness days of purchase. On all orders over $20.00, we offer free 2-3 day shipping. If you haven't recieved your items in 3 busines days, let us know and we'll reimburse you 5%.\n\nWe hope you enjoy the items you have purchased. If you have any questions, you can email us at the following email address:";

$pdf->MultiCell(0, 15, $message);

$pdf->SetFont('Arial', 'U', 12);
$pdf->SetTextColor(1, 162, 232);

$pdf->Write(13, "store@nettuts.com", "mailto:example@example.com");

/*$pdf->Output('reciept.pdf', 'F');*/

$attachment->AddStringAttachment($pdf->Output('', 'S'), 'message.pdf', "base64", 'application/pdf');
	
$to = 'info@mydomain.com';
$subject = 'Test Email';

// Headers
$headers = "MIME-Version: 1.0\r\n"; 
$headers .= 'Content-type: text/html; charset=UTF-8"' . "\r\n"; 
$headers .= 'From: ' . $from . "\r\n"; 
$headers .= $attachment;

// Message
$message = "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><head></head><body>\r\n";
$message.= "Test Email";
$message.="</body></html>";

mail($to, $subject, $message, $headers);

Open in new window

0
Comment
Question by:petewinter
3 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 350 total points
ID: 37800449
You have this code:

  $attachment->AddStringAttachment($pdf->Output('', 'S'), 'message.pdf', "base64", 'application/pdf');

...but I don't see $attachment being defined or created anywhere. I believe that AddStringAttachment is a function of PHPMailer, which you don't appear to be using (but it is a good class, so I would recommend using it).
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 37801145
Just a thought... Why send the email with an attachment?  Many mail reader programs for mobile devices cannot use attachments, and proliferating copies of the information is not to anybody's advantage.  Maybe instead you could store the PDF file on the server and send the client a clickable URL that links to the PDF.
0
 

Author Closing Comment

by:petewinter
ID: 37801212
Thanks Both and good point Ray_Paseur. A link maybe the best solution
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now