Halloween Sale 15% off Ends in See the plans
PHP

Add a Signature to Every WordPress Email

Appends a short signature, such as your company name, address and support link, to the bottom of every email the site sends, in both plain-text and HTML emails.

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

Code

$wpide_signature = array(
    'Example Ltd · 1 Main Street, Springfield',
    'Need help? [email protected]',
);

add_filter( 'wp_mail', function ( $atts ) use ( $wpide_signature ) {
    $message = (string) $atts['message'];
    $headers = is_array( $atts['headers'] ) ? implode( "\n", $atts['headers'] ) : (string) $atts['headers'];
    $is_html = false !== stripos( $headers, 'text/html' ) || 'text/html' === apply_filters( 'wp_mail_content_type', 'text/plain' );

    if ( $is_html ) {
        $block = '<p style="margin-top:24px;padding-top:12px;border-top:1px solid #ddd;color:#666;font-size:12px;">' . implode( '<br>', array_map( 'esc_html', $wpide_signature ) ) . '</p>';
        $atts['message'] = false !== stripos( $message, '</body>' ) ? preg_replace( '#</body>#i', $block . '</body>', $message, 1 ) : $message . $block;
    } else {
        $atts['message'] = rtrim( $message ) . "\n\n-- \n" . implode( "\n", $wpide_signature ) . "\n";
    }

    return $atts;
}, 30 );

Notes

Edit the lines at the top; HTML emails get the signature before the closing body tag.

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.