gpt4 book ai didi

php - 在电子邮件中添加 PhpSpreadsheet 附件

转载 作者:行者123 更新时间:2023-12-05 07:21:01 27 4
gpt4 key购买 nike

我尝试制作一个 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();

这是我在查看文档后构建的 SpreadSheet。


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 的情况下发送邮件没有问题,但是当我取消注释这一行时,我的所有尝试都会抛出错误。

谢谢你的时间最好的问候

最佳答案

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

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

$path = "<Absolut Path>";

//do stuff

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

关于php - 在电子邮件中添加 PhpSpreadsheet 附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57186290/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com