Simple Email Contact Form


#1

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


#2

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.


#3

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'      => '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());
      }

to this:

try {
  $kirby->email([
    'from' => 'welcome@supercompany.com',
    'replyTo' => 'no-reply@supercompany.com',
    'to' => 'someone@gmail.com',
    'cc' => 'anotherone@gmail.com',
    'bcc' => 'secret@gmail.com',
    'subject' => 'Welcome!',
    'body'=> 'It\'s great to have you with us',
  ]);
} catch (Exception $error) {
  echo $error;
}

But with my own email obviously :slight_smile:


#4

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

return function($kirby) {

You can also check out the fork by https://gist.github.com/starckio/30d800a6518ca4c966f48bfb9f658962 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.


#5

A working example is now in review in case you are still struggling: https://github.com/getkirby/getkirby.com/pull/423/files


#6

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


#7

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.