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?

```
$kirby->email([
  'from' => 'welcome@supercompany.com',
  'to' => 'someone@gmail.com',
  'subject' => 'Welcome!',
  'body' => 'Here are some attachments',
  'attachments' => [
    $page->file('somefile.jpg'),
    $page->file('someotherfile.jpg')
  ]
]);
```

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

$kirby->email([
  'from' => 'welcome@supercompany.com',
  'to' => 'someone@gmail.com',
  '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.

<?php

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' => [
                'file',
                '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' => 'website@example.com',
            'from' => 'test@example.com',
            'subject' => 'New contact form submission',
            'replyTo' => $form->data('email'),
            'template' => 'contact',
        ];

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

    return compact('form');
};

And in the config.php I set following:

<?php

return [
    'email' => [
        'transport' => [
            'type' => 'smtp',
            'host' => 'smtp.server.goes.here',
            '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'];
    }
}
3 Likes

@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