I try to build some payroll system that can send a pdf file payslip document to specified person using only save button, I use fpdf to generate pdf, how can I send this pdf file while I generate it without save it first to webserver? or maybe I should save it first then send it?
1 Answers
If you're using a framework like Zend, you can pretty easily attach a mime part to the email without having to first save it to disk. This example here uses file_get_contents()
to read the contents of the PDF, but if you already have the data as a string, just disregard that portion:
Adding an PDF attachment when using Zend_Mail
Edit:
@catcon I'm assuming that the OP is using -SOME- sort of framework...but he doesn't specify and hasn't returned to clarify either. Also your comment about using a mail service to send the file doesn't really answer the question. He wants to know if he can attach the file content to the email without first saving it to disk - which my answer is: "Yes - you can. And it's easiest if you're using a framework like Zend."
If he's not using a framework and just using straight PHP mail()
, he can still build up a Content-Type: multipart/mixed
email message by setting the appropriate mail headers, and send it without having to actually persist the PDF to disk first. Example:
Assume $content is the binary string representing the PDF:
// base64 encode our content and split/newline every 76 chars
$encoded_content = chunk_split(base64_encode($content));
// Create a random boundary for content parts in our MIME message
$boundary = md5("whatever");
// Set message headers header to indicate mixed type and define the boundary string
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from."\r\n"; // Sender's email
$headers .= "Reply-To: ".$reply_to."\r\n"; // reply email
$headers .= "Content-Type: multipart/mixed;\r\n"; // Content-Type indicating mixed message w/ attachment
$headers .= "boundary = $boundary\r\n"; // boundary between message parts
// Text of the email message
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
// PDF attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: application/pdf; name=yourbill.pdf\r\n";
$body .="Content-Disposition: attachment; filename=yourbill.pdf\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: somerandomstring\r\n\r\n";
$body .= $encoded_content; // Attaching the encoded file with email
// Send the message w/ attachment content
$result = mail($recipient, $subject, $body, $headers);

- 516
- 4
- 7
-
-
-
@SETYO: fpdf just generate pdf file, it's your job to use a mail service to send the pdf file – Jan 10 '19 at 06:07