čtvrtek 15. ledna 2015

WooCommerce + WordPress new line issue with Mandrill

As I stated in previous topic after installing Mandrill my WordPress text/plain emails doesnt contain new line breaks, because they are coded with /n/r code.

There is an option in settings of Mandrill, that you can:
Replace all line feeds ("\n") by <br/>in the message body?

But if you enable this the WooCommerce emails are going to be ugly, because they are already HTML emails, not just plain text emails, which are sended out by WordPress by default.

To solve the issue you need to use the mandrill_playload hook which is just fired before an email sents out.
add_filter('mandrill_payload', 'customFilterName');
 More about this filter: http://blog.mandrill.com/own-your-wordpress-email-with-mandrill.html

What you need to do is select the type of emails with tags which are plain/text emails only.

You need to get the tags from your Mandrill admin.


As you can see, these are the tags in my case.

The wp_WC_Email->send work just fine, because this is the tag for WooCommerce store emails, these are coded with text/html.

What I needed to add is: wp_userpro_mail, wp_template-contact.php and wp_wp_new_user_notification, because these are encoded only in text/plain.

To bypass this and insert correct line breaks tags you just to need to add the following to your functions.php in your theme.

 /*************************************************************************/
 /*WPMandrill playload, add <br> to emails*/
 /*************************************************************************/
 function wd_mandrill_woo_order( $message ) {
      $tags = $message['tags']['automatic'];
   if ( (in_array( 'wp_template-contact.php', $tags )) || (in_array( 'wp_userpro_mail', $tags )) || (in_array( 'wp_wp_new_user_notification', $tags )) ) {
     $message['html'] = nl2br( $message['html'] );
   }
   return $message;
 }
 add_filter( 'mandrill_payload', 'wd_mandrill_woo_order' );

The key functions here is the nl2br which just returns string with '<br />' or '<br>' inserted before all newlines (\r\n, \n\r, \n and \r).

After this little code snippet every system emails from WordPress are correctly displayed :)))

Maybe someone is facing the same issue and I helped him with this :).

1 komentář:

  1. Thank you for sharing excellent information. Your website is very cool. Fully useful your blog post... Online shopping website in india

    OdpovědětVymazat