Mail form not sent

I can’t get a mail form working, that works nicely on other sites at the same provider. Only difference is, that I use this form on the homepage via the snippet, not on a contact page.

When I send the form, the page refreshes but doesn’t show me any success or error page. When I refresh the page again, I get the message if I want to send the form again.

This is my config:

return [
  'debug' => false,
  'date.handler' => 'strftime',

  'email' => [
    'transport' => [
      'type' => 'smtp',
      'host' => 'mail.your-server.de',
      'port' => 587,
      'security' => true,
      'auth' => true,
      'username' => 'lala@emil.de',
      'bcc' => 'webmaster@@emil.de',
      'password' =>'Spasswort fürs Netz',
    ]
  ],

This is 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'),
            'message'   => get('message')
        ];

        $rules = [
            'name'      => ['required', 'min' => 3],
            'email'     => ['required', 'email'],
            'message'   => ['required', 'min' => 10, 'max' => 3000],
        ];

        $messages = [
            'name'      => 'Ihr Name.',
            'email'     => 'Ihre E-Mail Adresse.',
            'message'   => 'Ihre Nachricht.'
        ];

        // some of the data is invalid
        if ($invalid = invalid($data, $rules, $messages)) {
            $alerts = $invalid;
        }
        
        // get the uploads
        $uploads = $kirby->request()->files()->get('file');

        // we want no more than 3 files
        if (count($uploads) > 3) {
            $alerts[] = 'Sie können nur 3 Dateien auf einmal hochladen.';
        }

        // loop through uploads and check if they are valid
        foreach ($uploads as $upload) {

            //  make sure there are no other errors  
                if ($upload['error'] !== 0) {
                $alerts[] = 'The file could not be uploaded';
            // make sure the file is not larger than 2MB…    
            } elseif ($upload['size'] > 3000000)  {
                $alerts[] = $upload['name'] . ' is larger than 3 MB';
            // 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)) {
            try {
                $kirby->email([
                    'template' => 'email',
                    'from'     => 'lala@emil.de'',
                    'replyTo'  => $data['email'],
                    'to'       => ''lala@emil.de'',
                    'subject'     => esc($data['name']),
                    'data'        => [
                        'message'   => esc($data['message']),
                        'name'      => esc($data['name'])
                    ],
                    'attachments' => $attachments
                ]);
            } catch (Exception $error) {
                // we only display a general error message, for debugging use `$error->getMessage()`
                $alerts[] = "Die E-Mail konnte nicht gesendet werden. Sorry";
            }

            // 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([
                    'name'      => esc($data['name'])
                ]);
                // redirect to the success page
                go('success');
            }
        }
    }

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

And this is my form snippet:

<form class="kontakt-form" method="post" action="<?= $page->url() ?>" enctype="multipart/form-data">
    <div class="klebe">
        <label for="website">Website <abbr title="required">*</abbr></label>
        <input type="website" id="website" name="website">
    </div>

        <label for="name">
            Name <abbr title="required">*</abbr>
        </label>
        <input type="text" id="name" name="name" value="<?= $data['name'] ?? null ?>" required>
        <?= isset($alert['name']) ? '<span class="alert error">' . html($alert['name']) . '</span>' : '' ?>


        <label for="email">
            Email <abbr title="required">*</abbr>
        </label>
        <input type="email" id="email" name="email" value="<?= $data['email'] ?? null ?>" required>
  
        <label for="message">
            Ihre Nachricht <abbr title="required">*</abbr>
        </label>
        <textarea id="message" name="message" required><?= $data['message']?? null ?></textarea>        
        <abbr>*</abbr> <span class="pflicht">Pflichtfeld</span>

        <input name="file[]" type="file" multiple>

        <input type="submit" name="submit" value="Submit">
</form>

I would check if there was an error by dumping the $alerts variable in template file:

var_dump($alerts);

<?= var_dump($alerts); ?>

or

<?php var_dump($alerts); ?>

Or is it the same?

PS: Sorry for the question.

<?= var_dump($alerts); ?>in the form snippet produces an error

Undefined variable: alerts

var_dump() is already an echoed output function.

How do you call the form snippet()? Are you sending parameters to the snippet?

I guess so:

<?php snippet('kontakt-form', compact('data')); ?>

But you didn’t sent $alerts variable.

Obviously, but I don’t know why not.

I have the feeling that the controller doesn’t get called, but I don’t know why not. I tried renaming the controller in “home” like the template, but that wasn’t it.

Doesn’t a controller has to have the same name as the template where the form is on?

In your case, yes, if the template is called home.php, the controller must also be called home.php

Also, add the $alert variable to the variables you send to the snippet:

<?php snippet('kontakt-form', compact('data', 'alerts')); ?>

The f’n plural: alerts

```   <?php snippet('kontakt-form', compact('data', 'alerts')); ?>```

That did actually work!

It was the name of the controller (beginner level mistake) and that thing up there.