How to send email when creating page from frontend?

I am making registration using https://getkirby.com/docs/cookbook/forms/creating-pages-from-frontend
Also checked how to send emails https://getkirby.com/docs/cookbook/forms/basic-contact-form

Need help: what code and where to add? Having the same controller as here https://getkirby.com/docs/cookbook/forms/creating-pages-from-frontend#the-event-php-controller

Not sure if all is right, but this is working:

<?php

return function ($kirby, $page) {

    // if the form has been submitted…
    if ($kirby->request()->is('POST') && get('register')) {

        // check the honeypot and exit if is has been filled in
        if(empty(get('website')) === false) {
            go($page->url());
            exit;
        }

        $data = [
            'name'    => get('name'),
            'surname'    => get('surname'),
            'email'   => get('email'),
            'birthdate'    => get('birthdate'),
            'group'  => get('group'),
            'city'    => get('city'),
            'club'    => get('club'),
            'phone'   => get('phone'),
            'message' => get('message'),
            'newsletter' => get('newsletter'),
            'pageurl' => $page->uri(),
            'pagetitle' => $page->title(),
        ];

        $rules = [
            'name'  => ['required'],
            'surname'  => ['required'],
            'email' => ['required', 'email'],
        ];

        $messages = [
            'name'  => 'Please enter your (link: #name text: name)',
            'surname'  => 'Please enter your (link: #surname text: surname)',
            'email' => 'Please enter a valid (link: #email text: email address)',
        ];

        // some of the data is invalid
        if ($invalid = invalid($data, $rules, $messages)) {
            $alert = $invalid;



        } else {


            // authenticate as almighty
            $kirby->impersonate('kirby');

            // everything is ok, let's try to create a new registration
            try {
                // we store registrations as subpages of the current page
                $registration = $page->createChild([
                    'slug'     => md5(str::slug($data['name'] . microtime())),
                    'template' => 'registration',
                    'content'  => $data
                ]);
                
                

                $kirby->email([
                    'template' => 'reg',
                    'from'     => 'from@example.com',
                    'replyTo'  => $data['email'],
                    'to'       => 'to@example.com',
                    'subject'  => esc($data['name']) .' '.  esc($data['surname']) . ' nauja registracija',
                    'data'     => [
                        'text'   	=> esc($data['text']),
                        'name' 		=> esc($data['name']),
                        'surname' 	=> esc($data['surname']),
                        'group' 	=> esc($data['group']),
                        'club' 	=> esc($data['club']),
                        'city' 	=> esc($data['city']),
                        'birthdate' 	=> esc($data['birthdate']),
                        'email' 	=> esc($data['email']),
                        'phone' 	=> esc($data['phone']),
                        'pageurl' 	=> esc($data['pageurl']),
                        'pagetitle' 	=> esc($data['pagetitle'])
                        
                    ]
                ]);
                
                
                

                if ($registration) {
                    // store referer and name in session
                    $kirby->session()->set([
                        'referer' => $page->uri(),
                        'regName'  => esc($data['name'])
                    ]);
                    go('success');
                }

            } catch (Exception $e) {
                $alert = ['Your registration failed: ' . $e->getMessage()];
            }
        }
    }
    
    


    // return data to template
    return [
        'alert' => $alert ?? null,
        'data'  => $data ?? false,
    ];
};

I would send the email later, after the if-statememt

Thanks, before this line?

               go('success');

Yes, or before storing the data in the session.

Thank you!