Send Custom HTML Email Templates in WordPress with wp_mail()

Send HTML emails from WordPress with your own template

Every WordPress site sends emails: password resets, new user notifications, form submissions, order confirmations. By default wp_mail() sends them as plain text. For a branded email we need two things: HTML content type and a template that looks the same in all email clients, including Outlook.

Problem: Send a styled HTML email with a common header and footer and variable content from a theme or plugin, without an email plugin.

Solution: Keep the email markup in a PHP template file, render it into a string with output buffering, and send it with wp_mail() while the wp_mail_content_type filter returns text/html only for this one email.

Step 1. The sending function

Add to functions.php or a plugin:

<?php
function send_custom_email( $to, $subject, $template_file, $variables = array() ) {
    if ( ! file_exists( $template_file ) ) {
        return false;
    }

    // Render the template into a string, with the variables available inside it
    extract( $variables, EXTR_SKIP );
    ob_start();
    include $template_file;
    $message = ob_get_clean();

    $headers = array( 'From: ' . get_bloginfo( 'name' ) . ' <noreply@example.com>' );

    // HTML only for this email
    add_filter( 'wp_mail_content_type', 'custom_email_html_content_type' );
    $result = wp_mail( $to, $subject, $message, $headers );
    remove_filter( 'wp_mail_content_type', 'custom_email_html_content_type' );

    return $result;
}

function custom_email_html_content_type() {
    return 'text/html';
}

The filter is added right before sending and removed right after, so other emails in the same request (from WordPress or other plugins) keep their own content type. Use a real mailbox on your domain in the From header, otherwise the email is likely to land in spam.

Step 2. The template

Save it as email-templates/order-notify.php in the theme. Email clients, especially Outlook, ignore most modern CSS, so the only reliable layout is old-school: tables and inline styles.

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"></head>
<body style="margin:0;padding:0;background:#f4f4f4;font-family:Arial,sans-serif;">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr>
    <td align="center" style="padding:40px 0;">
      <table width="600" cellpadding="0" cellspacing="0" border="0" style="background:#ffffff;padding:40px;">
        <tr>
          <td style="font-size:24px;font-weight:bold;color:#333;padding-bottom:20px;">
            <?php echo esc_html( get_bloginfo( 'name' ) ); ?>
          </td>
        </tr>
        <tr>
          <td style="font-size:16px;color:#555;line-height:1.6;padding-bottom:20px;">
            <p>Hello <?php echo esc_html( $customer_name ); ?>,</p>
            <p>Your order <strong>#<?php echo esc_html( $order_id ); ?></strong> has been received.
               Total: <strong><?php echo wp_kses_post( $order_total ); ?></strong></p>
            <p><a href="<?php echo esc_url( $order_url ); ?>" style="background:#0073aa;color:#fff;padding:10px 20px;text-decoration:none;">View Order</a></p>
          </td>
        </tr>
        <tr>
          <td style="font-size:12px;color:#999;border-top:1px solid #eee;padding-top:20px;">
            &copy; <?php echo esc_html( date( 'Y' ) . ' ' . get_bloginfo( 'name' ) ); ?>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>

$order_total is printed with wp_kses_post() because the formatted WooCommerce total contains HTML (the currency symbol in a <span>).

Step 3. Send it from a hook

For example, an extra email to the customer after a WooCommerce order:

<?php
add_action( 'woocommerce_thankyou', 'send_custom_order_email' );

function send_custom_order_email( $order_id ) {
    $order = wc_get_order( $order_id );
    if ( ! $order || $order->get_meta( '_custom_email_sent' ) ) {
        return;
    }

    $sent = send_custom_email(
        $order->get_billing_email(),
        'Order Confirmation #' . $order->get_order_number(),
        get_template_directory() . '/email-templates/order-notify.php',
        array(
            'customer_name' => $order->get_billing_first_name(),
            'order_id'      => $order->get_order_number(),
            'order_total'   => $order->get_formatted_order_total(),
            'order_url'     => $order->get_view_order_url(),
        )
    );

    if ( $sent ) {
        $order->update_meta_data( '_custom_email_sent', 1 );
        $order->save();
    }
}

woocommerce_thankyou runs every time the thank-you page is opened, so the order meta flag makes sure the email goes out only once.

NOTE: wp_mail() returns true when PHP handed the message to the mail server, not when it was delivered. If emails do not arrive, the cause is usually the server (no SPF/DKIM for the domain), and sending through SMTP with an SMTP plugin or your own phpmailer_init hook solves it.