I tried generating unique ID for each contact mail using uniqid() but It’s not working, I do not know how to do it again.
here’s is my controller file.
<?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 = [
'uid' => get('uid'),
'fullname' => get('fullname'),
'email' => get('email'),
'phone' => get('phone'),
'objectemail' => get('objectemail'),
'text' => get('text')
];
$rules = [
'fullname' => ['required', 'minLength' => 3],
'email' => ['required', 'email'],
'phone' => ['required', 'num'],
'objectemail' => ['required', 'email'],
'text' => ['required', 'minLength' => 3, 'maxLength' => 3000],
];
$messages = [
'fullname' => 'Please enter a valid name',
'email' => 'Please enter a valid email address',
'phone' => 'Please enter a valid phone number',
'objectemail' => 'Objet de votre demande',
'text' => 'Please enter a text between 3 and 3000 characters'
];
// 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' => 'yourcontactform@yourcompany.com',
'replyTo' => $data['email'],
'to' => $data['objectemail'],
'subject' => 'Reclamation' . esc($data['uid']),
'data' => [
'text' => esc($data['text']),
'sender' => esc($data['fullname']) . esc($data['phone']) . esc($data['email'])
]
]);
} catch (Exception $error) {
if(option('debug')):
$alert['error'] = 'The form could not be sent: <strong>' . $error->getMessage() . '</strong>';
else:
$alert['error'] = 'The form could not be sent!';
endif;
}
// no exception occurred, let's send a success message
if (empty($alert) === true) {
$success = 'Votre message a été envoyé. Merci de nous avoir contacté. Nous traiteront votre demande dans les plus brefs délais. <b>N° demande #' . esc($data['uid']);
$data = [];
}
}
}
return [
'alert' => $alert,
'data' => $data ?? false,
'success' => $success ?? false
];