2017-03-29 106 views
-1

我有以下Excel作爲附件:發送PHP的使用PHP郵件

$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); 

此發送電子郵件與attachemnt但附件是一個空白文件。當我嘗試保存它時,它有這樣的文件名和擴展名:「filenamexlsx」,因此它看起來沒有得到正確的xlsx格式。我已經試過這也:

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

我也曾嘗試:

$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); 

我也使用AddAttachment()而不是字符串連接嘗試,並沒有對我做的電子郵件沒有附件。

我跟了: php send e-mail with attachment

和也看了許多東西有像PHPMailer, AddStringAttachment and Data URI Scheme

但它不似乎工作:(任何幫助將不勝感激

+3

'$ reportDate。「xlsx」'應該是'$ reportDate。「。xlsx」' –

+0

你的意思並不是讓我接受爲答案,我不希望別人因爲這樣一個小錯誤而停下來。對不起,我有點新發布quesitons等 – cocopan

+0

你會碰巧知道爲什麼我仍然得到一個錯誤,說該文件是腐敗?你能幫我發現其他的錯誤嗎? – cocopan

回答

0

我的想法!你的文件名應該是filename.xlsx不是這個filenamexlsx

+0

jeez,你是對的愚蠢的PHP語法扔我離開...謝謝感謝幫助! – cocopan