Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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

This comment has been minimized.

Copy link

@EdgarValfogo EdgarValfogo commented Sep 20, 2017

Thank you for saving me!

@hmorv

This comment has been minimized.

Copy link

@hmorv hmorv commented Jan 3, 2018

great! thanks so much!

@beneverard

This comment has been minimized.

Copy link

@beneverard beneverard commented Sep 14, 2018

Very useful indeed, thank you for sharing this.

@luancuba

This comment has been minimized.

Copy link

@luancuba luancuba commented Dec 6, 2018

thank you!

@transpose-aaron

This comment has been minimized.

Copy link

@transpose-aaron transpose-aaron commented May 23, 2019

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

This comment has been minimized.

Copy link

@ANJUJOJER ANJUJOJER commented May 28, 2019

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

This comment has been minimized.

Copy link

@arctic-blue arctic-blue commented May 29, 2019

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

This comment has been minimized.

Copy link

@ravismakwana ravismakwana commented Nov 16, 2019

Thank you for saving my time. :)

@gruntek

This comment has been minimized.

Copy link

@gruntek 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

This comment has been minimized.

Copy link

@misstest misstest commented Aug 26, 2020

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

This comment has been minimized.

Copy link

@arctic-blue arctic-blue commented Nov 18, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.