Hi everyone,
I’ve got a problem concerning Kirby mail contact form.
I want to create a custom form. I followed the instructions on the dedicated page.
It works until I try to add new fields. I’ve added it into the contact.php, the controller (also named contact.php but in the controllers folder), the email.php and email.html.php.
The form is correctly displayed but I get an error when I click on the submit button. It works again if I remove the four lines I added to the controller, these ones (bolded) :
$data = [
’tel’ => get(‘tel’),
’pers’ => get(‘pers’),
and these ones (bolded) :
$kirby->email([
‘data’ => [
’tel’ => esc($data[‘tel’]),
’pers’ => est($data[‘pers’]),
Any idea ?
Here is the full code of my controller :
<?php
return function($kirby, $pages, $page) {
$alert = null;
if($kirby->request()->is('POST') && get('submit')) {
// check the honeypot
//if(empty(get('website')) === false) {
// go($page->url());
// exit;
//}
$data = [
'tel' => get('tel'),
'pers' => get('pers'),
'name' => get('name'),
'email' => get('email'),
'text' => get('text'),
];
$rules = [
'name' => ['required', 'min' => 3],
'email' => ['required', 'email'],
'text' => ['required', 'min' => 3, 'max' => 3000],
];
$messages = [
'name' => 'Entrez un nom valide svp',
'email' => 'Entrez une adresse valide svp',
'text' => '3000 caractères maximum'
];
// some of the data is invalid
if($invalid = invalid($data, $rules, $messages)) {
$alert = $invalid;
// the data is fine, let's send the email
} else {
try {
$kirby->email([
'template' => 'email',
'from' => 'contact@lacuverieduchateau.com',
'replyTo' => $data['email'],
'to' => 'adrien.payet@outlook.com',
'subject' => esc($data['name']) . ' a envoyé un message via le site internet',
'data' => [
'tel' => esc($data['tel']),
'pers' => est($data['pers']),
'text' => esc($data['text']),
'sender' => esc($data['name']),
]
]);
} catch (Exception $error) {
$alert['error'] = "Le formulaire n'a pas pu être envoyé";
}
// no exception occured, let's send a success message
if (empty($alert) === true) {
$success = 'Votre message a bien été envoyé, merci. Nous vous répondons dès que possible.';
$data = [];
}
}
}
return [
'alert' => $alert,
'data' => $data ?? false,
'success' => $success ?? false
];
};