Confirmation Mail

I am in the process of incorporating an email form and have followed the Cookbook recipe for it (Email with attachments | Kirby CMS). However, I am failing to include a simple HTML-formatted confirmation email with a text like “We have received your message. Thank you very much”. Is there a simple waym for this that I have overlooked?

Here is the code from my controller:


<?php
return function($kirby, $page) {

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

        // initialize variables
        $alerts      = null;
        $attachments = [];

        // check the honeypot
        if (empty(get('website')) === false) {
            go($page->url());
            exit;
        }
  
        // get the data and validate the other form fields
        $data = [
            'name'      => get('name'),
            'email'     => get('email'),
            'reference' => get('reference'),
            'message'   => get('message'),
            'receipient'   => get('receipient')
        ];

        $rules = [
            'name'      => ['required', 'min' => 3],
            'email'     => ['required', 'email'],
            'reference' => ['required', 'in' => [page('jobs')->children()->listed()->pluck('reference', ',')]],
            'message'   => ['required', 'min' => 10, 'max' => 3000],
        ];

        $messages = [
            'name'      => 'Please enter a valid name.',
            'email'     => 'Please enter a valid email address.',
            'reference' => 'Please enter a valid reference.',
            'message'   => 'Please enter a text between 10 and 3000 characters.'
        ];

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


        // Which receipient?
        if($data['receipient'] == '01'):
            $receipient = 'mail@test1.com';
        elseif($data['receipient'] == '02'):
            $receipient = 'mail@test2.com';
        elseif($data['receipient'] == '03'):
            $receipient = 'mail@test3.com';
        endif;

        // get the uploads
        $uploads = $kirby->request()->files()->get('file');

        // we want no more than 3 files
        if (count($uploads) > 3) {
            $alerts[] = 'You may only upload up to 3 files.';
        }

        // loop through uploads and check if they are valid
        foreach ($uploads as $upload) {
            // make sure the user uploads at least one file
            if ($upload['error'] === 4) {
                $alerts[] = 'You have to attach at least one file';
            //  make sure there are no other errors  
            } elseif ($upload['error'] !== 0) {
                $alerts[] = 'The file could not be uploaded';
            // make sure the file is not larger than 2MB…    
            } elseif ($upload['size'] > 2000000)  {
                $alerts[] = $upload['name'] . ' is larger than 2 MB';
            // …and the file is a PDF
            } elseif ($upload['type'] !== 'application/pdf') {
                $alerts[] = $upload['name'] . ' is not a PDF';
            // all valid, try to rename the temporary file
            } else {
                $name     = $upload['tmp_name'];
                $tmpName  = pathinfo($name);
                // sanitize the original filename
                $filename = $tmpName['dirname']. '/'. F::safeName($upload['name']);
        
                if (rename($upload['tmp_name'], $filename)) {
                    $name = $filename;
                }
                // add the files to the attachments array
                $attachments[] = $name;
            }  
        }
        
        // the data is fine, let's send the email with attachments
        if (empty($alerts)) {
            foreach ($users as $user) {
                try {
                    $kirby->email([
                        'template' => 'email',
                        'from'     => 'mail@test.com',
                        'replyTo'  => $data['email'],
                        'to'       => [
                            $receipient,
                            $data['email']
                        ],
                        'subject'     => esc($data['name']) . ' applied for job ' . esc($data['reference']),
                        'data'        => [
                            'message'   => esc($data['message']),
                            'name'      => esc($data['name']),
                            'reference' => esc($data['reference'])
                        ],
                        'attachments' => $attachments
                    ]);
                } catch (Exception $error) {
                    // we only display a general error message, for debugging use `$error->getMessage()`
                    $alerts[] = "The email could not be sent";
                }

                // no exception occurred, let's send a success message
                if (empty($alerts) === true) {
                    // store reference and name in the session for use on the success page
                    $kirby->session()->set([
                        'reference' => esc($data['reference']),
                        'name'      => esc($data['name'])
                    ]);
                    // redirect to the success page
                    go('success');
                }
            }
        }
    }

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

Before you redirect to the success page (or instead of redirecting), you can email the form submitter.

would I do this with $kirby->email()?

Yes!

1 Like

It’s working. Thank you Sonja! I appreciate it.