How to send custom emails using the WooCommerce template

I used to believe that if you have to send custom emails within WooCommerce, then the only way possible is by creating a custom Email which extends WC_Email class. That process is tedious and honestly very few would need that.

I recently stumbled upon a code within WooCommerce which uses the WC()->mailer->wrap_message() function to send email in WooCommerce’s style with a few lines of code and got really happy as this solution isn’t already mentioned anywhere in the Google.

If like me, you would also like to send email’s with the WC template and don’t want to create your own custom WC_Email class then you would find this function useful.

/**
 * Send email using the WooCommerce template.
 *
 * @param string $to            Receiver's email.
 * @param string $email_subject Email subject.
 * @param string $body_heading  Title which appears at the top in large fonts.
 * @param string $body_message  Email text.
 *
 * @return bool
 */
function wc_send_email( $to, $email_subject, $body_heading, $body_message ) {
	$mailer  = WC()->mailer();
	$message = $mailer->wrap_message( $body_heading, $body_message );
	return $mailer->send( $to, wp_strip_all_tags( $email_subject ), $message );
}

Let me know if this snippet also becomes your go-to solution in future.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Up Next:

Function to Check if given Array of Product IDs are present in the Cart

Function to Check if given Array of Product IDs are present in the Cart