Skip to main content

Suppress welcome email and send custom version via SendGrid

Free Beginner Since v1.0.0

Benecaster’s built-in welcome email covers the basics, but many podcasters have existing welcome sequences in SendGrid with branding, sequences, and conditional logic that the built-in template can’t replicate. Return false from benecaster_email_should_send for the ‘welcome’ type, then fire your own SendGrid API call instead.

Code

<?php
add_filter(
    'benecaster_email_should_send',
    function ( bool $should, string $type, string $to, int $user_id, int $show_id ): bool {
        if ( 'welcome' !== $type ) {
            return $should;
        }
        wp_remote_post(
            'https://api.sendgrid.com/v3/mail/send',
            [
                'timeout'  => 5,
                'blocking' => false,
                'headers'  => [
                    'Content-Type'  => 'application/json',
                    'Authorization' => 'Bearer ' . SENDGRID_API_KEY,
                ],
                'body'     => wp_json_encode( [
                    'from'                    => [ 'email' => get_option( 'admin_email' ), 'name' => get_bloginfo( 'name' ) ],
                    'personalizations'        => [ [
                        'to'                    => [ [ 'email' => $to ] ],
                        'dynamic_template_data' => [
                            'first_name' => get_userdata( $user_id )->first_name,
                            'show_name'  => get_the_title( $show_id ),
                        ],
                    ] ],
                    'template_id'             => 'd-your-sendgrid-template-id',
                ] ),
            ]
        );
        return false; // Benecaster's own welcome is skipped.
    },
    10,
    5
);

View on GitHub →

Hooks Used