Simple Email Contact Form

Thank you, that’s been the critical hint: I realized that I removed postfix recently and did not reinstall any new service in exchange.

I set up the example, but its failing in the try-catch block.
I also set up the email templates, so I dont se why it’s failing.

try {
            $kirby->email([
                'template' => 'my-email',
                'from'     => 'yourcontactform@yourcompany.com',
                'replyTo'  => $data['email'],
                'to'       => 'you@yourcompany.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) {
            $alert['error'] = "The form could not be sent";
        }

My email temlates are in site/templates/emails and are named my-email.html.php and my-email.text.php. I also tried my-email.php but it isn’t working as well.

When submitting I get the error message “The form could not be sent”.

Try and change this line to

 $alert['error'] = $error->getMessage();

to hopefully get some more useful feedback.

Are you testing locally or on a remote server?

Thank you! I tried it and I get the message “The email template “my-email” cannot be found”.
I tried to figure out if its really the template. But when I use

   $kirby->email([
  'from' => 'welcome@supercompany.com',
  'to' => [
    'someone@gmail.com',
    'numbertwo@gmail.com'
  ],
  'subject' => 'Welcome!',
  'body'=> 'It\'s great to have you with us',
]);

I get the error message Could not instantiate mail function.

I’m testing locally.

Locally, make sure sending mail is enabled, by default, this is usually not the case.

Another option is to test your form on a remote server, or set up MailHog or another tool that intercepts your mail.

Thank you, I haven’t thought of it…
Now I can intercept my mail, but when I try to use a template it’s still not working (error message: “The email template “my-email” cannot be found”).
What am I missig or doing wrong?

So the setup does work now with Mailhog if you use the body attribute instead of a template?

yes, exactly. But it doesn’t work with template.

Sure your email template is in /site/templates/emails with an s at the end?

yes…
Unbenannt

Hm, that’s weird. Anything wrong with your file permissions maybe?

I have another question regarding the contact form cookbook.
Each of the inputs is followed by a line like this:

<?= isset($alert['email']) ? '<span class="alert error">' . html($alert['email']) . '</span>' : '' ?>

But where do we actually set the $alert['email'], etc ? I couldn’t find it in the controller. And doesn’t HTML5 automatically warn us if there is something wrong with the e-mail input?

Sorry if these are dumb questions, just trying to understand. :slight_smile:

The $alert variable is set in the controller.