Hi i am create email form template But I have to face some error like undefind variable $success

/site/controllers/contact.php
return function($kirby, $pages, $page) {

    $alert = null;

    if($kirby->request()->is('POST') && get('submit')) {

        // check the honeypot
        if(empty(get('website')) === false) {
            go($page->url());
            exit;
        }

        $data = [
            'name'  => get('name'),
            'email' => get('email'),
            'phone' => get('phone'),
            'company' => get('company'),
            'message'  => get('message')
            
        ];

        $rules = [
            'name'  => ['required'],
            'email' => ['required', 'email'],
            'text'  => ['required'],
            'phone' => ['required'],
            'company' => ['required'],
            'message'  => ['required']
        ];

        $messages = [
            '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 {
            try {
                $kirby->email([
                    'template' => 'email',
                    'from'     => 'mahesh.headbase@gmail.com',
                    'replyTo'  => $data['email'],
                    'to'       => 'mahesh.headbase@gmail.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) {
                if(option('debug')):
                    $alert['error'] = 'The form could not be sent: <strong>' . $error->getMessage() . '</strong>';
                else:
                    $alert['error'] = 'The form could not be sent!';
                endif;
            }

            // no exception occurred, let's send a success message
            if (empty($alert) === true) {
                $success = 'Your message has been sent, thank you. We will get back to you soon!';
                $data = [];
            }
        }
    }

    return [
        'alert'   => $alert,
        'data'    => $data ?? false,
        'success' => $success ?? false
    ];
};

/site/templates/contact.php

<?php if($success){ ?>

<?= $success ?>

<?php } elseif (isset($alert['error'])) { ?>
<?= $alert['error'] ?>
<?php } ?>
In Kontakt kommen

The code in your controller looks correct and it’s the example from the cookbook so it should work just fine. The only thing I can think of is that there’s an error somewhere in the file names and the controller is not actually loaded.

Are you sure your files are all named correctly? I’m talking about the controller and the template.

1 Like

I’m sorry I made a mistake in the controller folder name