Dear forum
I have already read a lot in the forum and many solutions I could already get from it. I am new to Kirby and much better in CSS and HTML than in PHP.
I have integrated the form from the Cookbook into a website and will customize it later.
When I fill out the form and send it, I get the success message, but no mail arrives.
Hosting has sendmail and/or PHP mail turned on. I tried with and without SMTP.
The form on the test site is here: Kontakt - Schmid Bewegt - Mirjam Schmid - Kinesiologie
site/controllers > kontakt.php
<?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 = [
'name' => get('name'),
'email' => get('email'),
'text' => get('text')
];
$rules = [
'name' => ['required', 'minLength' => 3],
'email' => ['required', 'email'],
'text' => ['required', 'minLength' => 3, 'maxLength' => 3000],
];
$messages = [
'name' => 'Please enter a valid name',
'email' => 'Please enter a valid email address',
'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' => 'info@kunde.ch',
'replyTo' => $data['email'],
'to' => 'info@kunde.ch',
'bcc' => 'ich@designheit.ch',
'subject' => esc($data['name']) . ' – Anfrage Webformular',
'data' => [
'text' => esc($data['text']),
'sender' => esc($data['name'])
]
]);
} catch (Exception $error) {
if(option('debug')):
$alert['error'] = 'Das Formular konnte nicht versendet werden: <strong>' . $error->getMessage() . '</strong>';
else:
$alert['error'] = 'Das Formular konnte nicht versendet werden.';
endif;
}
// no exception occurred, let's send a success message
if (empty($alert) === true) {
$success = '<h2>Vielen Dank. Wir melden uns in Kürze.</h2>';
$data = [];
}
}
}
return [
'alert' => $alert,
'data' => $data ?? false,
'success' => $success ?? false
];
};
site/snippets > form.php
<?php if($success): ?>
<div class="alert success">
<p><?= $success ?></p>
</div>
<?php else: ?>
<?php if (isset($alert['error'])): ?>
<div><?= $alert['error'] ?></div>
<?php endif ?>
<form method="post" action="<?= $page->url() ?>#form" class="uk-form-horizontal uk-margin-large-top">
<div class="honeypot">
<label for="website">Website <abbr title="required">*</abbr></label>
<input type="url" id="website" name="website" tabindex="-1">
</div>
<div class="uk-margin field">
<label class="uk-form-label" for="name">Name *</label>
<div class="uk-form-controls">
<input class="uk-input" type="text" id="name" name="name" value="<?= esc($data['name'] ?? '', 'attr') ?>" required>
<?= isset($alert['name']) ? '<span class="alert error">' . esc($alert['name']) . '</span>' : '' ?>
</div>
</div>
<div class="uk-margin field">
<label class="uk-form-label" for="email">E-Mail *</label>
<div class="uk-form-controls">
<input class="uk-input" type="email" id="email" name="email" value="<?= esc($data['email'] ?? '', 'attr') ?>" required>
<?= isset($alert['email']) ? '<span class="alert error">' . esc($alert['email']) . '</span>' : '' ?>
</div>
</div>
<div class="uk-margin field">
<label class="uk-form-label" for="text">Mitteilung*</label>
<div class="uk-form-controls">
<textarea class="uk-textarea" id="text" name="text" rows="5" required><?= esc($data['text'] ?? '') ?></textarea>
<?= isset($alert['text']) ? '<span class="alert error">' . esc($alert['text']) . '</span>' : '' ?>
</div>
</div>
<div class="uk-margin">
<div class="uk-form-controls">
<button class="uk-button uk-button-secondary uk-width-1-1 uk-width-1-3@l" type="submit" name="submit" value="Submit">Absenden</button>
</div>
</div>
</form>
<?php endif ?>
site/templates/emails > email.php
Anfrage Webformular:
<p><?= $text ?></p>
<hr>
<p>Für dieses Angebot interessiere ich mich:</p>
<hr>
<p>Kontakt:</p>
<p><?= $sender ?></p>
<p>
<?= $email ?>
</p>
Thanks for your support