title: Send email with auto / dynamically generated PDF as attachment in PHP
body: <p>I'm trying to send an email with an auto / dynamically generated PDF as an attachment in PHP. I am trying to achieve this with the code below, but it doesn't work.</p>
  
  <pre class="lang-php prettyprint-override"><code>    &lt;?php
  require('fpdf.php');
  
  class PDF extends FPDF
  {
      // Page header
      function Header()
      {
          // Logo
          //$this-&gt;Image('logo.jpg',10,6,30);
          // Arial bold 15
          $this-&gt;SetFont('Arial','B',15);
          // Move to the right
          $this-&gt;Cell(80);
          // Title
          $this-&gt;Cell(60,10,'Convert HTML TO PDF',1,0,'C');
          // Line break
          $this-&gt;Ln(20);
      }
  
      // Page footer
      function Footer()
      {
          // Position at 1.5 cm from bottom
          $this-&gt;SetY(-15);
          // Arial italic 8
          $this-&gt;SetFont('Arial','I',8);
          // Page number
          $this-&gt;Cell(0,10,'Page '.$this-&gt;PageNo().'/{nb}',0,0,'C');
      }
  }
  
  // Instanciation of inherited class
  $pdf = new PDF();
  $pdf-&gt;AliasNbPages();
  $pdf-&gt;AddPage();
  $pdf-&gt;SetFont('Times','',12);
  
  $pdf-&gt;Cell(0,10,'Name : ',0,1);
  $pdf-&gt;Cell(0,10,'Email : ',0,1);
  $pdf-&gt;Cell(0,10,'Mobile : ',0,1);
  $pdf-&gt;Cell(0,10,'Comment : ',0,1);
  $pdf-&gt;Output("filename.pdf","F");
  $pdfdoc = $pdf-&gt;Output("", "S");
  //$attachment = chunk_split(base64_encode($pdfdoc));
  
  $email_to      = "xxx@gmail.com"; // The email you are sending to (example)
  $email_from    = "xxx@outlook.com"; // The email you are sending from (example)
  $email_subject = "subject line"; // The Subject of the email
  $email_txt     = "text body of message"; // Message that the email has in it
  $fileatt_type  = "application/pdf"; // File Type
  $fileatt_name  = "filename.pdf"; // Filename that will be used for the file as the attachment
  $semi_rand     = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  $headers       = "From: NameHere"; // Who the email is from (example)
  $headers      .= "\nMIME-Version: 1.0\n" ."Content-Type: multipart/mixed;\n" ." boundary=\"{$mime_boundary}\"";
  $email_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" . $email_txt;
  $email_message .= "\n\n";
  $data = chunk_split(base64_encode($pdfdoc));
  $email_message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type};\n" ." name=\"{$fileatt_name}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$data . "\n\n" . "--{$mime_boundary}--\n";
  ;
  
  if(mail($email_to,$email_subject,$email_message,$headers))
  {
      echo "File Sent Successfully.";
      //unlink($attachment); // delete a file after attachment sent.
  }
  else
  {
      die("Sorry but the email could not be sent. Please go back and try again!");
      echo "File Not Sent .";
  }
  }
  
  ?&gt;
  </code></pre>
  
  <p>Is there any mistake in my method?</p>