yii框架中使用phpexcel得到的excel文件作为附件发送

来源:互联网 发布:虽知世故可请你善良 编辑:程序博客网 时间:2024/06/06 02:10
1、使用mail:private $subject = '测试';   //邮件主题private $html = '<strong>Hello</strong>'; //邮件发送html内容private $content_type = 'application/vnd.ms-excel';private $attach_file = '';      //邮件要发送的附件private $attach_file_path = '';      //邮件要发送的附件保存路径private $attach_file_name = ''; //附件名称private $attach_file_type = '.xlsx'; //附件类型private $attach_title = [];   //附件标题private $attach_data = []; //附件内容Yii::$app->mailer->compose()            ->setTo($this->send_to)            ->setSubject($this->subject)            ->attachContent($this->attach_file, ['fileName' => $this->attach_file_name . $this->attach_file_type, 'contentType' => $this->content_type])            ->setHtmlBody($this->html)            ->send(); 这里的$this->attach_file,yii官方规定是string类型的,那么作为一个文件,读取成文件流即可。 //首先读取数据,得到一个excel文件保存到根目录下。 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $this->attach_file_path = dirname(__DIR__). '/web/'. date('YmdHis').'.xlsx'; $objWriter->save($this->attach_file_path);  //保存,发送邮件之后再删除 $this->getExcel($this->attach_title, $this->attach_data);        if (file_exists($this->attach_file_path)) {            $file = fopen($this->attach_file_path, 'rb');//二进制读取            $this->attach_file = fread($file, filesize($this->attach_file_path));            fclose($file);        }发送完邮件之后,如果文件存在,就删除这个文件。if (file_exists($this->attach_file_path)) unlink($this->attach_file_path);//删除文件
原创粉丝点击