Send emails with WordPress

In this quickstart, you'll learn how to send transactional emails from your WordPress site using the Sidemail API. We'll use the native wp_remote_post function, so you don't need to install any external libraries or Composer packages.

Before you start

  1. Create a Sidemail account → get your API key
  2. Add a sending domain → set up your domain for sending

1. Add your API key

Add your API key to your wp-config.php file to keep it secure and accessible.

// wp-config.php
define( 'SIDEMAIL_API_KEY', 'your-api-key' );

2. Create a helper function

Add this function to your theme's functions.php or a custom plugin. This wrapper handles authentication and error checking.

// functions.php

function sidemail_send_email( $args ) {
    $api_key = defined( 'SIDEMAIL_API_KEY' ) ? SIDEMAIL_API_KEY : '';

    if ( empty( $api_key ) ) {
        return new WP_Error( 'missing_key', 'Sidemail API key is not defined.' );
    }

    $response = wp_remote_post( 'https://api.sidemail.io/v1/email/send', array(
        'headers' => array(
            'Authorization' => 'Bearer ' . $api_key,
            'Content-Type'  => 'application/json',
        ),
        'body'    => json_encode( $args ),
        'timeout' => 15,
    ) );

    if ( is_wp_error( $response ) ) {
        return $response;
    }

    $body = wp_remote_retrieve_body( $response );
    $code = wp_remote_retrieve_response_code( $response );

    if ( $code >= 400 ) {
        return new WP_Error( 'api_error', 'Sidemail API Error: ' . $body );
    }

    return json_decode( $body );
}

3. Send a welcome email

Hook into the user_register action to send an email when a new user signs up.

add_action( 'user_register', 'my_send_welcome_email', 10, 1 );

function my_send_welcome_email( $user_id ) {
    $user = get_userdata( $user_id );

    $result = sidemail_send_email( array(
        'toAddress'     => $user->user_email,
        'fromAddress'   => '[email protected]',
        'fromName'      => 'Your Site',
        'templateName'  => 'Welcome',
        'templateProps' => array(
            'username' => $user->user_login,
        ),
    ) );

    if ( is_wp_error( $result ) ) {
        error_log( $result->get_error_message() );
    }
}

4. Send a password reset email

You can override the default WordPress password reset email by hooking into retrieve_password_message.

add_filter( 'retrieve_password_message', 'my_custom_password_reset', 10, 4 );

function my_custom_password_reset( $message, $key, $user_login, $user_data ) {
    // Send via Sidemail
    $reset_url = network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user_login ), 'login' );

    sidemail_send_email( array(
        'toAddress'     => $user_data->user_email,
        'fromAddress'   => '[email protected]',
        'fromName'      => 'Your Site',
        'templateName'  => 'Password Reset',
        'templateProps' => array(
            'actionUrl' => $reset_url,
            'username'  => $user_login,
        ),
    ) );

    // Return false to stop WordPress from sending the default email
    return false;
}

5. Send a contact form submission

If you are processing a custom contact form:

function handle_contact_form_submission() {
    // ... validate nonce and fields ...
    $email = sanitize_email( $_POST['email'] );
    $name  = sanitize_text_field( $_POST['name'] );

    sidemail_send_email( array(
        'toAddress'     => '[email protected]',
        'fromAddress'   => '[email protected]',
        'fromName'      => 'Contact Form',
        'templateName'  => 'New Inquiry',
        'templateProps' => array(
            'replyTo' => $email,
            'name'    => $name,
            'message' => sanitize_textarea_field( $_POST['message'] ),
        ),
    ) );
}

6. Send HTML email

sidemail_send_email( array(
    'toAddress'   => '[email protected]',
    'fromAddress' => '[email protected]',
    'fromName'    => 'Your Site',
    'subject'     => 'Hello from WordPress',
    'html'        => '<html><body><h1>Hello world! 👋</h1></body></html>',
) );

7. Schedule email

Send email later. Set scheduledAt to an ISO date string.

$scheduled_at = gmdate( 'c', time() + 3600 ); // 1 hour from now

sidemail_send_email( array(
    'toAddress'     => '[email protected]',
    'fromAddress'   => '[email protected]',
    'fromName'      => 'Your Site',
    'templateName'  => 'Welcome',
    'scheduledAt'   => $scheduled_at,
) );