What is the correct way to generate an email attachment with PhpWord?
I tried multiple ways but I get a corrupt file with size 1kb. If I trigger a download of the file instead, the file is OK.
$fileName = 'file.docx';
$fileAttachment = tempnam(sys_get_temp_dir(), 'PHPWord');
$template->saveAs($fileAttachment);
$contentType = "application/octet-stream";
$notification
->setTemplate("template", $templateParams)
->setSubject($this->_("Email subject"))
->addRecipient("to", $email, $email)
->addAttachment($fileName, $fileAttachment, $contentType);
What am I doing wrong?
Thank you!
2
Answers
You could try to use file_get_contents function to read the contents of the file and then pass it as the second parameter to the
addAttachment()
method:PHPWord is for generating Word documents, but it does not send mails.
You can combine it with any PHP mailer library. This is a sample on how to do it using SwitfMailer:
You can use any other mail library you like. Or even use the PHP mail() function, but is more tricky to set the mail headers properly.