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 code
Options
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.
Code seems incomplete.
Error generated: PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function dcwd_woocommerce_email_headers(), 3 passed in F:\laragon\www\iGMS\wp-includes\class-wp-hook.php on line 287 and exactly 4 expected
Please review and fix the code.
Thanks
@KoolPal – I tried the code and it works without an error. I have WooCommerce 4.2.0. Since WooCommerce 3.7 the filter passes 4 parameters. What version of WooCommerce are you using?
Over email KoolPal told me that YITH Request a Quote plugin was installed. I found that this plugin calls the 3 parameter version of the filter function (PHP is okay if you omit some parameters).
I reworked the add_filter() function to call a 3 parameter wrapper version when YITH Request a Quote is active. I had to use a new add_action() call because I could not detect that plugin until quite late.
Here’s the updated code: https://gist.github.com/damiencarbery/d045aa304144320b85af49777deaca52#file-wc-add-bcc-to-emails-with-yith-php
Hey, i also use this codesnippet. But i have a “feaky” bug.
Send with PHP_Mail() only the main receiver in Woo-Settings get the mail. The Mail from snippet dont get the Mail.
If i send with SMTP Plugin all is working fine.
@Tobias – Please try the code in my Debugging WordPress Email Issues post. It will log the information sent to wp_mail(). Check the log to see if your Bcc email address is listed. If it is not listed then something is removing it.
Where should this code go? In theme’s function.php or…
I’ve added it using https://github.com/srikat/my-custom-functionality and it seems to work. Only one thing… BCC email address now gets an email for both customer and for the seller. Can you please help with customization of the code so that it BCCs only emails sent to the seller?
I use the Snippets plugin. Can this be added as a snippet instead of installing it as a plugin?
@Steve M.: Thanks for the question.
Yes, you can add the code as a snippet instead of installing as a plugin – remove the opening <?php line.
I prefer the plugin method as it’s more secure (in case there was every a security issue with the snippets plugin). I give instructions at: https://www.damiencarbery.com/2018/10/how-to-use-my-code-snippets/