• Resolved Lab41

    (@lab41)


    Hi

    I want to keep using this plugin, but I need to also be able to apply the template to HTML emails.

    Will this ever be integrated?

    The reason is that I have about 200 different email notifications, and I want to centrally manage the header & footer of these HTML emails. (Otherwise it’s such a mission to update the header & footer in the future!)

    What’s the best way to do this?

    https://www.ads-software.com/plugins/wp-better-emails/

Viewing 15 replies - 1 through 15 (of 17 total)
  • Plugin Author Nicolas Lemoine

    (@nlemoine)

    Hi,

    The plugin doesn’t do anything when emails are already text/html in order to avoid conflicts and/or code mess. For example, if another plugin adds a template, the email would be “templated” twice.

    In your specific case, if you are sure that wrapping HTML emails won’t mess everything up, you can force emails content type to be set to text/plain, just before WP Better Emails checks for content type and decide to wrap it or not:

    add_filter( 'wp_mail_content_type', 'wpbe_force_plain_text', 99 );
    function wpbe_force_plain_text( $content_type ) {
        return 'text/plain';
    }

    Paste the code below in your theme’s functions.php file.

    Let me know if it works for you.

    Thread Starter Lab41

    (@lab41)

    Alas that didn’t work. ??

    Is there a way to maybe only apply it to emails from one certain plugin?

    Plugin Author Nicolas Lemoine

    (@nlemoine)

    The posted solution should actually work. If you fool the plugin saying the content type of the message is text/plain, then the template is applied.

    What exactly didn’t work?

    Thread Starter Lab41

    (@lab41)

    Now it’s working! How strange! I guess it was a cache issue.

    This is SUPERB news!

    Thanks.

    Thread Starter Lab41

    (@lab41)

    The only issue is that it adds extra spaces between
    <p>TEXT</p>

    It adds a blank line between each paragraph.

    Plugin Author Nicolas Lemoine

    (@nlemoine)

    The plugin applies some formatting operations on content to make it more HTML friendly. It converts line break into <br /> tags and make links out of URL.
    So your paragraphs probably get a <br /> and consequently add an extra line break.

    Two dirty solutions I didn’t test.

    Remove all HTML tags from content before the email is processed by WP Better Emails:

    add_action( 'phpmailer_init', 'wpbe_strip_html_tags', 9);
    function wpbe_strip_html_tags( $phpmailer ) {
        $phpmailer->Body = strip_tags($phpmailer->Body);
    }

    Remove only line breaks on the HTML body:

    add_filter( 'wpbe_html_body', 'wpbe_remove_br');
    function wpbe_remove_br( $message ) {
        return str_replace(array('<br />', '<br>'), '', $message);
    }

    You should probably try both. Choose the one (and only one) that fits better your situation. It depends on the content of your emails.

    Thread Starter Lab41

    (@lab41)

    The second one works like a charm! Many Thanks.

    Thread Starter Lab41

    (@lab41)

    It’s almost ALL working except I just hit a big bug.
    It’s now breaking my plain text emails by stripping out all the spaces!
    Is there a way to limit it ONLY to HTML emails?

    Thread Starter Lab41

    (@lab41)

    Sorry for the obvious question but do I just add this action below the functions?

    Where do I add this?
    add_action( ‘phpmailer_init’, ‘wpbe_strip_html_tags’, 9);
    function wpbe_strip_html_tags( $phpmailer ) {
    $phpmailer->Body = strip_tags($phpmailer->Body);
    }

    Thread Starter Lab41

    (@lab41)

    I’ve also encountered another pretty serious bug.(I think it’s a bug)
    This doesn’t get detected as a link:
    <a href="https://website.com/wp-admin/admin.php?page=wpclients_invoicing&tab=invoice_edit">Create Invoice</a>.
    –> This is the main reason I’m moving over to HTML so I can make neat links!
    –> It shows like this in the email

    href="https://lab41.co/wp-admin/admin.php?page=wpclients_invoicing&tab=invoice_edit">Create
    Invoice.

    Whereas this link works perfectly:
    <a href="https://website.com/wp-admin/admin.php"><strong>Create Invoice</strong></a>.

    Thread Starter Lab41

    (@lab41)

    I’ve realised that you can’t edit old posts so I want to sum up the two major issues I’m still having.

    Issue 1: Extra spaces between all paragraphs.
    I tried

    add_filter( 'wpbe_html_body', 'wpbe_remove_br');
    function wpbe_remove_br( $message ) {
        return str_replace(array('<br />', '<br>'), '', $message);
    }

    But it then broke all my other plain text emails, by removing all spaces there too! (I have a mix of HTML and plain text emails on my site)

    1)is there a way to only apply it to HTML emails and skip this function on plain text emails?

    2) I didn’t try this as wasn’t sure how to use it.
    Do I just insert it in my functions?

    add_action( 'phpmailer_init', 'wpbe_strip_html_tags', 9);
    function wpbe_strip_html_tags( $phpmailer ) {
        $phpmailer->Body = strip_tags($phpmailer->Body);
    }

    –> will it only apply to HTML emails?

    Issue 2: Not Converting Links Properly
    I’ve used the following in an HTML email.
    <a href="https://website.com/wp-admin/admin.php?page=wpclients_invoicing&tab=invoice_edit">Create Invoice</a>.

    BUT it’s not detecting it as a link.
    It shows it as

    href="https://lab41.co/wp-admin/admin.php?page=wpclients_invoicing&tab=invoice_edit">Create
    Invoice.

    –> This is the main reason to move over to HTML to make these links friendly.

    I tested this link in the email and it works like a charm, so it’s only the longer ones that break.

    Whereas this link works perfectly:
    <a href="https://website.com/wp-admin/admin.php"><strong>Create Invoice</strong></a>.

    I tested this in two different emails from different plugins and it didn’t work in the one but worked in the other. I will ask that plugin author why, but if you have any ideas please let me know!

    Sorry for all the posts, but I was encountering issues as I tried different things! I really appreciate your help & support!

    Plugin Author Nicolas Lemoine

    (@nlemoine)

    It’s now breaking my plain text emails by stripping out all the spaces!

    Can you be more accurate in your descriptions ? None of the solutions removes white spaces (the first one removes HTML tags, the other one removes HTML line break tag). BTW, who cares about plain/text emails? 99% of your users won’t even see it.

    Is there a way to limit it ONLY to HTML emails?

    The actions/filters I posted should only be applied to the HTML part.

    Sorry for the obvious question but do I just add this action below the functions?

    Your can paste the code blocks as they are in your functions.php file or in a plugin/plugin-mu file.

    I can’t really understand what is the exact problem about links. This is for sure caused by the action that make links out of URLs. Try to remove the link tags.

    I’m providing support for something that’s not a bug in my plugin, please try to post as much information as possible (paste source code with something like https://codepad.org/, etc.).

    Thread Starter Lab41

    (@lab41)

    Hi

    1) I’ve used the other in a functions now and it seems to all be working! Great news!
    2) Regarding broken links, here is what I mean in more details:
    https://codepad.org/7KccLp0E
    (There’s a URL at the top with a demo)

    Many Thanks! I’m super greatful for the support you’ve provided and I love the new Mandrill feature!

    Plugin Author Nicolas Lemoine

    (@nlemoine)

    Thanks for the testing page, makes my life easier. Knowing that you’re using WpMandrill should also have been mentionned before as the process in not exaclty the same with WpMandrill.

    So let’s start over… Because you’re in a WpMandrill case, the email doesn’t even go through the phpmailer_init action.

    // remove native wpbe filter
    remove_filter( 'mandrill_payload', array($wp_better_emails, 'wpmandrill_compatibility') );
    
    // do your own email processing
    add_filter( 'mandrill_payload', 'wpbe_wpmandrill_compatibility' );
    function wpbe_wpmandrill_compatibility( $message ) {
    
    	global $wp_better_emails;
    
    	// make sure you have the %content% tag in the template
    	if ( $wp_better_emails->check_template() ) {
    		// clean < and > around text links in WP 3.1
    		$message['html'] = $wp_better_emails->esc_textlinks( $message['html'] );
    		// make links out of URLs
    		$message['html'] = make_clickable( $message['html'] );
    		// set template
    		$message['html'] = $wp_better_emails->set_email_template( $message['html'] );
    		// replace variables
    		$message['html'] = apply_filters( 'wpbe_html_body', $wp_better_emails->template_vars_replacement( $message['html'] ) );
    	}
    
    	return $message;
    }

    Remove the other pieces of code I gave you and paste this one.

    If you have problems with links, try to remove or comment the make_clickable line.

    Thread Starter Lab41

    (@lab41)

    Hi

    First up, thanks for taking the time to help out here!
    I’ve tried the new code and also commented out the clickable line.

    Here is what it looks like with new code sample:
    https://www.mediafire.com/?klcdj0p62t3d5f2
    https://www.mediafire.com/view/9w6mgcq983d48r0/Code1.jpg

    It worked mostly with the previous code, it was only the one plugin that caused the HTML links to break. Maybe it was an issue with that plugin?

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘HTML Email Format’ is closed to new replies.