Add an email address (or two) as BCC to all WooCommerce emails.
About 3 years ago I was helping a fellow developer debug WooCommerce email issues where variation information was not being included in the emails. In that situation I redirected order emails to me when I was logged in.
In this post I want to be BCCd on WooCommerce emails without disrupting emails. This will allow me to see real order emails and not just debug orders. It turns out to be easier than I expected.
It didn’t take long to find that the email headers have the ‘woocommerce_email_headers‘ filter. During my search I could see that the headers is a string with each entry on its own line.
The filter function receives 4 parameters – the headers string, the email ID (a string), the object the email is for and the email object. Each email type has a different ID, for example, the ID of the email sent to a customer when it is completed is ‘customer_completed_order‘, the ID of the new order notification sent to the shop manager is ‘new_order‘.
I have a small plugin that logs all emails sent by WordPress. I find this very useful and used it while debugging this new code. I was able to see that the Bcc line was correctly added to the headers string.
The simplest usage is to add a Bcc to every email.
By examining the $email_id parameter you can target a specific email. For example, earlier this year I wrote about Add Tracking Info to WooCommerce order. It added information to the email with ID of ‘customer_completed_order‘. I could “watch” those emails by limiting the code to only adding my email address for those emails.
Remember, as this is a filter function you must return something, even if you do not change the data.