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

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

petewinterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
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
petewinterAuthor Commented:
Thanks Both and good point Ray_Paseur. A link maybe the best solution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.