Add SendGrid category tracking to all emails
Tags every outbound Benecaster email with a SendGrid category so the SendGrid dashboard can slice open-rate, bounce-rate, and unsubscribe metrics by email type. The category is attached by adding an X-SMTPAPI header through the benecaster_email_headers filter.
SMTP relay delivery only. This approach works when WordPress delivers mail through SendGrid’s SMTP gateway (smtp.sendgrid.net) — via WP Mail SMTP set to SMTP mode, FluentSMTP, or any SMTP client pointed at SendGrid. If WP Mail SMTP is configured with the SendGrid API mailer type, the X-SMTPAPI header becomes a normal email header delivered to the recipient’s inbox and never reaches SendGrid’s category dashboard. Use the sibling recipe Add SendGrid category tracking in API mode for API mode; hook both in parallel if the operator may switch delivery modes.
Code
<?php
add_filter(
'benecaster_email_headers',
function ( array $headers, string $type, int $user_id, int $show_id ): array {
$smtpapi = [
'category' => [ 'benecaster', 'benecaster_' . $type ],
'unique_args' => [
'benecaster_type' => $type,
'user_id' => (string) $user_id,
'show_id' => (string) $show_id,
],
];
$headers[] = 'X-SMTPAPI: ' . wp_json_encode( $smtpapi );
return $headers;
},
10,
4
);
Hooks Used
Need this built rather than just documented? See our services →