Skip to main content

Add SendGrid category tracking to all emails

Free Beginner Since v1.0.0

Sends Benecaster emails with SendGrid category tags for segmenting analytics by email type in the SendGrid dashboard. This approach requires SMTP delivery — WordPress must be sending email through SendGrid’s SMTP gateway (smtp.sendgrid.net) via a plugin like WP Mail SMTP or FluentSMTP configured in SMTP mode.

If you are using SendGrid’s Web API (WP Mail SMTP in API mode, the official SendGrid WordPress plugin), the X-SMTPAPI header added here will have no effect. API-mode integrations handle categories through the request body, not email headers — consult your integration plugin’s documentation for adding SendGrid categories.

Code

<?php
add_filter(
    'benecaster_email_headers',
    function ( array $headers, string $type, string $to, array $context ): array {
        $smtpapi = [
            'category'      => [ 'benecaster', 'benecaster_' . $type ],
            'unique_args'   => [
                'benecaster_type' => $type,
                'show_id'         => (int) ( $context['show_id'] ?? 0 ),
            ],
        ];
        $headers[] = 'X-SMTPAPI: ' . wp_json_encode( $smtpapi );
        return $headers;
    },
    10,
    4
);

View on GitHub →

Hooks Used