# How to send email when creating page from frontend?

**URL:** <https://forum.getkirby.com/t/how-to-send-email-when-creating-page-from-frontend/14281>\
**Category:** Questions\
**Tags:** v3\
**Created:** [May 20, 2019, 6:29am UTC](https://forum.getkirby.com/t/how-to-send-email-when-creating-page-from-frontend/14281 "2019-05-20T06:29:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [May 20, 2019, 6:29am UTC](https://forum.getkirby.com/t/how-to-send-email-when-creating-page-from-frontend/14281/1 "2019-05-20T06:29:51Z")

</div>

I am making registration using [Create pages from frontend | Kirby CMS](https://getkirby.com/docs/cookbook/forms/creating-pages-from-frontend)  
Also checked how to send emails [Contact form | Kirby CMS](https://getkirby.com/docs/cookbook/forms/basic-contact-form)

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

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [May 20, 2019, 2:02pm UTC](https://forum.getkirby.com/t/how-to-send-email-when-creating-page-from-frontend/14281/2 "2019-05-20T14:02:02Z")

</div>

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,
    ];
};
```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 20, 2019, 2:18pm UTC](https://forum.getkirby.com/t/how-to-send-email-when-creating-page-from-frontend/14281/3 "2019-05-20T14:18:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [May 20, 2019, 2:52pm UTC](https://forum.getkirby.com/t/how-to-send-email-when-creating-page-from-frontend/14281/4 "2019-05-20T14:52:38Z")

</div>

Thanks, before this line?

```
               go('success');
```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 20, 2019, 2:53pm UTC](https://forum.getkirby.com/t/how-to-send-email-when-creating-page-from-frontend/14281/5 "2019-05-20T14:53:34Z")

</div>

Yes, or before storing the data in the session.

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [May 20, 2019, 3:01pm UTC](https://forum.getkirby.com/t/how-to-send-email-when-creating-page-from-frontend/14281/6 "2019-05-20T15:01:18Z")

</div>

Thank you!
