Add attachment file to email

I want to add a generated file as attachment to an email I send.
The problem is, I can’t get the system to add the file to the email.
I use Uniform to send the email from a form. The form works fine without adding the attachment.

The error I get
There was an error sending the form: Could not access file: http://kirby.test/reserveren.txt

In the form email action I am trying to attach a kirby page file.
This page can be called in the browser with http://kirby.test/reserveren.txt
Also the reserveren page will give a result.
It just won’t be attached to the email.

What can I do to attach this file (or another generated text file) to the mail?

The call to send the mail with attachment.

detail from: 

  if ($kirby->request()->is('POST')) {
        'to' => '',
        'from' => '',
        'subject' => 'mail with attachment ',
        'template' => 'aanvraag',
        'attachments' => ['reserveren.txt'], 

I tried:

        'attachments' => ['reserveren'],
        'attachments' => ['reserveren.php'],
        'attachments' => ['reserveren.txt'],
        'attachments' => ['reserveren.txt.php'],

I hope there is enough code to explain, but if you need more code, I would gladly provide it here.
I have been struggling with this for a couple of days now, so any help will be appreciated :slight_smile:


When and where is the file generated?

The file is not generated by me in the code. The file is a Kirby page generated by Kirby. I followed the route of ‘Content Representations’ (from the youtube video on the Kirby channel).

Do I instead need to generate a file?

Ah ok. So it’s not a real file. That could be your problem. From my understanding the file is only created by opening the corresponding php via browser. I don’t think the script can access it.

Have you tried to attach a static, real text file, which is located at this adress? ( http://kirby.test/reserveren.txt).

May I ask what kind of content is in that file? Would it make sense to write this text directly to the email body?

You are right! The file http://kirby.test/reserveren.txt can be send as attachment.

So, because the file needs to be opened by the browser for it to be created, this method will not work.

.ics file
The content is an .ics file with the data of an event. The user (my client, the owner of the site) can click the .ics file to easily add an agenda item to their calendar. This file is different for every form-entry from visitors who request a reservation on a date.

create file
I have tried creating a file with $page->createFile() and also $myFile = File::create(). But I couldn’t understand how to fill the parameters and options. Documentation and examples I found could not help me get a grip of how to create a file.

Hope you can help :slight_smile:


I don’t have a complete solution, but I think if you going down this path $page->createFile() is the right choice. You can either generate the file and attach it directly or you could store it temporarily with some random filename and send a link in the mail, for example: http://kirby.test/e6a97970-3430-4cbd-b358-81e8513ec25b.ics

If you don’t want to create files you could also generate a link in the mail with all parameters attached, like this: http://kirby.test/reserveren?datefrom=xyz&dateto=xyz. When the user clicks the link your content representation .ics could then do it’s magic. But I don’t know how complex the form is and if there’s anything else speaking against it. And it’s a bit back and forth with data.

Maybe someone else has a better idea.

Thank you for your suggestions!