html emails
-
Hi Markus,
I would like to send html emails with this plugin. Can you give me a hint? Is there an easy way?
-
Hi @wpturk, try something like this (e.g. added to your theme’s functions.php):
add_filter( 'bbp_subscription_email_headers', function( $headers ) { array_push( $headers, 'MIME-Version: 1.0', 'Content-Type: text/html; charset=UTF-8' ); return $headers; } ); add_filter( 'bbp_forum_subscription_email_message', function( $message, $forum_id, $topic_id ) { $topic_title = wp_specialchars_decode( strip_tags( bbp_get_topic_title( $topic_id ) ), ENT_QUOTES ); $topic_author_display_name = bbp_get_topic_author_display_name( $topic_id ); $topic_url = get_permalink( $topic_id ); $topic_content = wp_specialchars_decode( strip_tags( bbp_get_topic_content( $topic_id ) ), ENT_QUOTES ); return sprintf( '<p>%s posted new topic <a href="%s">%s</a></p><p>%s</p>', $topic_author_display_name, $topic_url, $topic_title, $topic_content ); } ); add_filter( 'bbp_topic_subscription_email_message', function( $message, $forum_id, $topic_id, $reply_id ) { $reply_author_display_name = bbp_get_reply_author_display_name( $reply_id ); $reply_url = bbp_get_reply_url( $reply_id ); $reply_content = wp_specialchars_decode( strip_tags( bbp_get_reply_content( $reply_id ) ), ENT_QUOTES ); return sprintf( '<p>%s replied:</p><p>%s</p><p><a href="%s">Read Online</a></p>', $reply_author_display_name, $reply_content, $reply_url ); } );
Thank you Markus, this is great. I will test.
Do you know any easy way to use an email template, so that your functions only push topic_title, topic_author_display_name, topic_url, topic_content etc. and all html/css stuff comes from the template.
Sure. In the above example code, the message filters return an html string. That string could also be loaded from a template by using php output buffers like so:
ob_start(); include( get_stylesheet_directory() . '/email_templates/new_topic_message.html' ); $ob = ob_get_clean();
Then you simply replace any tokens in the template with author name, content etc and return the result.
Hi Markus, I get somehow blank page after posting a reply. I used your above code in functions.php
add_filter( 'bbp_subscription_email_headers', function( $headers ) { array_push( $headers, 'MIME-Version: 1.0', 'Content-Type: text/html; charset=UTF-8' ); return $headers; } ); add_filter( 'bbp_forum_subscription_email_message', function( $message, $forum_id, $topic_id ) { $topic_title = wp_specialchars_decode( strip_tags( bbp_get_topic_title( $topic_id ) ), ENT_QUOTES ); $topic_author_display_name = bbp_get_topic_author_display_name( $topic_id ); $topic_url = get_permalink( $topic_id ); $topic_content = wp_specialchars_decode( strip_tags( bbp_get_topic_content( $topic_id ) ), ENT_QUOTES ); return sprintf( '<p>%s posted new topic <a href="%s">%s</a></p><p>%s</p>', $topic_author_display_name, $topic_url, $topic_title, $topic_content ); } ); add_filter( 'bbp_topic_subscription_email_message', function( $message, $forum_id, $topic_id, $reply_id ) { $reply_author_display_name = bbp_get_reply_author_display_name( $reply_id ); $reply_url = bbp_get_reply_url( $reply_id ); $reply_content = wp_specialchars_decode( strip_tags( bbp_get_reply_content( $reply_id ) ), ENT_QUOTES ); return sprintf( '<p>%s replied:</p><p>%s</p><p><a href="%s">Read Online</a></p>', $reply_author_display_name, $reply_content, $reply_url ); } );
Sorry about that. The code was untested. The number of filter arguments was missing and also I discovered that the MIME type was added a second time by WordPress, so I removed that part. Here is a tested version that works for me:
add_filter( 'bbp_subscription_email_headers', function( $headers ) { $headers []= 'Content-Type: text/html; charset=UTF-8'; return $headers; } ); add_filter( 'bbp_forum_subscription_email_message', function( $message, $forum_id, $topic_id ) { $topic_title = wp_specialchars_decode( strip_tags( bbp_get_topic_title( $topic_id ) ), ENT_QUOTES ); $topic_author_display_name = bbp_get_topic_author_display_name( $topic_id ); $topic_url = get_permalink( $topic_id ); $topic_content = wp_specialchars_decode( strip_tags( bbp_get_topic_content( $topic_id ) ), ENT_QUOTES ); return sprintf( '<p>%s posted new topic <a href="%s">%s</a></p><p>%s</p>', $topic_author_display_name, $topic_url, $topic_title, $topic_content ); }, 10, 3 ); add_filter( 'bbp_topic_subscription_email_message', function( $message, $forum_id, $topic_id, $reply_id ) { $reply_author_display_name = bbp_get_reply_author_display_name( $reply_id ); $reply_url = bbp_get_reply_url( $reply_id ); $reply_content = wp_specialchars_decode( strip_tags( bbp_get_reply_content( $reply_id ) ), ENT_QUOTES ); return sprintf( '<p>%s replied:</p><p>%s</p><p><a href="%s">Read Online</a></p>', $reply_author_display_name, $reply_content, $reply_url ); }, 10, 4 );
*MIME version, not type.
Works like a charm! Thanks for your 5 star support!
Glad I could help. Thanks for your friendly review. ??
- The topic ‘html emails’ is closed to new replies.