I am trying to create a pdf document using mpdf and in the first step I tried to save it in my downloads folder and then I tried to open it and it worked perfect. I can see what I need in the pdf document.
In the next step I tried to send the pdf as an attachment and the email was sent successfully with the pdf attachment but when I tried to open it from the mail its returning me an error something as "Adobe cannot open the pdf document because it is neither a supported file type or the file is damaged.......".
I searched for different questions in stackoverflow and I tried their valuable answers too but none of them helped me.
The below is the same question but the answer posted there too didn't helped me.
Stackoverflow questions related to my post
Few Links Which I followed
Here's my code:
$mpdf=new mPDF( );
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0;
$mpdf->WriteHTML($html,2);
ob_clean(); // **Tried this from Stackoverflow answers**
$mpdf->Output($filename.'.pdf', 'I');
$mailto = $row['Email1'].",".$row['Email5'];
$path = 'C:\\Users\\Downloads\\';
$file = $path."/".$filename;
$subject = 'Test Email';
$message = 'Test <br> Case';
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$separator = md5(time());
$eol = PHP_EOL;
$headers = "From: John <john.xxxxx#gmail.com>".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"".$eol.$eol;
// message
$body .= "Content-Transfer-Encoding: 7bit".$eol;
$body .= "This is a MIME encoded message.".$eol;
$body = "--" . $separator.$eol;
$body .= "Content-Type: text/html; charset=\"UTF-8\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--" . $separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . '.pdf' . "\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol ;
$body .= $content.$eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
//echo "mail send ... OK"; // or use booleans here
}
Have you tried changing the mime-type you set for the attachment?
The standard mime-type for pdf files is:
application/pdf
I noticed you are trying to load a file from local file system. Does the script run on the same machine? Or on a webserver?
If it runs on a webserver you might need to upload the file to the webserver first. You'll need to change the $path and $file variables once uploaded.
$path = 'www/images/[some-path]';
$file = '/'.$filename;
If it runs on the same machine as where the file is located, you might need to update the $file variable so it uses a backslash \ instead of the forward slash.
$path = 'C:\\Users\\Downloads\\';
$file = $path.'\\'.$filename;
Another error in your script: You never set the mpdf contents as the content for the email attachment. Try something like:
$content = chunk_split(base64_encode($mpdf->Output($filename.'.pdf', 'I')));
You'll have to check if the I in the Output() function of mpdf is correct for this use. I'm not sure of that.
EDIT
// First save it to the server somewhere
$mpdf->Output($filename.'.pdf', 'F'); // Make sure the path is valid and writing permissions are set correctly to prevent writing errors.
// Then get the contents from the PDF
$content = chunk_split(base64_encode($mpdf->Output($filename.'.pdf', 'S')));
Related
i'm used fpdf for create pdf with php. everything are ok. but i want to send pdf file to outlook.
I'm using $pdf->Output('file.pdf','D');
it's running. I want to attachment this file to microsoft outlook.
How i can this ?
Something you can do is save the file to a temporary location on the server, and then use something like PHPMailer to attach that saved file to the email. PHPMailer is a lot easier to use for attachments than PHP's built-in mail function.
You can temporarily store your PDF file a number of ways. Here's one:
$tempfilename = time().'.pdf';
$pdf->Output($tempfilename,'F');
Then in PHPMailer, you can attach it as such:
$mail->addAttachment($tempfilename);
And then after you are done, you can remove the temporary file from the server.
unlink($tempfilename);
If PHPMailer is not possible to use for your situation for whatever reason, you can use PHP's built-in mail function. If you're working from a fresh file or a small file where the cost of adding PHPMailer is relatively small, do that if you can. Otherwise, you may try adding code like this to your $headers. Adapted from an answer on using mail to send attachments:
// Generate a random hash to send mixed content
$sep = md5(time());
// End of line
$eol = PHP_EOL;
// Content of file
$content = file_get_contents($tempfilename);
$content = chunk_split(base64_encode($content));
// Add attachment to headers
$headers .= "--" . $sep . $eol;
$headers .= "Content-Type: application/octet-stream; name=\"" . $tempfilename . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: base64" . $eol;
$headers .= "Content-Disposition: attachment" . $eol . $eol;
$headers .= $content . $eol . $eol;
$headers .= "--" . $sep . "--";
I'm trying to send an attachment via email but, even though the right file is saved in the server, the one attached to the email is empty (0 kb).
I'm using gmail to send the emails.
Here is the relevant part of my code:
if (empty($error)) {
//boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x$semi_randx";
//tell the headers about the boundary
$header .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"$mime_boundary\"";
//define the first part of the email, which is the text part
$message = "\r\n" . "--$mime_boundary\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\r\n" ;
//build the body of the 1st part of the email
$content_body = "
Email del formulario de contacto en ".home_url().": <br />
//whatever
";
$message .= $content_body . "\r\n";
$message .= "--$mime_boundary\n";
//define the second part of the email, which is the atachments
//if a file has been uploaded
if (!empty($_FILES['cv']['name'])){
// Open the file for a binary read
$file = fopen($temp_name,'rb');
// Read the file content into a variable
$data = fread($file,filesize($temp_name));
// close the file
fclose($file);
// Now we need to encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
// Actually build the second part of the email
$message .= "Content-Type: \"application/octet-stream\";\r\n name=\"" . $file_name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\r\n filename=\"" . $file . "\"\r\n\r\n";
$message .= $data; //The base64 encoded message
$message .= "\n";
$message .= "--$mime_boundary--\n";
}
//send th email
mail( $receive_email, "Email del formulario de contacto en web", $message, $header);
$msg = $succesful_text;
}
My guess is that I'm doing something wrong here:
$message .= "Content-Type: \"application/octet-stream\";\r\n name=\"" . $file_name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\r\n filename=\"" . $file . "\"\r\n\r\n";
$message .= $data; //The base64 encoded message
$message .= "\n";
$message .= "--$mime_boundary--\n";
But I have no idea what it can be.
I know I can use a library, but I would like to see what is wrong with my code just for the sake of learning.
Any help will be highly appreciated.
Sonia
Thanks Cherry and miken32 for your replies.
Finally I figured it out.
This is what happened, in case it can help somebody else in a future.
As there was no base64 encoded data in the text message, I thought the problem might no be in the $message text but in the actual handling of the file.
// Open the file for a binary read
$file = fopen($temp_name,'rb');
// Read the file content into a variable
$data = fread($file,filesize($temp_name));
// close the file
fclose($file);
// Now we need to encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
I realized the temp file was not being opened. I couldn't see why but I solved it by handling directly the file from the uploads folder.
Here is the fixed code:
// Open the file for a binary read
$file = fopen($server_file,'rb');
// Read the file content into a variable
$flsz=filesize($server_file);
$data = fread($file,$flsz);
// close the file
fclose($file);
// Now we need to encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
I also had to modify the filename in the $message text, using $file_name instead of $file because $file.
$message .= "Content-Disposition: attachment;\r\n filename=\"" . $file_name . "\"\r\n\r\n";
So I'm trying to generate and save an Excel-file to the server. This works flawlessly with PHPExcel. The next step is to read the file, e-mail it as an attachment and then delete it.
For some reason, PHP does not recognize xlsx as a proper file:
/* excel is generated before here */
$filename = "/results/excel/export-" . $today . ".xlsx";
$writer = new PHPExcel_Writer_Excel2007($exc);
$writer->save($filename);
/* so far so good; the file is created and exists on the server */
$to = "someone#domain.com";
$sendermail = "no-reply#domain.com";
$from = "Sender <" . $sendermail . ">";
$subject = "Email with attachment";
$message = "Here you go";
$file = $filename;
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "From: $from";
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\" {$mime_boundary}\"";
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
if(is_file($file)) {
$message .= "--{$mime_boundary}\n";
$fp = #fopen($file, "rb");
$data = #fread($fp, filesize($file));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"" . basename($file). ";\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $sendermail;
mail($to, $subject, $message, $headers, $returnpath);
} else {
echo("This is not a file: " . $file);
}
Which always gives me "This is not a file...". How do I read the XLSX-file?
here's a tip to prompt the excel and also send the file as an attachment without saving the file on server:
$objWriter = PHPExcel_IOFactory::createWriter ($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
$data = ob_get_contents();
$sendmail=new ExcelMail($data); //this is your mail class; if you use zend mail use addattachement($data); and Zend_Mime
The outputted message means is_file() is failing . This can happen for a number of reasons, but as far as I know not because the file format. As such, I would recommend looking into the following things:
Is the file actually there? You say you have the file saved, but you have to make sure it actually is.
Is the file path the xslx file is using correct?
Is the file path to the xslx file to the actual xml file and not to a symlink to it?
Are the permissions of the xslx file set so that the php script is allowed to acces them?
I believe it has to be one of those problems...
Okay, I found out what it was. $filename was actually a direct (http://...) link to the file, not a relative link. Changed it to be relative and it worked :).
I'm trying to generate a VCard via PHP, and them email it out to the user. I wrote an initial script with hard-coded data, but the end-result will fill in the VCard from MySQL.
When viewing the VCard side-by-side with a legitimate VCard (downloaded and tested from another site) they look pretty much identical, but when I try and import my generated VCard it shows up with no data. Actually, if I open it on my phone, it doesn't even recognize that it is a vcard, and instead just sends me to a broken Google Doc.
I've borrowed some code from wikipedia for formatting the vcard, and everything seems fine. Do you see any errors in my formatting? I've tried different line breaks - to no avail. Ideas?
Here is the code for my generation / mail:
<?php
$content = "BEGIN:VCARD\r";
$content .= "VERSION:3.0\r";
$content .= "CLASS:PUBLIC\r";
$content .= "FN:Joe Wegner\r";
$content .= "N:Wegner;Joe ;;;\r";
$content .= "TITLE:Technology And Systems Administrator\r";
$content .= "ORG:Wegner Design\r";
$content .= "ADR;TYPE=work:;;21 W. 20th St.;Broadview ;IL;60559;\r";
$content .= "EMAIL;TYPE=internet,pref:__munged__#wegnerdesign.com\r";
$content .= "TEL;TYPE=work,voice:__munged__\r";
$content .= "TEL;TYPE=HOME,voice:__munged__\r";
$content .= "URL:http://www.wegnerdesign.com\r";
$content .= "END:VCARD";
mail_attachment("Joe Wegner.vcf", $content, "__munged__#wegnerdesign.com", "__munged__#wegnerdesign.com", "Wegner Design Contacts", "__munged__#wegnerdesign.com", "Joe Wegner's Contact Info", "");
function mail_attachment($filename, $content, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$fileatt_type = "application/octet-stream";
$headers = "FROM: ".$from_mail;
$data = chunk_split(base64_encode($content));
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$filename}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
echo "sending message";
mail($mailto, $subject, $message, $headers);
}
?>
Update 1: This makes me more confused, but perhaps it will help you debug. I've downloaded my (bad) generated VCard to my computer, as well as a good VCard downloaded from a different website. As expected, my generated one opens with no data, but the good one works fine. I then created a third empty file with a .vcf extension, and copied the text from my (bad) file into that empty file. I opened that file, and all the data showed perfectly. To test even further, I copied the text from the good VCard file into my bad file, and it still opened with no data. So, it appears it's something about encoding or some other file thing that I don't understand. It's not permissions - that's all identical.
Update 2: I changed my PHP so that it would force me to download the VCard as well as email it. The downloaded file opens perfectly fine, so the error is either happening in how I'm encoding (right word?) the file, or how GMail is interpretting it.
Update 3: Fixed : Figured it out. I'm not sure why this is - because every other tutorial I can find out there says the opposite - but there were a few key changes. First, I changed the encoding on the email from base64 to 8bit, and I changed the attachment content to just be the string passed to the email function (so that it is in 8bit form, not 64). That made the VCard valid and readable on my desktop. To get it to read on my android I had to change the $fileatt_type variable to "text/x-vcard", otherwise Gmail thinks it is a document.
You are missing a \n at the end of each line. If you look at a normal vCard (with notepad++ for example), it has a CR and LF at the end of each line, the one you create only has CR ('\r'). this works for me:
$content = "BEGIN:VCARD\r\n";
$content .= "VERSION:3.0\r\n";
$content .= "CLASS:PUBLIC\r\n";
$content .= "FN:Joe Wegner\r\n";
$content .= "N:Wegner;Joe ;;;\r\n";
$content .= "TITLE:Technology And Systems Administrator\r\n";
$content .= "ORG:Wegner Design\r\n";
$content .= "ADR;TYPE=work:;;21 W. 20th St.;Broadview ;IL;60559;\r\n";
$content .= "EMAIL;TYPE=internet,pref:joe#wegnerdesign.com\r\n";
$content .= "TEL;TYPE=work,voice:7089181512\r\n";
$content .= "TEL;TYPE=HOME,voice:8352355189\r\n";
$content .= "URL:http://www.wegnerdesign.com\r\n";
$content .= "END:VCARD\r\n";
I had to get vcards working in android and iphone recently. I used the following function which is a modified version of the one listed above. This will send vcards that both gmail and mail on the iphone will be able to open. They work in Thunderbird as well.
function mail_attachment($filename, $content, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$fileatt_type = "text/x-vcard";
$headers = "FROM: ".$from_mail;
$data = $content;
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$filename}\"\n" .
"Content-Transfer-Encoding: 8bit\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$filename}\"\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
//echo "sending message";
mail($mailto, $subject, $message, $headers);
}
I wrote a vCard validator based on the RFC which could help. It's not complete, but the cleaned files are at least nicely compatible with the tools and services I've tried (Gmail, gnokii and some others I can't remember). HTH.
I've recently created an online template for creating job postings for our website. Everything is all done, it formats correctly in a browser, automatically posts to our website, bla bla bla.
The last piece I'm creating is to give the administrator a few options for distributing the posting to various places (via email) in a consistent, convenient way. I've created a PHP page that creates a PDF doc on the fly, using the TCPDF library. When loading pdf.php?id=X, the page displays a PDF with the content of job posting X. This means I'm never saving the PDF file to the server, just creating it on the fly each time it's called.
But I want to attach this PDF to an email, and send it to various colleges, and internal mailing lists, etc. If I attach the pdf.php?id=x to the email, it doesn't attach the PDF, it attaches what appears to be a blank file, with the above name.
Is it possible to attach this to the email without saving it to the server?
Below added based on JM4's response for further trouble shooting. I have put the PDF file creation into a function, and put it into an include file, just to keep things easier to manage.
// random hash necessary to send mixed content
$separator = md5(time());
$eol = PHP_EOL;
// attachment name
$filename = "_Desiredfilename.pdf";
include_once('pdf.php');
// encode data (puts attachment in proper format)
$pdfdoc = job_posting_to_pdf($posting_id);
$attachment = chunk_split(base64_encode($pdfdoc));
///////////HEADERS INFORMATION////////////
// main header (multipart mandatory) message
$headers = "From: Sender_Name<valid_email#mydomain.com>".$eol;
//$headers .= "Bcc: email#domain.com".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
//Email message
if(mail('valid_email#mydomain.com', 'test job posting', 'message body goes here', $headers)) {
echo 'mail sent';
} else {
echo 'error in email';
}
Here is a stripped down version of pdf.php:
function job_posting_to_pdf($job_id) {
require_once(ROOT . 'assets/libs/tcpdf/config/lang/eng.php');
require_once(ROOT . 'assets/libs/tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('');
$pdf->SetTitle('OPL Job Posting');
$pdf->SetSubject('Job Posting');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(11, PDF_MARGIN_TOP, 11);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
$pdf->SetFont('times', 'I', 9);
$pdf->AddPage();
$left_cell_width = 60;
$row_height = 6;
$pdf->Image(ROOT . 'assets/gfx/logos/OPL-Logo.jpg', 0, 5, null, 16, null, null, 'N', false, null,'R');
$pdf->Ln('3');
if(!$row['internal']) {
$pdf->Cell(0,0,'This position will be posted internally and externally, concurrently.',0,2,'C');
} else {
$pdf->Cell(0,0,'Internal posting only.',0,2,'C');
}
//Remainder of actual PDF creation removed to keep things simple
return $pdf->Output("", "S");
}
If I completely understand what you are asking this is quite simple. I am assuming you already have the PDF generated using something like fdpf or tcpdf. In that case - simply use the following code:
<?php
// random hash necessary to send mixed content
$separator = md5(time());
$eol = PHP_EOL;
// attachment name
$filename = "_Desiredfilename.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
///////////HEADERS INFORMATION////////////
// main header (multipart mandatory) message
$headers = "From: Sender_Name<sender#domain.com>".$eol;
$headers .= "Bcc: email#domain.com".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
//Email message
mail($emailto, $emailsubject, $emailbody, $headers);
?>
I just had to figure this out and my eyeballs were definitely sore by the end...
1) You need to install PHPMailer to the php server.
2) Include the PHPmailer class in your TCPDF script, like so (your path may vary):
require_once('../PHPMailer_v5.1/class.phpmailer.php');
3) Now after your pdf code simply talk to PHPMailer like so:
$filename = "custompdf_$name_$time.pdf";
$pdf->Output($filename, 'F'); // save the pdf under filename
$mail = new PHPMailer(); $mail->IsSMTP();
$mail->Host = "mail.yourhost.com";
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "user+yourhost.com"; // SMTP account username
$mail->Password = "topsecret"; // SMTP account password
$mail->From = "noreply#yourhost.com";
$mail->FromName = "Stack Overflower";
$mail->AddAddress( $email, $name ); // in this case the variable has been passed
$mail->AddCC( "person#somehost.net", "Johnny Person"); // in this case we just hard code it
$mail->SMTPDebug = 0; // use 2 for debugging the email send
$pdf_content = file_get_contents($filename);
$mail->WordWrap = 50;
$mail->AddStringAttachment($pdf_content, "custompdf_for_$name_$time.pdf", "base64", "application/pdf"); // note second item is name of emailed pdf
$mail->IsHTML(true);
$mail->Subject = "Your pdf is here";
$mail->Body = "Dear $name,<br>
Here is your custom generated pdf generated at $t.<br><br>
Thank you";
if(!$mail->Send()) {
echo "Sorry ... EMAIL FAILED";
} else { echo "Done. . . Email sent to $email at $t."; }
unlink($filename); // this will delete the file off of server
Of course you have many options for the email sent, like not using html, or sending both html and text, adding many recipients and/or cc's, etc.
EDIT: This does save the file temporarily on the server, but it cleans up after itself with the unlink command.
Take a look at this page which discusses advanced email in PHP.
http://articles.sitepoint.com/article/advanced-email-php/5
They take an uploaded file and load the binary data into $data, but you can just start from there.
You may also want to look at sending it as an attachment via PEAR Mail_Mime. It can accept an attachment as a string of data.
The RMail package also looks as if it will do the same via the stringAttachment class. You'll have to google for it, because I'm a new user and so I can post only one link at a time.
I agree about using a mail library in place of building mime messages by hand with the default mail() function. SwiftMailer is another good open source PHP mail library. Here's the sample code for using dynamic content as an attachment without having to save it to the file system first.
Your headers seem to be a little out:
application/octet-stream should become application/octetstream
Content-Disposition: attachment .. should become Content-Disposition: attachment; filename="' . basename($filename) . '"
heres what the attachement headers should look like:
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octetstream;".$eol; //Fixed
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= 'Content-Disposition: attachment; filename="' . basename(filename).'"'.$eol;
$headers .= 'Content-ID: <' . basename($filename) . '>' . $eol . $eol //EOL X2 Before
$headers .= $attachment;
//Run the above in a loop for multiple attachments, after add the final line
$headers .= '--' . $separator . '--' . $eol;
This was taken from one of my working applications, heres the loop if you wish to see it:
foreach ($this->attachments as $attachment) {
if (file_exists($attachment['file'])) {
$handle = fopen($attachment['file'], 'r');
$content = fread($handle, filesize($attachment['file']));
fclose($handle);
$message .= '--' . $boundary . $this->newline;
$message .= 'Content-Type: application/octetstream' . $this->newline;
$message .= 'Content-Transfer-Encoding: base64' . $this->newline;
$message .= 'Content-Disposition: attachment; filename="' . basename($attachment['filename']) . '"' . $this->newline;
$message .= 'Content-ID: <' . basename($attachment['filename']) . '>' . $this->newline . $this->newline;
$message .= chunk_split(base64_encode($content));
}
}
$message .= '--' . $boundary . '--' . $this->newline;