Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Send Dynamic PDF via PHP Email

Featured Replies

Hi guys, me again.

 

So, iv created a dynamic PDF using html2pdf.

 

It is located in pdf.php?invoice-id=1

 

^ as an example.

 

I have a functions.php with my function to create the email upon creating an invoice via a form. Only when the pdf.php is visited with the unique id is it displayed.

 

I need to email the .pdf as an attachment WITHOUT saving the pdf to the server.

 

I was thinking i could do it like:

 

$attachment = "/pdf.php?invoice-id=1"

 

invoice id being generated via PHP also.

 

Hope this makes sense?

As always, any help is much appreciated.

  • Author

Okay,

 

So for those that CAN.. i managed to get this far but now when trying to open the pdf, it comes up as corrupt.

 

   require_once(dirname(__FILE__).'/pdf/html2pdf.class.php');
    $html2pdf = new HTML2PDF('P','A4','fr');
    $html2pdf->WriteHTML($content);


$to = "steve@domain.com";
$from = "noone@domain.com";
$subject = "send email with pdf attachment";
$message = "<p>Please see the attachment.</p>";
$separator = md5(time());
$eol = PHP_EOL;
$filename = "example.pdf";
$pdfdoc = $html2pdf->Output('', 'S');
$attachment = chunk_split(base64_encode($pdfdoc));


$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;



$body .= "Content-Transfer-Encoding: 7bit".$eol;
$body .= "This is a MIME encoded message.".$eol; //had one more .$eol


$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol; 


$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";


mail($to, $subject, $body, $headers);
  • Author

For anyone using html2pdf and of whom wants to email the dynamic pdf, here is the code that now works for me:

 

$content = "<style></style>
<page>
    <h1>$cname</h1>
	<table class='address'>
	<tr>
		<td><b>Invoice No: </b></td>
		<td>$invno</td>
	</tr>
	<tr>
		<td><b>Date: </b></td>
		<td>$date</td>
	</tr>
	<tr>
		<td><b>Address: </b></td>
		<td>$name</td>
	</tr>
	<tr>
		<td></td>
		<td>$address1</td>
	</tr>
	<tr>
		<td></td>
		<td>$address2</td>
	</tr>
	<tr>
		<td></td>
		<td>$address3</td>
	</tr>
	<tr>
		<td></td>
		<td>$town</td>
	</tr>
	<tr>
		<td></td>
		<td>$county</td>
	</tr>
	<tr>
		<td></td>
		<td>$postcode</td>
	</tr>
	</table>
	<table style='width: 700px;'>
		<tr>
			<td><b>Company: </b></td>
			<td>$company</td>
		</tr>
		<tr>
			<td><b>Address: </b></td>
			<td>$addr1</td>
			<td>>$addr1</td>
			<td>>$addr1</td>
			<td>>$town2</td>
			<td>>$county2</td>
			<td>>$postcode2</td>
		</tr>
		
	</table>
	<table class='items'>
		<tr><td><b>Item Code</b></td><td><b>Item Name</b></td><td><b>Description</b></td><td><b>Quantity</b></td><td><b>Price Per Unit</b></td><td><b>Total</b></td></tr>
	$items2
	</table>
</page>";

    require_once(dirname(__FILE__).'/pdf/html2pdf.class.php');
    $html2pdf = new HTML2PDF('P','A4','fr');
    $html2pdf->WriteHTML($content);


$to = "steve@email.com";
$from = "noone@domain.com";
$subject = "send email with pdf attachment";
$message = "<p>Please see the attachment.</p>";
$separator = md5(time());
$eol = PHP_EOL;
$filename = "example.pdf";
$pdfdoc = $html2pdf->Output('', 'S');
$attachment = chunk_split(base64_encode($pdfdoc));


$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;



$body .= "Content-Transfer-Encoding: 7bit".$eol;
$body .= "This is a MIME encoded message.".$eol; //had one more .$eol


$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol; 


$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";


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

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.