-1

I have the following:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_start();
$objWriter->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->SetFrom($sender);
        $mail->AddAddress('emailaddress');   
        $mail->Subject="Combined repair and production report from ".$reportDate;
        $encoding = "base64"; 
        //$type = "image/png";
        $mail->AddStringAttachment(base64_decode($xlsData1), "combined_report".$reportDate."xlsx");  
        //$mail->AddAttachment($xlsData, $filename);
        $message= "Please see attached combined report";
        $message = "</body></html>";              
        $mail->MsgHTML($message);    

        if(!$mail->Send()) 
        {
            $response =  array(
                'op' => 'failed',
                'file' => "data:application/vnd.ms-excel;base64,".base64_encode($xlsData)
            );
        } 
        else 
        {
            $response =  array(
                'op' => 'ok',
                'file' => "data:application/vnd.ms-excel;base64,".base64_encode($xlsData)
            );

        } 





die(json_encode($response));
pg_close($conn);

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:

$mail->AddStringAttachment("data:application/vnd.ms-excel;base64,".base64_encode($xlsData);  

I have also tried:

    $encoding = "base64"; 
    $type = "application/vnd.ms-excel"; // and :application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    $mail->AddStringAttachment(base64_decode($xlsData1), "combined_report".$reportDate."xlsx",$encoding,$type);

I also tried using the AddAttachment() instead of the string attachment and there is no attachment on the email when I do that.

I have followed : php send e-mail with attachment

and have also looked at multiple things out there like PHPMailer, AddStringAttachment and Data URI Scheme

but it doesnt appear to be working :( any help will be appreciated!

Community
  • 1
  • 1
cocopan
  • 109
  • 3
  • 19
  • 3
    `$reportDate."xlsx"` should be `$reportDate.".xlsx"` – Funk Forty Niner Mar 29 '17 at 17:26
  • well yours was not letting me accept as an answer and I didn't want others stopping for such a small mistake. sorry I am kinda new with posting quesitons etc. – cocopan Mar 29 '17 at 17:35
  • Would you happen to know why I still get an error saying that the file is corrupt? can you help me spot any other errors? – cocopan Mar 29 '17 at 17:39
  • I figured it out thanks... Just in case it helps anybody else in the future you have to **'$xlsData = ob_get_contents(); $contact_image_data="data:application/vnd.ms-excel;base64,".base64_encode($xlsData); $data = substr($contact_image_data, strpos($contact_image_data, ","));** and then **$mail->AddStringAttachment(base64_decode($data),"combined_report_.xlsx",$encoding,$type);** – cocopan Mar 29 '17 at 17:55

1 Answers1

0

I think there is mistake in your file name. Your file name should be filename.xlsx not this filenamexlsx

Kinjal Modi
  • 56
  • 1
  • 9