Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 61 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save craigedmonds/b65c65e02fd40bd381bf023ffef2c80e to your computer and use it in GitHub Desktop.
Save craigedmonds/b65c65e02fd40bd381bf023ffef2c80e to your computer and use it in GitHub Desktop.
<?php
/*
This script will allow you to send a custom email from anywhere within wordpress
but using the woocommerce template so that your emails look the same.
Created by craig@123marbella.com on 27th of July 2017
Put the script below into a function or anywhere you want to send a custom email
*/
function get_custom_email_html( $order, $heading = false, $mailer ) {
$template = 'emails/my-custom-email-i-want-to-send.php';
return wc_get_template_html( $template, array(
'order' => $order,
'email_heading' => $heading,
'sent_to_admin' => false,
'plain_text' => false,
'email' => $mailer
) );
}
// load the mailer class
$mailer = WC()->mailer();
//format the email
$recipient = "someone@somewhere.com";
$subject = __("Hi! Here is a custom notification from us!", 'theme_name');
$content = get_custom_email_html( $order, $subject, $mailer );
$headers = "Content-Type: text/html\r\n";
//send the email through wordpress
$mailer->send( $recipient, $subject, $content, $headers );
@sobuzislam123
Copy link

is it possible to show here $subject = __("Hi! Here is a custom notification from us!", 'theme_name'); product short description ?

@petrone7
Copy link

Great! Thank you for this code!

@medAmarFilali
Copy link

This code saved me a lot of headaches, thank you!!

@8ctopotamus
Copy link

Just what I was looking for. Thanks for sharing!

@FluxCoder
Copy link

I know this has been here for ages, but I want to store the template in a plugin folder instead of the woocommerce folder, does anyone know how this is possible to do?

Whenever I try to do it, there will be an error because the plugin is expecting the template to be stored in the WooCommerce folder and no where else.

@vladoATL
Copy link

@FluxCoder, have your resolved the problem? I need to send emails from my own plugin using WC email template. I cannot find how to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment