在PHP中创建一个word文件并将其附加到邮件中



我创建Word文档的方式是这样的:

// I use my templace that's with my files : 
$templateProcessor = new TemplateProcessor('Template.docx');
// I fill the template values from an sql query : 
$templateProcessor->setValue('titre', $options['titre']);
$templateProcessor->setValue('source', $options['source']);
$templateProcessor->setValue('auteur', $options['auteur']);
$templateProcessor->setValue('date_pub', $options['date_pub']);
$templateProcessor->setValue('contenu', $options['contenu']);
// I give the user the file (I don't fully understand how this works but it does)
header("Content-Disposition: attachment; filename=$title.docx");
$templateProcessor->saveAs('php://output');

人们建议在php邮件中附加文件的方式如下:

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
$email = new PHPMailer();
$email->SetFrom('you@example.com', 'Your Name'); //Name is optional
$email->Subject   = 'Message Subject';
$email->Body      = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );
$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );
return $email->Send();

我有一个问题与PATH_OF_YOUR_FILE_Here我用来创建word文档的代码只是将它提供给用户,以便他们下载它,但是它的路径是什么呢?

非常感谢你的帮助,谢谢

首先需要在服务器上创建该文件,以便将其附加到电子邮件中。您可以在发送完邮件后将文件从服务器上删除。

header()php://output告诉用户的浏览器下载文件,所以如果你删除标题并将saveAs()更改为服务器上的真实(可写)路径,你应该最终得到一个可以附加到电子邮件的文件。

我建议将文件写到一个新的位置(服务器根目录之上)来存储这些临时文件,而不是放在源代码中。然后是->saveAs('/path/to/file/File.docx');$file_to_attach = '/path/to/file/File.docx';

最新更新