Commit fd008c483c for woocommerce

commit fd008c483c730b19760420f96afcc0bcfe159e74
Author: Seun Olorunsola <30554163+triple0t@users.noreply.github.com>
Date:   Wed Jan 28 14:03:11 2026 +0100

    Fix email editor Send a test email feature to use configured WooCommerce "From" email address and name (#62936)

    * Enhance email preview functionality by adding action hooks before and after sending emails. This allows for modification of email headers and post-send actions. Update README to document new hooks.

    * Add changelog files

    * Fix lint errors and implement PR feedback

    * The WC_Email instance does not exist on some test env. Switching to fetching the email instance from \WC_Emails

    * Coderabbitai PR feedback

diff --git a/packages/php/email-editor/README.md b/packages/php/email-editor/README.md
index ef9d7f16f5..c9640a6bce 100644
--- a/packages/php/email-editor/README.md
+++ b/packages/php/email-editor/README.md
@@ -80,11 +80,13 @@ We may add, update and delete any of them.

 ### Actions

-| Name                                            | Argument         | Description                                                                                                      |
-|-------------------------------------------------|------------------|------------------------------------------------------------------------------------------------------------------|
-| `woocommerce_email_editor_initialized`          | `null`           | Called when the Email Editor is initialized                                                                      |
-| `woocommerce_email_blocks_renderer_initialized` | `BlocksRegistry` | Called when the block content renderer is initialized. You may use this to add a new BlockRenderer               |
-| `woocommerce_email_editor_register_templates`   |                  | Called when the basic blank email template is registered. You can add more templates via register_block_template |
+| Name                                                         | Argument                                                        | Description                                                                                                                                                        |
+|--------------------------------------------------------------|-----------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| `woocommerce_email_editor_initialized`                       | `null`                                                          | Called when the Email Editor is initialized                                                                                                                        |
+| `woocommerce_email_blocks_renderer_initialized`              | `BlocksRegistry`                                                | Called when the block content renderer is initialized. You may use this to add a new BlockRenderer                                                                 |
+| `woocommerce_email_editor_register_templates`                |                                                                 | Called when the basic blank email template is registered. You can add more templates via register_block_template                                                   |
+| `woocommerce_email_editor_send_preview_email_before_wp_mail` | `string` $to, `string` $subject, `string` $body                 | Called before sending the preview email via wp_mail. Use this to modify email headers (e.g., from address, from name) or perform pre-send actions.                 |
+| `woocommerce_email_editor_send_preview_email_after_wp_mail`  | `string` $to, `string` $subject, `string` $body, `bool` $result | Called after sending the preview email via wp_mail. Use this to clean up filters, log results, or perform post-send actions. The `$result` indicates success.      |

 ### Filters

diff --git a/packages/php/email-editor/changelog/stomail-7684-add-preview-email-action-hooks b/packages/php/email-editor/changelog/stomail-7684-add-preview-email-action-hooks
new file mode 100644
index 0000000000..23a3c5fc7d
--- /dev/null
+++ b/packages/php/email-editor/changelog/stomail-7684-add-preview-email-action-hooks
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Add action hooks for customizing preview email sending: woocommerce_email_editor_send_preview_email_before_wp_mail and woocommerce_email_editor_send_preview_email_after_wp_mail.
diff --git a/packages/php/email-editor/src/Engine/class-send-preview-email.php b/packages/php/email-editor/src/Engine/class-send-preview-email.php
index 215607d6e1..60ca601dc9 100644
--- a/packages/php/email-editor/src/Engine/class-send-preview-email.php
+++ b/packages/php/email-editor/src/Engine/class-send-preview-email.php
@@ -147,6 +147,8 @@ class Send_Preview_Email {
 	 * @return bool Returns true if the email was sent successfully, false otherwise.
 	 */
 	public function send_email( string $to, string $subject, string $body ): bool {
+		do_action( 'woocommerce_email_editor_send_preview_email_before_wp_mail', $to, $subject, $body );
+
 		add_filter( 'wp_mail_content_type', array( $this, 'set_mail_content_type' ) );

 		$result = wp_mail( $to, $subject, $body );
@@ -154,6 +156,8 @@ class Send_Preview_Email {
 		// Reset content-type to avoid conflicts.
 		remove_filter( 'wp_mail_content_type', array( $this, 'set_mail_content_type' ) );

+		do_action( 'woocommerce_email_editor_send_preview_email_after_wp_mail', $to, $subject, $body, $result );
+
 		return $result;
 	}

diff --git a/plugins/woocommerce/changelog/stomail-7684-email-previews-should-use-the-configured-from-email b/plugins/woocommerce/changelog/stomail-7684-email-previews-should-use-the-configured-from-email
new file mode 100644
index 0000000000..6fb6e535c2
--- /dev/null
+++ b/plugins/woocommerce/changelog/stomail-7684-email-previews-should-use-the-configured-from-email
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix email preview to use the configured WooCommerce "From" email address and name instead of WordPress defaults.
diff --git a/plugins/woocommerce/src/Internal/EmailEditor/Integration.php b/plugins/woocommerce/src/Internal/EmailEditor/Integration.php
index db7ee12251..a13b39e18e 100644
--- a/plugins/woocommerce/src/Internal/EmailEditor/Integration.php
+++ b/plugins/woocommerce/src/Internal/EmailEditor/Integration.php
@@ -51,6 +51,13 @@ class Integration {
 	 */
 	private EmailApiController $email_api_controller;

+	/**
+	 * The WC_Email instance.
+	 *
+	 * @var \WC_Email
+	 */
+	private \WC_Email $wc_email_instance;
+
 	/**
 	 * Constructor.
 	 */
@@ -108,6 +115,15 @@ class Integration {
 		$this->editor_page_renderer    = $container->get( PageRenderer::class );
 		$this->template_api_controller = $container->get( TemplateApiController::class );
 		$this->email_api_controller    = $container->get( EmailApiController::class );
+
+		// Using any email class to get the instance.
+		$registered_emails = \WC_Emails::instance()->get_emails();
+		if ( isset( $registered_emails['WC_Email_New_Order'] ) ) {
+			$this->wc_email_instance = $registered_emails['WC_Email_New_Order'];
+		} else {
+			$first_email_key         = array_key_first( $registered_emails );
+			$this->wc_email_instance = $registered_emails[ $first_email_key ];
+		}
 	}

 	/**
@@ -121,6 +137,8 @@ class Integration {
 		add_filter( 'woocommerce_email_editor_send_preview_email_rendered_data', array( $this, 'update_send_preview_email_rendered_data' ), 10, 2 );
 		add_filter( 'woocommerce_email_editor_send_preview_email_personalizer_context', array( $this, 'update_send_preview_email_personalizer_context' ) );
 		add_filter( 'woocommerce_email_editor_preview_post_template_html', array( $this, 'update_preview_post_template_html_data' ), 100, 1 );
+		add_action( 'woocommerce_email_editor_send_preview_email_before_wp_mail', array( $this, 'send_preview_email_before_wp_mail' ), 10 );
+		add_action( 'woocommerce_email_editor_send_preview_email_after_wp_mail', array( $this, 'send_preview_email_after_wp_mail' ), 10 );
 	}

 	/**
@@ -375,4 +393,26 @@ class Integration {
 			)
 		);
 	}
+
+	/**
+	 * Action hook callback before sending the preview email via wp_mail
+	 *
+	 * @since 10.6.0
+	 * @return void
+	 */
+	public function send_preview_email_before_wp_mail() {
+		add_filter( 'wp_mail_from', array( $this->wc_email_instance, 'get_from_address' ) );
+		add_filter( 'wp_mail_from_name', array( $this->wc_email_instance, 'get_from_name' ) );
+	}
+
+	/**
+	 * Action hook callback after sending the preview email via wp_mail.
+	 *
+	 * @since 10.6.0
+	 * @return void
+	 */
+	public function send_preview_email_after_wp_mail() {
+		remove_filter( 'wp_mail_from', array( $this->wc_email_instance, 'get_from_address' ) );
+		remove_filter( 'wp_mail_from_name', array( $this->wc_email_instance, 'get_from_name' ) );
+	}
 }