【问题标题】:Add PhpSpreadsheet attachement in a mail在电子邮件中添加 PhpSpreadsheet 附件
【发布时间】:2019-12-02 19:21:27
【问题描述】:

我尝试制作一个 PhpSpreadsheet 文档,然后将他添加到邮件附件中。也许是热,但在 phpSpreadsheet 文档中几个小时后我什么也没找到。

这是我发送邮件的文件

$name = 'export-sst';
$filename = FileAssociatedPeer::getNewTempFilename($name . '.xlsx"');
$filepath = sfConfig::get('sf_upload_dir') . sfConfig::get('app_sfPropelFileAssocPlugin_temp_dir') . '/' . $filename;

include dirname(__FILE__) . 'template.php';

$mail = new PHPMailerBootstrap();
$mail->initialize();
$mail->setCharset('utf-8');
$mail->setContentType('text/html');

$mail->setMailer('smtp');
$mail->setPort((int) sfConfig::get('app_send_mail_port'));
$mail->setHostname(sfConfig::get('app_send_mail_host'));
$mail->setUsername(sfConfig::get('app_send_mail_username'));
$mail->setPassword(sfConfig::get('app_send_mail_password'));
$mail->setSender('cyril@quarks.pro');
$mail->setFrom('cyril@quarks.pro', 'Logiciel Quarks');

$subject = 'test';

$mail->setSubject($subject);
$body = <<<EOT
Test
EOT;
$mail->setBody(nl2br($body));
$mail->addAddress('test@talala.com');

$mail->addAttachment($spreadsheet, $name . '.xlsx', 'base64', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

$mail->send();

查看文档后,这里是我的电子表格结构。


use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Style;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;

$spreadsheet = new Spreadsheet();
$active_sheet = $spreadsheet->getActiveSheet();
$active_sheet->setTitle('Tableau général');

$active_sheet->setCellValue('A1', 'Hello World !');
$active_sheet->setCellValue('B1', 'tada');


header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'. $filename .'.xlsx"');
header('Cache-Control: max-age=0');

$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');

我在没有 addAttachement 的情况下发送邮件没有问题,但是当我取消注释这一行时,我所有的尝试都会抛出错误。

感谢您的宝贵时间 最好的问候

【问题讨论】:

  • 您正在创建一个电子表格并将其发送到浏览器。您需要将其保存到一个文件中,然后该文件可能会附加到您发送的邮件中。
  • 我想了很多。像``` $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->保存($文件路径); ``` 和 ``` $mail->addAttachment($filepath, $name . '.xlsx', 'base64', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); ```我有另一个错误无法关闭 zip 文件 /var/...tmp1563982511-8751#export-sst.xlsx"
  • 您在正确的轨道上,但听起来您还有其他问题需要解决,将电子表格生成为文件或字符串,然后再将其附加到电子邮件- 先解决第一个问题!
  • 经过几次新尝试后,我遇到了一个新错误。警告:无法修改标头信息 - 标头已由(输出已开始)我尝试通过下载来制作我的文档,我没有问题
  • 它的工作。我在 tmp 文件夹中使用正确的权限解决了我的问题。谢谢大家的帮助

标签: php phpmailer phpspreadsheet


【解决方案1】:

无法关闭 zip 文件错误表明您对目标文件路径没有正确的权限。

尝试使用绝对路径以确保您拥有正确的路径。

$path = "<Absolut Path>";

//do stuff

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($path); 

【讨论】:

  • 我的路很好。我在我的文件夹中以正确的权限解决了我的问题。谢谢你的时间。祝你有美好的一天
猜你喜欢
  • 2011-08-04
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
  • 2023-03-22
  • 2023-03-21
相关资源
最近更新 更多