douhuangzhi0707 2013-06-11 14:29
浏览 58
已采纳

PHPExcel通过电子邮件发送文件

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条回答 默认 最新

  • dougu2036 2013-06-11 14:39
    关注

    Have you tried...

    $mail->AddAttachment($full_path_to_file, "report.xls");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?