Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4256864
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:24:47+00:00 2026-05-21T05:24:47+00:00

In my current project, I have to create a PDF file on the fly

  • 0

In my current project, I have to create a PDF file on the fly and attach as a mail attachment and send. It works fine, the file generate and send to the email I provided. When I send it to Microsoft Outlook or Windows Live account, the PDF has attached but can’t open the file with pdf, gives and error saying, its damaged. But with gmail and yahoo it works fine. Does anybody have a solution for this. Below is my code

$dompdf = new DOMPDF();
    $dompdf->load_html($message);
    $dompdf->set_paper("a4", "landscape");
    $dompdf->render();

    // The next call will store the entire PDF as a string in $pdf
    $pdf = $dompdf->output();

    // You can now write $pdf to disk, store it in a database or stream it to the client.
    file_put_contents("pdfs/invoice.pdf", $pdf);


    $fileatt = "pdfs/invoice.pdf"; // Path to the file
    $fileatt_type = "pdf"; // File Type
    $fileatt_name = "invoice.pdf"; // Filename that will be used for the file as the attachment


    $fp = fopen($fileatt, "rb");
    $file = fread($fp, filesize($fileatt));

    $file = chunk_split(base64_encode($file));
    $num = md5(time());




    $to = "mail@mail.com";

    $subject = "Invoice";
    $headers = "From: " . "Manager" . "<" . "mail@mail.com" . ">\r\n";
    $headers  .= "MIME-Version: 1.0\r\n";
    $headers  .= "Content-Type: multipart/mixed; ";
    $headers  .= "boundary=".$num."\r\n";
    $headers  .= "--$num\r\n";

    $headers .= "Message-ID: <" . gettimeofday() . " TheSystem@" . $_SERVER['SERVER_NAME'] . ">\r\n";
    $headers .= "X-Mailer: PHP v" . phpversion() . "\r\n";



    $headers  .= "Content-Type:".$fileatt_type." ";
    $headers  .= "name=\"".$fileatt_name."\"r\n";
    $headers  .= "Content-Transfer-Encoding: base64\r\n";
    $headers  .= "Content-Disposition: attachment; ";
    $headers  .= "filename=\"".$fileatt_name."\"\r\n";
    $headers  .= "".$file."\r\n";
    $headers  .= "--".$num."\r\n";

    $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
    $headers .= "".$message."\r\n";
    $headers .= "--".$num."--";

    if (mail($to, $subject, $message, $headers)) {
        fclose($fp);
        echo "Success";
        //header("location: client.php?m=1");
    } else {
        echo "Error";
        //header("location: client.php?m=0");
    }

Hope someone can help me to solve the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:24:47+00:00Added an answer on May 21, 2026 at 5:24 am

    Building your own mime messages is never a good idea. Use PHPMailer or Swiftmailer instead. They both handle the heavy lifting of building the messages, including file attachments. Best of all, they’re both free and work far far better than the built-in PHP mail function. Your code above could be reduced to about 5 of 6 lines of mail-sending-code with either of the packages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a .Net developer, but for my current project I have to create some
In my current project, we have to create a website (ASP.NET MVC) which is
Hai, I have create a iPhone project where I need to show the current
I have a requirement on my current project (a Flex app which will be
I have a spec in my current project that requires us to advise the
I am hosting SpiderMonkey in a current project and would like to have template
I am using a Cairngorm MVC architecture for my current project. I have several
OK, another road bump in my current project. I have never had form elements
I have a fairly standard inheritance situation in my current LINQ-to-SQL project. I have
My current project involves deploying an upgraded .exe file that runs as a Windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.