Email form is not working

Email form is not working.

I wrote the code exactly as described in the kirby cookbook.

Below is the website I am working on

However, when I click submit, no success or failure messages appear and the email does not arrive. Is there anything I am missing?

/site/templates/contact.php

<?php snippet('header') ?>
<main class="main">
    <h1><?= $page->title()->html() ?></h1>

    <?php if($success): ?>
    <div class="alert success">
        <p><?= $success ?></p>
    </div>
    <?php else: ?>
    <?php if (isset($alert['error'])): ?>
        <div><?= $alert['error'] ?></div>
    <?php endif ?>
    <form method="post" action="<?= $page->url() ?>">
        <div class="honeypot">
            <label for="website">Website <abbr title="required">*</abbr></label>
            <input type="website" id="website" name="website" tabindex="-1">
        </div>
        <div class="field">
            <label for="name">
                Name <abbr title="required">*</abbr>
            </label>
            <input type="text" id="name" name="name" value="<?= $data['name'] ?? '' ?>" required>
            <?= isset($alert['name']) ? '<span class="alert error">' . html($alert['name']) . '</span>' : '' ?>
        </div>
        <div class="field">
            <label for="email">
                Email <abbr title="required">*</abbr>
            </label>
            <input type="email" id="email" name="email" value="<?= $data['email'] ?? '' ?>" required>
            <?= isset($alert['email']) ? '<span class="alert error">' . html($alert['email']) . '</span>' : '' ?>
        </div>
        <div class="field">
            <label for="text">
                Text <abbr title="required">*</abbr>
            </label>
            <textarea id="text" name="text" required>
                <?= $data['text']?? '' ?>
            </textarea>
            <?= isset($alert['text']) ? '<span class="alert error">' . html($alert['text']) . '</span>' : '' ?>
        </div>
        <input type="submit" name="submit" value="Submit">
    </form>
    <?php endif ?>
</main>
<?php snippet('footer') ?>

/site/controllers/contact.php

<?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'),
            'text'  => get('text')
        ];

        $rules = [
            'name'  => ['required', 'minLength' => 3],
            'email' => ['required', 'email'],
            'text'  => ['required', 'minLength' => 3, 'maxLength' => 3000],
        ];

        $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'     => 'sonahyong@gmail.com',
                    'replyTo'  => $data['email'],
                    'to'       => 'sonahyong@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/emails/email.php

Hello Company,

<?= $text ?>

<?= $sender ?>

/site/templates/emails/email.html.php

Hello Company,

<p><?= $text ?></p>

<p>Best,</p>
<p><?= $sender ?></p>

I’d first try if $kirby->email() works without the form, so hardcode some data into your $kirby->email() code and put it into a template.

Thank you for your answer.

How can I put it?
Could you please let me know a little more specific?

Put this code in a template or controller, open a page that uses this controller in your browser:

 $kirby->email([
                    'template' => 'email',
                    'from'     => 'sonahyong@gmail.com',
                    'replyTo'  => 'whoever@whatever.com',
                    'to'       => 'sonahyong@gmail.com',
                    'subject'  => 'Someone sent you a message from your contact form',
                    'data'     => [
                        'text'   => 'Some text',
                        'sender' => 'The great pretender',
                    ]
                ]);

Or store the result in a variable and dump it

$success =  $kirby->email([
                    'template' => 'email',
                    'from'     => 'sonahyong@gmail.com',
                    'replyTo'  => 'whoever@whatever.com',
                    'to'       => 'sonahyong@gmail.com',
                    'subject'  => 'Someone sent you a message from your contact form',
                    'data'     => [
                        'text'   => 'Some text',
                        'sender' => 'The great pretender',
                    ]
                ])->isSent();

var_dump($success);

I modified my controller. But nothing changes.
There is no e-mail, and no message appears.
Is there anything I am missing?

site/controllers/contact.php

<?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'),
        'text'  => get('text')
    ];

    $rules = [
        'name'  => ['required', 'minLength' => 3],
        'email' => ['required', 'email'],
        'text'  => ['required', 'minLength' => 3, 'maxLength' => 3000],
    ];

    $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'     => 'sonahyong@gmail.com',
                'replyTo'  => 'whoever@whatever.com',
                'to'       => 'sonahyong@gmail.com',
                'subject'  => 'Someone sent you a message from your contact form',
                'data'     => [
                    'text'   => 'Some text',
                    'sender' => 'The great pretender',
                ]
            ]);

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

Did you do the test with the code I posted above in a controller?

What is the result of var_dump($success)?

It might be that sending mail is not enabled at all on your system, but in this case you should get an error.

And additionally, what are you email transport settings? Are you sending through a provider? Using sendmail? SMTP?

Hi Sonja,
I’m having a similar issue in relation to implementing the email contact form recipe on a one pager. I’m using home.php as the template and have renamed controller to match. Equally I’ve tested hard coding the email function into the template and after some wrangling with GoDaddy and PhpMailer it seems that the email() is working but for some reason the form code isn’t. Instead on submitting the post I don’t get anything back… no alerts or mails. The page refreshes but does nothing. Would be much appreciated if you had any suggestions!

Cheers,
Max

Are you using the example from the recipe as is with no changes whatsoever? Have you adapted the action attribute URL?

Please try to dump the output of your form in two locations as below:

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

    $alert = null;

    if($kirby->request()->is('POST') && get('submit')) {
dump($_POST); // check if you get into this if-statement at all
        // check the honeypot
        if(empty(get('website')) === false) {
            go($page->url());
            exit;
        }

        $data = [
            'name'  => get('name'),
            'email' => get('email'),
            'text'  => get('text')
        ];

        $rules = [
            'name'  => ['required', 'minLength' => 3],
            'email' => ['required', 'email'],
            'text'  => ['required', 'minLength' => 3, 'maxLength' => 3000],
        ];

        $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 {
dump($data);
            try {
                $kirby->email([
                    //...
                    ]
                ]);

            } catch (Exception $error) {
                if(option('debug')):
                    $alert['error'] = 'The form could not be sent: ' . $error->getMessage();
                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
    ];
};

Dear @texnixe I’m also having issues with the email form.
I successfully implemented it before, but with the newest version of Kirby It does not seem to work any longer.
I tried the Cookbook, the tips you mention from above (line 43 has a ] too much).
But as a result I’m getting a page sowing this:

Array
(
    [website] => 
    [name] => Vorname Name
    [email] => vn@vname.com
    [text] => Testing this ...                                    
    [submit] => Submit
)
Array
(
    [name] => Vorname Name
    [email] => vn@vname.com
    [text] => Testing this ...                                    
)

The output from the dumps look as expensive. Do you mean 3.7.5 or the 3.8.0-rc?

What exactly does not work?

Oh wow @texnixe, I did not anticipate an answer tonight.

I’m on 3.7.5

I’ve got it to work now in the contact template on a dedicated page.
On the homepage in the contact.php snippet, the page reloads but does not send.

(I do have two controllers, one contact.php the other one home.php)

Good morning @texnixe, I looked at the contact.php snipped again with fresh eyes and found the mistake. Everything is working fine now. Sorry for that.