Hello,
I’m using the contact form from the cookbook, but I don’t want to restrict the attachments to PDF only. In the controller I commented the IF that checks the attachment and it seems to work fine but then I receive attachments only if they are PDFs. I tried also attaching a PDF and a JPG and in the email received I got just the PDF attachment. What’s wrong with my code?
<?php
return function($kirby, $page) {
if ($kirby->request()->is('POST') && get('submit')) {
// initialize variables
$alerts = null;
$attachments = [];
$success = false;
// check the honeypot
if (empty(get('website')) === false) {
go($page->url());
exit;
}
// get the data and validate the other form fields
$data = [
'firstname' => get('firstname'),
'lastname' => get('lastname'),
'email' => get('email'),
'phone' => get('phone'),
'message' => get('message')
];
$rules = [
'firstname' => ['required', 'min' => 3],
'lastname' => ['required', 'min' => 3],
'email' => ['required', 'email'],
'message' => ['required', 'min' => 10, 'max' => 3000],
];
$messages = [
'firstname' => 'Please enter a valid name.',
'lastname' => 'Please enter a valid name.',
'email' => 'Please enter a valid email address.',
'message' => 'Please enter a text between 10 and 3000 characters.'
];
// some of the data is invalid
if ($invalid = invalid($data, $rules, $messages)) {
$alerts = $invalid;
}
// get the uploads
$uploads = $kirby->request()->files()->get('file');
// we want no more than 3 files
if (count($uploads) > 3) {
$alerts[] = 'You may only upload up to 3 files.';
}
// loop through uploads and check if they are valid
foreach ($uploads as $upload) {
// make sure the user uploads at least one file
if ($upload['error'] === 4) {
// $alerts[] = 'You have to attach at least one file';
// make sure there are no other errors
} elseif ($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;
}
}
// the data is fine, let's send the email with attachments
if (empty($alerts)) {
try {
$kirby->email([
'beforeSend' => function ($mailer) {
$mailer->SMTPOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
];
},
'template' => 'email',
'from' => 'info@xxxx.com',
'replyTo' => $data['email'],
'to' => 'info@xxxx.com',
'subject' => ' Enquiry from ' . esc($data['firstname']) . ' ' . esc($data['lastname']),
'data' => [
'message' => 'First name: '.esc($data['firstname']).'<br>Last Name: '.esc($data['lastname']).'<br>Phone: '.esc($data['phone']).'<br><br>Message:'.esc($data['message']),
'firstname' => esc($data['firstname']),
'lastname' => esc($data['lastname']),
'phone' => esc($data['phone']),
],
'attachments' => $attachments
]);
} catch (Exception $error) {
// we only display a general error message, for debugging use `$error->getMessage()`
$alerts[] = "The email could not be sent";
}
// no exception occurred, let's send a success message
if (empty($alerts) === true) {
$kirby->session()->set([
'firstname' => esc($data['firstname'])
]);
// redirect to the success page
$success = true;
}
}
}
// return data to template
return [
'alerts' => $alerts ?? null,
'data' => $data ?? false,
'success' => $success ?? false
];
};