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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.