# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Email](https://wpide.com/library/category/email/)
- Tags: email, resend, api
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/email-send-via-resend/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Use Resend to Deliver WordPress Email" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
