Shopware 6 Guide

For Shopware 6, the integration works very similarly to PrestaShop, as Shopware 6 is also built on a modern Symfony foundation.

Here is the step-by-step guide on how to build a custom plugin named SentryFormSpam that secures the standard contact form.

Plugin Folder Structure

Create a new folder named SentryFormSpam in the custom/plugins/ directory of your Shopware installation with this structure:

Step 1: The composer.json

This file defines the plugin for the Shopware plugin system.


{
    "name": "sentryform/spam",
    "description": "SentryForm Spam Protection for Shopware 6",
    "type": "shopware-platform-plugin",
    "version": "1.0.0",
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "SentryForm\\Spam\\": "src/"
        }
    },
    "extra": {
        "shopware-plugin-class": "SentryForm\\Spam\\SentryFormSpam",
        "plugin-name": "SentryFormSpam"
    }
}
    

Step 2: The main plugin class (src/SentryFormSpam.php)

    
<?php     

declare(strict_types=1);

namespace SentryForm\Spam;

use Shopware\Core\Framework\Plugin;

class SentryFormSpam extends Plugin
{
}
    

Step 3: The SpamChecker service (src/Service/SentryFormSpamChecker.php)

This is where the HTTP request to your SentryForm API is processed (using Shopware's native Symfony HTTP client).



<?php

declare(strict_types=1);

namespace SentryForm\Spam\Service;

use Symfony\Contracts\HttpClient\HttpClientInterface;

class SentryFormSpamChecker
{
    private HttpClientInterface $httpClient;
    private string $apiKey = 'Pxu78kBoHKLRYdkWzFYOClsQfnnRtHyRFvndyWeikXaciinl3Dj388Neh1bs'; // Dein API-Key

    public function __construct(HttpClientInterface $httpClient)
    {
        $this->httpClient = $httpClient;
    }

    public function check(string $sender, string $recipient, string $subject, string $message): bool
    {
        try {
            $response = $this->httpClient->request('POST', 'https://api.sentryform.online/api/v1/scan', [
                'headers' => [
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json',
                    'X-Spam-ApiKey' => $this->apiKey,
                    'X-Spam-Referer' => $request->getHost(),
                ],
                'json' => [
                    'sender' => $sender,
                    'recipient' => $recipient,
                    'subject' => $subject,
                    'message' => $message,
                ],
            ]);

            $statusCode = $response->getStatusCode();
            $data = $response->toArray(false);

            // Wenn API 200 liefert und der Spam-Code 1 ist -> Spam erkannt!
            if ($statusCode === 200 && isset($data['code']) && $data['code'] === 1) {
                return true; 
            }

        } catch (\Exception $e) {
            // Bei API-Ausfällen den Shop im Zweifel nicht blockieren
            return false;
        }

        return false;
    }
}
        

Step 4: The event subscriber for the contact form (src/Subscriber/ContactFormSubscriber.php)

This listens for the submission of the contact form in Shopware 6.


<?php 
            
declare(strict_types=1);

namespace SentryForm\Spam\Subscriber;

use SentryForm\Spam\Service\SentryFormSpamChecker;
use Shopware\Core\Content\ContactForm\Event\ContactFormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class ContactFormSubscriber implements EventSubscriberInterface
{
    private SentryFormSpamChecker $spamChecker;
    private RequestStack $requestStack;

    public function __construct(SentryFormSpamChecker $spamChecker, RequestStack $requestStack)
    {
        $this->spamChecker = $spamChecker;
        $this->requestStack = $requestStack;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            ContactFormEvent::class => 'onContactFormSend',
        ];
    }

    public function onContactFormSend(ContactFormEvent $event): void
    {
        $request = $this->requestStack->getCurrentRequest();
        if (!$request) {
            return;
        }

        $data = $event->getData();
        $senderEmail = $data['email'] ?? $request->request->get('email');
        $messageText = $data['comment'] ?? $request->request->get('comment');
        $subject = $data['subject'] ?? 'Kontaktformular-Anfrage';
        $recipientEmail = 'service@sebitec.net'; // Oder dynamisch aus den Shopware-Konfigurationen

        // Spam-Prüfung ausführen
        $isSpam = $this->spamChecker->check($senderEmail, $recipientEmail, $subject, $messageText);

        if ($isSpam) {
            // Wirft eine Exception, die Shopware abfängt und dem Nutzer im Frontend anzeigt
            throw new \Exception('Ihre Nachricht wurde als Spam eingestuft und konnte nicht gesendet werden.');
        }
    }
}
        

Step 5: The service configuration (src/Resources/config/services.yml)


services:
  SentryForm\Spam\Service\SentryFormSpamChecker:
    arguments:
      - '@Symfony\Contracts\HttpClient\HttpClientInterface'

  SentryForm\Spam\Subscriber\ContactFormSubscriber:
    arguments:
      - '@SentryForm\Spam\Service\SentryFormSpamChecker'
      - '@request_stack'
    tags:
      - { name: 'kernel.event_subscriber' }
        

Activation in the Shopware 6 Shop

  1. Upload the SentryFormSpam folder via FTP to the custom/plugins/ directory (or push it via Git).
  2. Open your terminal on the server and run these commands sequentially to install and activate the plugin:
    Bash
    bin/console plugin:refresh bin/console plugin:install --activate SentryFormSpam bin/console cache:clear
  3. Alternatively, you can install and activate the plugin in the Shopware Admin under Extensions > My Extensions.