Simple Email Contact Form

Is there a simple solution or guide to create a contact me email form in Kirby 3?

There is an older version for Kirby 2, with the help of the email documentation, you should be able to adapt that.

Here are the gists for Kirby 2

In the controller, you have to change lines 34 to 53 and use the new email engine instead. The rest should be basically the same.

I’ve put it on the up next list for the cookbook.

Thank you @texnixe

Just to confirm, am I changing this:

  // create the body from a simple snippet
      $body  = snippet('contactmail', $data, true);
      // build the email
      $email = email(array(
        'to'      => '',
        'from'    => '',
        '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()) {
      // add the error to the alert list if it failed
      } else {
        $alert = array($email->error());

to this:

try {
    'from' => '',
    'replyTo' => '',
    'to' => '',
    'cc' => '',
    'bcc' => '',
    'subject' => 'Welcome!',
    'body'=> 'It\'s great to have you with us',
} catch (Exception $error) {
  echo $error;

But with my own email obviously :slight_smile:

Yep, and in the controller return function with the $kirby variable:

return function($kirby) {

You can also check out the fork by which has some useful enhancements, like storing the value in case an error appears so that the form is not emptied all the time and a little honeypot.

A working example is now in review in case you are still struggling:

I set this up but after filling in the details all I get is *The form could not be sent * :frowning:

Hm, did you set up the email templates both for HTML and plain text? Since its failing in the try-catch block, there seems to be something wrong in the email setup.

Same issue here.
If I have email.text.php and email.html.php the form can’t be sent. I need to have a email.php and it works fine.

Great Stuff, but unfortunately I can’t integrate the form into my Onepager. The home.php does not work with the controller.

What can I do?


@ingman What are you referring to? The cookbook example?

In case of a one pager that uses the home.php template, you have to put the logic into a /site/controllers/home.php controller.

Thanks for the quick feedback.

Yes, I use the cookbook example and yes, I put the logic into the controllers/home.php controller, but it doesn’t work. When you click on “send”, only the page is reloaded.

It works fine if I use a contact.php instead of home.php.

Have I forgotten something?

Could you please post the complete snippet that contains the contact form? It should also work on the homepage.

For testing I renamed the contact.php to home.php. The controller is the same as in my example only, it is called home.php and contains my email addresses.

The code of my home.php:

<?php snippet('header') ?>

<main class="main">
  <?php if($success): ?>
    <div class="alert success">
      <p><?= $success ?></p>
  <?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">
    <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 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 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>' : '' ?>
    <input type="submit" name="submit" value="Submit">
  <?php endif ?>

<?php snippet('footer') ?>

Hm, are you using the latest Kirby release (3.2)?

I don’t really see how this can’t work, and it works when I use the code in a fresh Starterkit on the home page.

Yes, I use 3.2.

I’ll experiment with a fresh starter kit and try to find out by exclusion procedure.

Thanks so far

So, some very smart guys have the solution and it’s really very simple. You just have to make “slash” behind the form action like this: <form action="<?php echo $page->url()?>/" method="post">

This is how the email form also works with home.php

Here is the github link for the solution:

Thanks to mzur