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 );
@EdgarValfogo
Copy link

Thank you for saving me!

@hmorv
Copy link

hmorv commented Jan 3, 2018

great! thanks so much!

@beneverard
Copy link

Very useful indeed, thank you for sharing this.

@luancmaia
Copy link

thank you!

@transpose-aaron
Copy link

This is a great solution. Thank you very much. I have one question...

I am trying to send this custom email from a different address to that of the one set in the Woocommerce settings. I have tried to override the email by modifying the headers to something like the following...

$headers = array('Content-Type: text/html\r\n', 'From: John Doe info@johndoe.com');

However I can't seem to override the default email in the settings. Any idea on how to do this?

@ANJUJOJER
Copy link

Hai,
I am trying to do send a custom email from anywhere within wordpress using woocommerce template.But I can't send the mail with the woocommerce template (by calling the function).Without template content the mail is sending properly.Can you please identify the error why mail is not sending with woocommerce template.

@arctic-blue
Copy link

this is a really good start for me. if this helps for anyone: you have to copy the template file of your choice from the woocommerce plugin folder template/emails/choose-one-template.php into your prepared theme folder where the override files are located. the string for the template needs to fit in for the template file, so change it accordingly. I just copied a template file into that folder and renamed it to something proper for my purpose.

the next thing i was doing is to compact the function so, that i can call it with one line of code. and as of my purpose i am using an add_action hook to completely disable email sending (expecially for all automated emails). so to make this function work (again) and send mails again i need to call remove_action() with my function call/params, so the disabling of email sending was re-enabled again.

p.s.: it is easy to override the to email address with a add_action and hook into phpmailer_init

@ravismakwana
Copy link

Thank you for saving my time. :)

@gruntek
Copy link

gruntek commented Apr 4, 2020

Can I use it to send additional email when there is new woocommerce order?
Where should I place that script to achieve that?

@misstest
Copy link

I added on my page, on functions.php and in snippets, but i don't see a button or anything to interact with. How does it work?

@arctic-blue
Copy link

thanks mnajlat! I'll try this out next time :)

@arixwap
Copy link

arixwap commented Feb 8, 2021

Hi @craigedmonds
I found your gist is very helpful. Thank you so much

@shaddow11
Copy link

Thank you!

@ragno7
Copy link

ragno7 commented Jun 22, 2021

This is very useful, but for some reason the following "standard" emails sent from woocommerce keep repeating this custom one, how would it be possible to - escape - this email?

Thanks in advance

@CupCodeIr
Copy link

You're Awesome!

@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