Title: Programmatically Sending Email Using Woocommerce Template
Last modified: August 9, 2018

---

# Programmatically Sending Email Using Woocommerce Template

 *  Resolved [Chad](https://wordpress.org/support/users/aeboi80/)
 * (@aeboi80)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/programmatically-sending-email-using-woocommerce-template/)
 * I am working on creating a custom Order Action with a custom field called Estimated
   Ship Date. When viewing an order in the admin, it’s checking to see if a staff
   member has input an Estimated Ship Date for the order, if so it enables the custom
   Order Action.
 * When the custom Order Action is selected from the dropdown, I need it to send
   an email to billing email address with the Estimated Ship Date in the email.
 * I created a custom email template which has the code in it, but the issue I’m
   running into is actually triggering the email to send programmatically.
 * This is the full code I’m using in my plugin.
 *     ```
       /* ############################################
       	Estimated Ship Date
       ############################################ */
   
       	/**
       	 * Add a custom action to order actions select box on edit order page
       	 *
       	 * @param array $actions order actions array to display
       	 * @return array - updated actions
       	 */
       	function bhi_wc_add_order_meta_box_action( $actions ) {
       	    global $theorder;
   
       	    // bail if the order does not have an estimated ship date
       	    if ( ! get_field('estimated_ship_date') ) {
       	        return $actions;
       	    }
   
       	    // add "estimated ship date" custom action
       	    $actions['wc_custom_order_action'] = __( 'Send Estimated Ship Date Email', 'bhi-textdomain' );
       	    return $actions;
       	}
       	add_action( 'woocommerce_order_actions', 'bhi_wc_add_order_meta_box_action' );
   
       	/**
       	 * Send Email to Customer with Estimated Ship Date
       	 *
       	 * @param \WC_Order $order
       	 */
       	function bhi_wc_process_order_meta_box_action( $order ) {
   
   
       	    function get_custom_email_html( $order, $heading = false, $mailer ) {
   
       	        $template = 'emails/estimated-ship-date.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 = $order->get_billing_email();
       	    $subject = __("Estimated Ship Date Assigned", '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 );
   
       	    // Add Order Note indicated email has been sent
       	        // translators: Placeholders: %s is a user's display name
       	        $message = sprintf( __( 'Estimated shipment date email has been sent by %s', 'bhi-textdomain' ), wp_get_current_user()->display_name );
       	        $order->add_order_note( $message );
       	}
       	add_action( 'woocommerce_order_action_wc_custom_order_action', 'bhi_wc_process_order_meta_box_action' );
       ```
   
 * This is the code I’m using in my custom email template:
 *     ```
       <?php
       /**
        * Estimated Ship Date email
        *
        * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-note.php.
        *
        *
        * @see 	    https://docs.woocommerce.com/document/template-structure/
        * @author 		Bluehive Interactive
        * @package 	WooCommerce/Templates/Emails
        * @version     2.5.0
        */
   
       if ( ! defined( 'ABSPATH' ) ) {
       	exit;
       }
   
       /**
        * @hooked WC_Emails::email_header() Output the email header
        */
       do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
   
       <p><?php _e( "Hello, An estimated shipment date has been added to your order:", 'woocommerce' ); ?></p>
       <p><?php $bhi_estimated_ship_date 	= get_field('estimated_ship_date', $order_id);?> <strong>Estimated Ship Date:</strong> <?php echo $bhi_estimated_ship_date;?></p>
   
       <p><?php _e( "For your reference, your order details are shown below.", 'woocommerce' ); ?></p>
   
       <?php
   
       /**
        * @hooked WC_Emails::order_details() Shows the order details table.
        * @hooked WC_Structured_Data::generate_order_data() Generates structured data.
        * @hooked WC_Structured_Data::output_structured_data() Outputs structured data.
        * @since 2.5.0
        */
       do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );
   
       /**
        * @hooked WC_Emails::order_meta() Shows order meta data.
        */
       do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );
   
       /**
        * @hooked WC_Emails::customer_details() Shows customer details
        * @hooked WC_Emails::email_address() Shows email address
        */
       do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );
   
       /**
        * @hooked WC_Emails::email_footer() Output the email footer
        */
       do_action( 'woocommerce_email_footer', $email );
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Support [John Coy a11n](https://wordpress.org/support/users/johndcoy/)
 * (@johndcoy)
 * Automattic Happiness Engineer
 * [7 years, 9 months ago](https://wordpress.org/support/topic/programmatically-sending-email-using-woocommerce-template/#post-10615208)
 * Hi [@aeboi80](https://wordpress.org/support/users/aeboi80/)
 * You can use the id `customer_completed_order` to trigger the email after a completed
   order. This article may help with that process:
 * [https://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/](https://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/)
 *  [Luminus Alabi](https://wordpress.org/support/users/luminus/)
 * (@luminus)
 * Automattic Happiness Engineer
 * [7 years, 9 months ago](https://wordpress.org/support/topic/programmatically-sending-email-using-woocommerce-template/#post-10637824)
 * [@aeboi80](https://wordpress.org/support/users/aeboi80/),
 * We haven’t heard back from you in a while, so I’m going to mark this as resolved–
   if you have any further questions, you can start a new thread.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Programmatically Sending Email Using Woocommerce Template’ is closed 
to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [custom email](https://wordpress.org/support/topic-tag/custom-email/)
 * [email](https://wordpress.org/support/topic-tag/email/)
 * [order action](https://wordpress.org/support/topic-tag/order-action/)

 * 2 replies
 * 3 participants
 * Last reply from: [Luminus Alabi](https://wordpress.org/support/users/luminus/)
 * Last activity: [7 years, 9 months ago](https://wordpress.org/support/topic/programmatically-sending-email-using-woocommerce-template/#post-10637824)
 * Status: resolved