Optional file attachment contact form

I used the examples from the cookbook, the job application form and want to make it fit my needs. That is making attachments to an email just optional.

The cookbook examples requires to upload an attachement.

Of course I deleted the “required” from the file form field but this wasn’t enough.

How do I make attachments optionally?

in the controler there are some lines that look suspicious to me:

        // get the uploads
        $uploads = $kirby->request()->files()->get('file');

Is “request” the problem?

  if ($upload['error'] === 4) {
                $alerts[] = 'You have to attach at least one file';

There is this alert but it is not the thing that demands a file to be attached, right?

In the controller, we require at least one file

if ($upload['error'] === 4) {
    $alerts[] = 'You have to attach at least one file';
    //  make sure there are no other errors  

You would have to remove that bit…

:wink: So, it is not just an alert. It is a thing?!

PS: That was fast!

Well, error 4 means there is no file attached, so in this case we throw an error. You probably have to adapt it some more, but I would have to check this out myself.


syntax error, unexpected 'elseif' (T_ELSEIF)


Well, you can’t start an if statement with an else if, so you have to change this to an if

Yes, I saw that the minute after I posted. But if I change that to if like:

 if ($upload['error'] !== 0) {
                $alerts[] = 'The file could not be uploaded';

I get that error and it doesn’t send.

And so forth.

And the content of my textarea is not sent. How could I screw up that?

Sorry, my mistake.

Try this:

if ($uploads[0]['error'] !== 4) {
            foreach ($uploads as $upload) {
                //  make sure there are no other errors  
                if ($upload['error'] !== 0) {
                    $alerts[] = 'The file could not be uploaded';
                // make sure the file is not larger than 2MB…    
                } elseif ($upload['size'] > 2000000)  {
                    $alerts[] = $upload['name'] . ' is larger than 2 MB';
                // …and the file is a PDF
                } elseif ($upload['type'] !== 'application/pdf') {
                    $alerts[] = $upload['name'] . ' is not a PDF';
                // all valid, try to rename the temporary file
                } else {
                    $name     = $upload['tmp_name'];
                    $tmpName  = pathinfo($name);
                    // sanitize the original filename
                    $filename = $tmpName['dirname']. '/'. F::safeName($upload['name']);
                    if (rename($upload['tmp_name'], $filename)) {
                        $name = $filename;
                    // add the files to the attachments array
                    $attachments[] = $name;

If it still doesn’t work, I’ll have to postpone this till tomorrow and test it myself (not to fluent with files in forms).

Yes, this works fine!

Thank you!

PS: Now I have to figure out where I screwed-up the message text.
PS: Maybe add this to the cookbook?

Used the wrong variables from the jobs application template from the jobkit.


<p><?= $message ?></p>

for the html email template

and just:

<?= $message ?> 

for the plain text template, did the trick.

Just like in the docs.