simgy
February 23, 2023, 8:17pm
1
Good evening,
I am struggling with creating and attaching this one to an email.
Both part work individually but not in one function and I have no idea how to solve this properly.
file_put_contents($page->contentFileDirectory() .'/' . $page->title() . '.pdf', $dompdf->output());
sleep(5); // I tried this, but it did not help
$attachments = [];
foreach ( $page->documents()->filterBy('extension', 'pdf') as $documents ) {
$attachments[] = $documents;
}
kirby()->email([
'template' => 'order',
'from' => 'my@email.de',
'to' => (string)$page->email(),
'subject' => 'Thank’s for your order!',
'data' => [
'name' => $page->name(),
'email' => (string)$page->email(),
'preis' => formatPrice($page->cart()->getSum())
],
'attachments' => $attachments
]);
the file_put_contents saves the pdf file to the page folder. After I check all pdf files in a foreach function and in the end I attach the array.
if the pdf is already in the folder, it is attached to the mail. if it doesn’t exists, it is created properly, but not attached.
I use Dompdf as pdf library.
Any help or hint is appreciated.
Thanks,
Simon
texnixe
February 23, 2023, 10:28pm
2
You have to create a new file object, append that to the existing files and then create the attachments:
$attachments = [];
F::write($page->contentFileDirectory() .'/' . $page->title() . '.pdf', $dompdf->output());
$newFile = new File([
'filename' => $page->title() . '.pdf',
'url' => $page->url() . '/' . $page->title() . '.pdf',
'parent' => $page,
]);
$newFiles = $page->files()->append($newFile);
foreach($newFiles->filterBy('extension', 'pdf') as $file) {
$attachments[] = $file;
};
1 Like
simgy
February 24, 2023, 6:24am
3
It works perfect. Thank you.
Just to understand what it is happening, if I create the new File, it adds the .txt file. Is this also the time when Kirby copies the file into the media folder?
And do you have an idea, why I was able to send the file in a separate routine?
Thanks a lot and as always the best support.
texnixe
February 24, 2023, 12:27pm
4
No, the image is only copied to the media folder once the url()
method is called or the file is programmatically published.
When you have a separate routine or reload the page, the new file is added to the collection. However, as long as you are in the same process, the inventory is already defined and will not be changed until you force this change.
1 Like
simgy
February 24, 2023, 4:09pm
5
Thank you for explaining.