title: sending php excel as attachment using php mail
body: <p>I have the following:  </p>
  
  <pre><code>$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  ob_start();
  $objWriter-&gt;save("php://output");
  $xlsData = ob_get_contents();
  $xlsData1= "data:application/vnd.ms-excel;base64,".base64_encode($xlsData);
  ob_end_clean();
  
  
          //Require for mailing functionality
          require_once('../../PHPMailer/PHPMailer-master/class.phpmailer.php');
  
          //Send mail function
          $mail= new PHPMailer(); // defaults to using php "mail()"
          $sender ="emailadddress";
          $mail-&gt;SetFrom($sender);
          $mail-&gt;AddAddress('emailaddress');   
          $mail-&gt;Subject="Combined repair and production report from ".$reportDate;
          $encoding = "base64"; 
          //$type = "image/png";
          $mail-&gt;AddStringAttachment(base64_decode($xlsData1), "combined_report".$reportDate."xlsx");  
          //$mail-&gt;AddAttachment($xlsData, $filename);
          $message= "Please see attached combined report";
          $message = "&lt;/body&gt;&lt;/html&gt;";              
          $mail-&gt;MsgHTML($message);    
  
          if(!$mail-&gt;Send()) 
          {
              $response =  array(
                  'op' =&gt; 'failed',
                  'file' =&gt; "data:application/vnd.ms-excel;base64,".base64_encode($xlsData)
              );
          } 
          else 
          {
              $response =  array(
                  'op' =&gt; 'ok',
                  'file' =&gt; "data:application/vnd.ms-excel;base64,".base64_encode($xlsData)
              );
  
          } 
  
  
  
  
  
  die(json_encode($response));
  pg_close($conn);
  </code></pre>
  
  <p>this sends an email with an attachemnt but the attachment is a blank file. When I try to save it it has the filename and the extension like this: "filenamexlsx" so it looks like it is not getting the correct xlsx format. I have tried this too:</p>
  
  <pre><code>$mail-&gt;AddStringAttachment("data:application/vnd.ms-excel;base64,".base64_encode($xlsData);  
  </code></pre>
  
  <p>I have also tried: </p>
  
  <pre><code>    $encoding = "base64"; 
      $type = "application/vnd.ms-excel"; // and :application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
      $mail-&gt;AddStringAttachment(base64_decode($xlsData1), "combined_report".$reportDate."xlsx",$encoding,$type);
  </code></pre>
  
  <p>I also tried using the <code>AddAttachment()</code> instead of the string attachment and there is no attachment on the email when I do that.</p>
  
  <p>I have followed :
   <a href="https://stackoverflow.com/questions/3092821/php-send-e-mail-with-attachment?noredirect=1&amp;lq=1">php send e-mail with attachment</a></p>
  
  <p>and have also looked at multiple things out there like <a href="https://stackoverflow.com/questions/1201955/phpmailer-addstringattachment-and-data-uri-scheme/1202577#1202577">PHPMailer, AddStringAttachment and Data URI Scheme</a></p>
  
  <p>but it doesnt appear to be working :( any help will be appreciated!</p>