I can’t get a mail form working, that works nicely on other sites at the same provider. Only difference is, that I use this form on the homepage via the snippet, not on a contact page.
When I send the form, the page refreshes but doesn’t show me any success or error page. When I refresh the page again, I get the message if I want to send the form again.
This is my config:
return [
'debug' => false,
'date.handler' => 'strftime',
'email' => [
'transport' => [
'type' => 'smtp',
'host' => 'mail.your-server.de',
'port' => 587,
'security' => true,
'auth' => true,
'username' => 'lala@emil.de',
'bcc' => 'webmaster@@emil.de',
'password' =>'Spasswort fürs Netz',
]
],
This is my controller:
<?php
return function($kirby, $page) {
if ($kirby->request()->is('POST') && get('submit')) {
// initialize variables
$alerts = null;
$attachments = [];
// check the honeypot
if (empty(get('website')) === false) {
go($page->url());
exit;
}
// get the data and validate the other form fields
$data = [
'name' => get('name'),
'email' => get('email'),
'message' => get('message')
];
$rules = [
'name' => ['required', 'min' => 3],
'email' => ['required', 'email'],
'message' => ['required', 'min' => 10, 'max' => 3000],
];
$messages = [
'name' => 'Ihr Name.',
'email' => 'Ihre E-Mail Adresse.',
'message' => 'Ihre Nachricht.'
];
// 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[] = 'Sie können nur 3 Dateien auf einmal hochladen.';
}
// loop through uploads and check if they are valid
foreach ($uploads as $upload) {
// make sure there are no other errors
if ($upload['error'] !== 0) {
$alerts[] = 'The file could not be uploaded';
// make sure the file is not larger than 2MB…
} elseif ($upload['size'] > 3000000) {
$alerts[] = $upload['name'] . ' is larger than 3 MB';
// 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([
'template' => 'email',
'from' => 'lala@emil.de'',
'replyTo' => $data['email'],
'to' => ''lala@emil.de'',
'subject' => esc($data['name']),
'data' => [
'message' => esc($data['message']),
'name' => esc($data['name'])
],
'attachments' => $attachments
]);
} catch (Exception $error) {
// we only display a general error message, for debugging use `$error->getMessage()`
$alerts[] = "Die E-Mail konnte nicht gesendet werden. Sorry";
}
// no exception occurred, let's send a success message
if (empty($alerts) === true) {
// store reference and name in the session for use on the success page
$kirby->session()->set([
'name' => esc($data['name'])
]);
// redirect to the success page
go('success');
}
}
}
// return data to template
return [
'alerts' => $alerts ?? null,
'data' => $data ?? false,
];
};
And this is my form snippet:
<form class="kontakt-form" method="post" action="<?= $page->url() ?>" enctype="multipart/form-data">
<div class="klebe">
<label for="website">Website <abbr title="required">*</abbr></label>
<input type="website" id="website" name="website">
</div>
<label for="name">
Name <abbr title="required">*</abbr>
</label>
<input type="text" id="name" name="name" value="<?= $data['name'] ?? null ?>" required>
<?= isset($alert['name']) ? '<span class="alert error">' . html($alert['name']) . '</span>' : '' ?>
<label for="email">
Email <abbr title="required">*</abbr>
</label>
<input type="email" id="email" name="email" value="<?= $data['email'] ?? null ?>" required>
<label for="message">
Ihre Nachricht <abbr title="required">*</abbr>
</label>
<textarea id="message" name="message" required><?= $data['message']?? null ?></textarea>
<abbr>*</abbr> <span class="pflicht">Pflichtfeld</span>
<input name="file[]" type="file" multiple>
<input type="submit" name="submit" value="Submit">
</form>