Send uploaded files via Email

I have a built an upload using the cookbook recipe. No I want to send link to the (multiple) files via email or attach them to an email.

Is it possible to use the kirby email function for this?

  'from' => '',
  'to' => '',
  'subject' => 'Welcome!',
  'body' => 'Here are some attachments',
  'attachments' => [

Yes, that should work.

I’m struggeling with the syntax of the attachments. Is there an example

  • how to add multiple attachments in relation to the number of the uploaded files
  • and how to use the path to the uploaded file in the mail syntax?

Your code looks like you’ve taken it from the docs, right?

Is mail sent at all without attachments?

Yes, the code is the exemple from the docs. I have combined it with the controller for uploading files to the panel. Both works: uploading files to a storage-page and sending mails. But i would like to send the files also with the mail.

What exactly is the problem? What you need to do is store all uploaded files into a array, then add that array as attachment like in the code snippet above.

Sorry, I just don’t know, how to loop through the array inside of the attachment-data.

// define an empty array before you go into the loop
$attachments = [];
// within the loop, after `$file = `
// add the file to the array

$attachments[] = $file;

//then after all files are uploaded, send mail

  'from' => '',
  'to' => '',
  'subject' => 'Welcome!',
  'body' => 'Here are some attachments',
  'attachments' => $attachments;

If you’re interested I used Uniform on a recent project to archive the same thing.


use Uniform\Form;
use Uniform\Actions\SaveFormData;

return function ($kirby)
    $form = new Form([
        'email' => [
            'rules' => ['required', 'email'],
            'message' => 'Please enter a valid email address',
        'resume' => [
            'rules' => [
                'mime' => ['application/pdf'],
                'filesize' => 5000,
            'message' => [
                'Please choose a file.',
                'Please choose a PDF.',
                'Please choose a file that is smaller than 5 MB.',
        'captcha' => [
            'rules' => ['required', 'num'],
            'message' => 'Please fill in the captcha field',

    if ($kirby->request()->is('POST')) {     
        $email = [
            'to' => '',
            'from' => '',
            'subject' => 'New contact form submission',
            'replyTo' => $form->data('email'),
            'template' => 'contact',

        $resume = $form->data('resume');
        if (! empty($resume['tmp_name'])) {
            $email['attachments'][] = renameUploadedFile($resume);
            ->calcGuard(['field' => 'captcha'])
                'file' => kirby()->roots()->site().'/messages.log'

    return compact('form');

And in the config.php I set following:


return [
    'email' => [
        'transport' => [
            'type' => 'smtp',
            'host' => '',
            'port' => 465,
            'security' => 'tls',
            'auth' => true,
            'username' => 'username',
            'password' => 'password',

Then I created a plugin where I defined this function:

if (! function_exists('renameUploadedFile')) {
    function renameUploadedFile(array $file): string {
        $tmp = pathinfo($file['tmp_name']);
        $newFilename = $tmp['dirname'].'/'.$file['name'];

        if (rename($file['tmp_name'], $newFilename)) {
            return $newFilename;
        return $file['tmp_name'];

@flokuek There seems to be something missing though, because where is the function renameUploadedFile() defined?

I edited my post above :slight_smile:

1 Like

Thank you. I didn’t thought about such a simple syntax inside the email-directive. Works fine!

Thank you, I will have a look at it too.

1 Like