Woocommerce send mail

In WooCommerce, you can send emails to customers and administrators using the woocommerce_send_email function. This function allows you to send emails for various events, such as:

  1. Order confirmation
  2. Order shipping
  3. Order completed
  4. Order cancelled
  5. Order refunded
  6. New account creation
  7. Password reset
  8. Product review submission

Here's an example of how to use the woocommerce_send_email function:

function send_email_example() {
    // Get the order ID
    $order_id = 123;

    // Get the order object
    $order = wc_get_order( $order_id );

    // Set the email subject and message
    $subject = 'Order Confirmation';
    $message = 'Thank you for your order!';

    // Send the email
    wc_send_email( 'customer_processing_order', $order_id, array(
        'order' => $order,
        'subject' => $subject,
        'message' => $message,
    ) );
}

In this example, we're sending an email to the customer when an order is processed. The woocommerce_send_email function takes three arguments:

  1. email_type: The type of email to send (e.g. customer_processing_order, customer_order_shipped, etc.)
  2. order_id: The ID of the order to send the email for
  3. args: An array of arguments to pass to the email template

You can also use the woocommerce_get_email_template function to get the email template for a specific email type:

function get_email_template_example() {
    // Get the email template for the "customer_processing_order" email type
    $template = wc_get_email_template( 'customer_processing_order', 'woocommerce' );

    // Print the template
    echo $template;
}

This function takes two arguments:

  1. email_type: The type of email to get the template for
  2. template_type: The type of template to get (e.g. html, text, etc.)

You can also use the woocommerce_get_email_recipient function to get the recipient's email address:

function get_email_recipient_example() {
    // Get the order ID
    $order_id = 123;

    // Get the order object
    $order = wc_get_order( $order_id );

    // Get the recipient's email address
    $recipient_email = wc_get_email_recipient( $order_id, 'customer_email' );

    // Print the recipient's email address
    echo $recipient_email;
}

This function takes two arguments:

  1. order_id: The ID of the order to get the recipient's email address for
  2. recipient_type: The type of recipient to get the email address for (e.g. customer_email, admin_email, etc.)

Note that you can customize the email templates and subject lines by creating your own email templates in the woocommerce directory of your theme. You can also use the woocommerce_email_template filter to modify the email templates.