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'];
}
}