Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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') ?>
@mrflix

This comment has been minimized.

Copy link

commented Dec 5, 2014

Awesome!

@andreasnymark

This comment has been minimized.

Copy link

commented Dec 19, 2014

The email class uses a capital E; make sure you change it in the controller.

@brunorodrigues355

This comment has been minimized.

Copy link

commented Jun 12, 2015

This is great, could you tell me how to send an success message instead of going to other page?
Just changing "go('contact/thank-you');" to "$alert = "Thank You";" didnt work for me.
Im not great with php lol
Thanks in advance

@superfunkminister

This comment has been minimized.

Copy link

commented Jun 7, 2016

Is there a reason this wouldn't work with AJAX? I'm using the same code I was using from my Kirby 1 site with the old plugin and it's no longer working.

@atomicbird

This comment has been minimized.

Copy link

commented Aug 30, 2016

For some themes (e.g. baseblog) it's necessary to add <?php snippet('menu') ?> at the top of contact.php, right after the header line.

@masiarekrafal

This comment has been minimized.

Copy link

commented Jan 31, 2017

Hey, this gist still works with kirby >= 2.4.1?

@foobartel

This comment has been minimized.

Copy link

commented Mar 6, 2017

@masiarekrafal Yes, the gist still works fine with 2.4.1

@lkleen

This comment has been minimized.

Copy link

commented Apr 22, 2017

The specification is a bit ambiguous at this point but it is more convenient to end the input tag with />, isn't it?

@aerus

This comment has been minimized.

Copy link

commented Jul 11, 2017

is it possible to add a fileupload field and to send the file as an attachment?

@thykes

This comment has been minimized.

Copy link

commented Aug 19, 2017

I am wondering the same thing as @aerus. I would love away to add and send a file attachment from the form.

@MaxiK82

This comment has been minimized.

Copy link

commented Oct 31, 2017

Thanks a lot. Working fine with Kirby => 2.5.6

@starckio

This comment has been minimized.

Copy link

commented Jan 18, 2018

I made a fork, added an anti bot, CSS styles (for Starterkit), blueprints and several improvements in the template and the controller.

https://gist.github.com/starckio/30d800a6518ca4c966f48bfb9f658962

@t4bbo

This comment has been minimized.

Copy link

commented Feb 25, 2018

Hey,
thanks for your work ;)
i get the message: The email could not be sent
But no errors in logfiles or something.
Any suggestions why that happens?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.