Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save senlin/64077ada47af7483207132771cff1b2d to your computer and use it in GitHub Desktop.
Save senlin/64077ada47af7483207132771cff1b2d to your computer and use it in GitHub Desktop.
Plain contactform example for Kirby 2
<?php
return function($site, $pages, $page) {
$alert = null;
if(get('submit')) {
$data = array(
'name' => get('name'),
'email' => get('email'),
'text' => get('text')
);
$rules = array(
'name' => array('required'),
'email' => array('required', 'email'),
'text' => array('required', 'min' => 3, 'max' => 3000),
);
$messages = array(
'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 {
// create the body from a simple snippet
$body = snippet('contactmail', $data, true);
// build the email
$email = email(array(
'to' => 'bastian@getkirby.com',
'from' => 'contactform@getkirby.com',
'subject' => 'New contact request',
'replyTo' => $data['email'],
'body' => $body
));
// try to send it and redirect to the
// thank you page if it worked
if($email->send()) {
go('contact/thank-you');
// add the error to the alert list if it failed
} else {
$alert = array($email->error());
}
}
}
return compact('alert');
};
Hey,
a new contact request has been submitted!
----
Name: <?php echo $name ?>
----
Email: <?php echo $email ?>
----
Text: <?php echo $text ?>
<?php snippet('header') ?>
<main class="main" role="main">
<h1><?php echo $page->title()->html() ?></h1>
<form method="post">
<?php if($alert): ?>
<div class="alert">
<ul>
<?php foreach($alert as $message): ?>
<li><?php echo html($message) ?></li>
<?php endforeach ?>
</ul>
</div>
<?php endif ?>
<div class="field">
<label for="name">Name <abbr title="required">*</abbr></label>
<input type="text" id="name" name="name">
</div>
<div class="field">
<label for="email">Email <abbr title="required">*</abbr></label>
<input type="email" id="email" name="email" required>
</div>
<div class="field">
<label for="text">Text <abbr title="required">*</abbr></label>
<textarea id="text" name="text" required></textarea>
</div>
<input type="submit" name="submit" value="Submit">
</form>
</main>
<?php snippet('footer') ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment