# Add attachment file to email

**URL:** <https://forum.getkirby.com/t/add-attachment-file-to-email/28122>\
**Category:** Questions\
**Tags:** v3\
**Created:** [March 16, 2023, 9:21am UTC](https://forum.getkirby.com/t/add-attachment-file-to-email/28122 "2023-03-16T09:21:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thePhrase](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thephrase/32/10635_2.png) [@thePhrase](https://forum.getkirby.com/u/thePhrase)\
**Post date:** [March 16, 2023, 9:21am UTC](https://forum.getkirby.com/t/add-attachment-file-to-email/28122/1 "2023-03-16T09:21:10Z")

</div>

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`

**situation**  
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.

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

The call to send the mail with attachment.

```auto
detail from: 
plainkit/site/plugins/cafe-blokken/snippets/blocks/cafeformulier.php

  if ($kirby->request()->is('POST')) {
    $form->emailAction([
        'to' => 'test@planet.nl',
        'from' => 'info@somewhere.eu',
        'subject' => 'mail with attachment ',
        'template' => 'aanvraag',
        'attachments' => ['reserveren.txt'], 
    ])->done();
  }
?>

```

**I tried:**

```auto
        '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 🙂

Thanks!  
René

---

<div class="post-metadata">

**Author:** ![stffr](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@stffr](https://forum.getkirby.com/u/stffr)\
**Post date:** [March 16, 2023, 9:29am UTC](https://forum.getkirby.com/t/add-attachment-file-to-email/28122/2 "2023-03-16T09:29:33Z")

</div>

When and where is the file generated?

---

<div class="post-metadata">

**Author:** ![thePhrase](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thephrase/32/10635_2.png) [@thePhrase](https://forum.getkirby.com/u/thePhrase)\
**Post date:** [March 16, 2023, 9:53am UTC](https://forum.getkirby.com/t/add-attachment-file-to-email/28122/3 "2023-03-16T09:53:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![stffr](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@stffr](https://forum.getkirby.com/u/stffr)\
**Post date:** [March 16, 2023, 11:14am UTC](https://forum.getkirby.com/t/add-attachment-file-to-email/28122/4 "2023-03-16T11:14:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![thePhrase](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thephrase/32/10635_2.png) [@thePhrase](https://forum.getkirby.com/u/thePhrase)\
**Post date:** [March 16, 2023, 12:40pm UTC](https://forum.getkirby.com/t/add-attachment-file-to-email/28122/5 "2023-03-16T12:40:03Z")

</div>

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 🙂

René

---

<div class="post-metadata">

**Author:** ![stffr](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@stffr](https://forum.getkirby.com/u/stffr)\
**Post date:** [March 16, 2023, 2:17pm UTC](https://forum.getkirby.com/t/add-attachment-file-to-email/28122/6 "2023-03-16T14:17:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![thePhrase](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thephrase/32/10635_2.png) [@thePhrase](https://forum.getkirby.com/u/thePhrase)\
**Post date:** [March 16, 2023, 3:50pm UTC](https://forum.getkirby.com/t/add-attachment-file-to-email/28122/7 "2023-03-16T15:50:53Z")

</div>

Thank you for your suggestions!
