WooCommerce does not include the customer's name in the To: field of the headers when sending out emails. I'm trying to add them myself with the WooCommerce email recipient filters, but I'm running into a bit of a roadblock.
The code is being triggered as expected, but the messages don't seem to be arriving.
Here's my current code:
function add_customer_name_to_email_header($recipient, $order) {
// Don't run if something else has already modified it and added a name somehow.
if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();
$recipient_name = trim($first_name . ' ' . $last_name);
$recipient_name = sanitize_text_field($recipient_name);
if (!empty($recipient_name)) {
$recipient = sprintf('"%s" <%s>', htmlspecialchars($recipient_name), $recipient);
}
}
return $recipient;
}
add_filter('woocommerce_email_recipient_customer_note', 'add_customer_name_to_email_header', 10, 2);
add_filter('woocommerce_email_recipient_customer_completed_order', 'add_customer_name_to_email_header', 10, 2);
add_filter('woocommerce_email_recipient_customer_invoice', 'add_customer_name_to_email_header', 10, 2);
add_filter('woocommerce_email_recipient_customer_processing_order', 'add_customer_name_to_email_header', 10, 2);
I've confirmed that it is properly generating the correctly-formatted value for that field, e.g.: "John Doe" <jdoe@example.com>
, but messages don't actually arrive once the $recipient value has been modified.
My suspicions are that WooCommerce or WordPress are further sanitizing the recipient value before the email is sent, probably stripping out or modifying characters like < and >, but I don't know for sure. Is there any existing method for this I just haven't found, or a known way around this?