PHPExcel send file in email

2019-07-31 23:23发布

I have a site where in a page I have developed an xls report. After its creation I want to send it in a mail with phpmailer. How can I attach this file to the email? This is my code:

    $objPHPExcel = new PHPExcel();

            // Set document properties
            $objPHPExcel->getProperties()->setCreator("Alessandro Minoccheri")
                                         ->setLastModifiedBy("Alessandro Minoccheri")
                                         ->setTitle("Office 2007 XLSX Test Document")
                                         ->setSubject("Office 2007 XLSX Test Document")
                                         ->setDescription("Generazione report inverter")
                                         ->setKeywords("office 2007 openxml php")
                                         ->setCategory("");

            $row=1;
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('0', $row, 'Inverter');
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('1', $row, 'Channel');
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('2', $row, 'Picco');
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('3', $row, 'Picco data');
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('4', $row, 'Media');
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow('5', $row, 'Sommatoria');
$objPHPExcel->setActiveSheetIndex(0);

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('report.xls');

               $yourName = 'Client';
        $yourEmail_2 = 'alessandro@xxx-it';

        ini_set("include_path", "../inc/phpmailer/");
        require("class.phpmailer.php");
        $mail = new PHPMailer();

        $mail->From     = 'xxx@xxx.com';
        $mail->FromName = 'Client';
        $mail->AddAddress($yourEmail_2); 
        //$mail->AddBCC($yourEmail_2); 

        /*if(!empty($_FILES['attachment']['tmp_name'])){
            $new_name = urlencode( rand(0,10000).rand(10000,20000).$_FILES['attachment']['name'] );
            if(move_uploaded_file($_FILES['attachment']['tmp_name'],'./uploads/'.$new_name)){
                $string_file = '<p>Curriculum allegato: '</p>';
            }
        }*/

        $mail->WordWrap = 50;                              // set word wrap
        $mail->IsHTML(true);                               // send as HTML

        $mail->Subject  =  'Riepilogo settimanale';

        $mail->Body     =  "Riepilogo settimanale dell'impianto: ".utf8_decode($inv['name']);

        $mail->AltBody  =  "Riepilogo settimanale dell'impianto: ".utf8_decode($inv['name']);

        if ( $mail->Send()){
            //ok
        }

2条回答
三岁会撩人
2楼-- · 2019-07-31 23:53
**Code to create excel in php:**
$dtime=date('Y-m-d H-i-s');
$dtimeFile=date('Y-m-d-H-i-s');
$headerTab ="Col1 \t Col2\t Col3\n";
$rowRecords='';
$rowRecords .=preg_replace("/\r|\n|\t/"," ",$Col1)."\t".preg_replace("/\r|\n|\t/", " ",$Col2)."\t".preg_replace("/\r|\n|\t/", " ",Col3). "\t\n";
date_default_timezone_set('America/Los_Angeles');
$filename="Your File Name-".$dtimeFile.".xls";
$path='/pathOfFile/';
$csv_handler = fopen ($path.$filename,'w');
fwrite ($csv_handler,$headerTab);
fwrite ($csv_handler,$rowRecords);
fclose ($csv_handler);

**Code to send html email with attached excel in php:**
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$headers = "From: from@gmail.com"."\r\n";
$headers.= "Bcc: bcc@gmail.com"."\r\n";
$headers.= "MIME-Version: 1.0\r\n";
$headers.= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= $msg."\r\n\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$headers .= $content."\r\n\r\n";
$headers .= "--".$uid."--"; 

$date=date("Y-m-d");
if(mail($to,"Mail heading--".$date,$msg,$headers)){
    echo "Mailed successfully";
}
else
{
    echo "Mailed couldn't be sent"; 
}
查看更多
神经病院院长
3楼-- · 2019-07-31 23:55

Have you tried...

$mail->AddAttachment($full_path_to_file, "report.xls");
查看更多
登录 后发表回答