Halloween Sale 15% off Ends in See the plans
PHP

Use Resend to Deliver WordPress Email

Delivers all WordPress emails through the Resend email API with a single API key, a simple way to get reliable delivery without SMTP.

Don’t have WPIDE yet? Install it on your site

Code

add_filter( 'pre_wp_mail', function ( $return, $atts ) {
    if ( null !== $return || ! defined( 'WPIDE_RESEND_KEY' ) || ! WPIDE_RESEND_KEY || ! empty( $atts['attachments'] ) ) {
        return $return;
    }

    $lines = is_array( $atts['headers'] ) ? $atts['headers'] : explode( "\n", str_replace( "\r\n", "\n", (string) $atts['headers'] ) );
    $h     = array( 'from' => '', 'reply-to' => array(), 'cc' => array(), 'bcc' => array(), 'content-type' => '' );
    foreach ( $lines as $line ) {
        if ( false === strpos( (string) $line, ':' ) ) {
            continue;
        }
        list( $name, $value ) = array_map( 'trim', explode( ':', $line, 2 ) );
        $name = strtolower( $name );
        if ( in_array( $name, array( 'reply-to', 'cc', 'bcc' ), true ) ) {
            $h[ $name ] = array_merge( $h[ $name ], array_filter( array_map( 'trim', explode( ',', $value ) ) ) );
        } elseif ( isset( $h[ $name ] ) ) {
            $h[ $name ] = $value;
        }
    }

    $to      = is_array( $atts['to'] ) ? $atts['to'] : explode( ',', (string) $atts['to'] );
    $is_html = false !== stripos( $h['content-type'], 'text/html' ) || 'text/html' === apply_filters( 'wp_mail_content_type', 'text/plain' );

    $body = array(
        'from'    => $h['from'] ? $h['from'] : sprintf( '%s <%s>', apply_filters( 'wp_mail_from_name', 'WordPress' ), apply_filters( 'wp_mail_from', get_option( 'admin_email' ) ) ),
        'to'      => array_values( array_filter( array_map( 'trim', $to ) ) ),
        'subject' => (string) $atts['subject'],
    );
    $body[ $is_html ? 'html' : 'text' ] = (string) $atts['message'];
    if ( $h['cc'] ) {
        $body['cc'] = $h['cc'];
    }
    if ( $h['bcc'] ) {
        $body['bcc'] = $h['bcc'];
    }
    if ( $h['reply-to'] ) {
        $body['reply_to'] = $h['reply-to'];
    }

    $response = wp_remote_post( 'https://api.resend.com/emails', array(
        'timeout' => 15,
        'headers' => array( 'Authorization' => 'Bearer ' . WPIDE_RESEND_KEY, 'Content-Type' => 'application/json' ),
        'body'    => wp_json_encode( $body ),
    ) );

    $code = (int) wp_remote_retrieve_response_code( $response );
    if ( $code >= 200 && $code < 300 ) {
        do_action( 'wp_mail_succeeded', $atts );
        return true;
    }

    $message = is_wp_error( $response ) ? $response->get_error_message() : 'Resend returned HTTP ' . $code . ': ' . wp_remote_retrieve_body( $response );
    do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $message, $atts ) );

    return false;
}, 10, 2 );

Notes

Define WPIDE_RESEND_KEY in wp-config.php and send from a domain verified in Resend; emails with attachments fall back to the normal mailer.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.