Email form is not working.
I wrote the code exactly as described in the kirby cookbook.
Below is the website I am working on
However, when I click submit, no success or failure messages appear and the email does not arrive. Is there anything I am missing?
/site/templates/contact.php
<?php snippet('header') ?>
<main class="main">
<h1><?= $page->title()->html() ?></h1>
<?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() ?>">
<div class="honeypot">
<label for="website">Website <abbr title="required">*</abbr></label>
<input type="website" id="website" name="website" tabindex="-1">
</div>
<div class="field">
<label for="name">
Name <abbr title="required">*</abbr>
</label>
<input type="text" id="name" name="name" value="<?= $data['name'] ?? '' ?>" required>
<?= isset($alert['name']) ? '<span class="alert error">' . html($alert['name']) . '</span>' : '' ?>
</div>
<div class="field">
<label for="email">
Email <abbr title="required">*</abbr>
</label>
<input type="email" id="email" name="email" value="<?= $data['email'] ?? '' ?>" required>
<?= isset($alert['email']) ? '<span class="alert error">' . html($alert['email']) . '</span>' : '' ?>
</div>
<div class="field">
<label for="text">
Text <abbr title="required">*</abbr>
</label>
<textarea id="text" name="text" required>
<?= $data['text']?? '' ?>
</textarea>
<?= isset($alert['text']) ? '<span class="alert error">' . html($alert['text']) . '</span>' : '' ?>
</div>
<input type="submit" name="submit" value="Submit">
</form>
<?php endif ?>
</main>
<?php snippet('footer') ?>
/site/controllers/contact.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' => 'sonahyong@gmail.com',
'replyTo' => $data['email'],
'to' => 'sonahyong@gmail.com',
'subject' => esc($data['name']) . ' sent you a message from your contact form',
'data' => [
'text' => esc($data['text']),
'sender' => esc($data['name'])
]
]);
} 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 = 'Your message has been sent, thank you. We will get back to you soon!';
$data = [];
}
}
}
return [
'alert' => $alert,
'data' => $data ?? false,
'success' => $success ?? false
];
};
/site/templates/emails/email.php
Hello Company,
<?= $text ?>
<?= $sender ?>
/site/templates/emails/email.html.php
Hello Company,
<p><?= $text ?></p>
<p>Best,</p>
<p><?= $sender ?></p>