Skip to main content

Captcha

The Email Manager Plugin provides a flexible way to integrate custom captcha solutions into your forms through a combination of frontend snippets and validation callbacks.

Configuration

The captcha integration requires two parts of configuration:

1. Form Configuration

Add the captcha configuration to your form's blueprint:

# site/blueprints/emails/contact-form.yml
captcha:
  frontend:
    snippet: captcha/custom  # Path to your captcha snippet
    fieldname: captcha-field  # Name of the form field
  options:
    # Optional parameters for your captcha implementation
  error_messages:
    missing:
      en: "Please complete the captcha"
      de: "Bitte füllen Sie das Captcha aus"
    invalid:
      en: "Invalid captcha response"
      de: "Ungültige Captcha-Antwort"

2. Validation Callback

Register a validation callback in your site's config:

// site/config/config.php
return [
  'philippoehrlein.kirby-email-manager' => [
    'captcha' => [
      'callback' => function($response, $config) {
        // Your validation logic here
        // $response: The user's input
        // $config: The captcha configuration from your blueprint
        return true; // or false based on validation
      }
    ]
  ]
];

Callback Parameter

Name Type Default Description
$config object

The complete captcha configuration from your blueprint

$response object

The user's input from the form field

Implementation

Create a snippet for your captcha that follows the plugin's structure:

// site/snippets/captcha/custom.php
<?php
use KirbyEmailManager\Helpers\FormHelper;

// Your captcha generation logic here

?>
<div class="<?= FormHelper::getClassName('field', $config) ?>">
    <label class="<?= FormHelper::getClassName('label', $config) ?>">
        <!-- Your captcha challenge -->
    </label>
    
    <input type="text" 
            name="<?= $fieldName ?>" 
            class="<?= FormHelper::getClassName('input', $config) ?>"
            required>

    <?php if (isset($error)): ?>
        <p class="<?= FormHelper::getClassName('error', $config) ?>">
            <?= $error ?>
        </p>
    <?php endif ?>
</div>

The plugin will automatically:

  • Handle the validation using your callback
  • Display error messages as configured
  • Integrate with the plugin's styling system