From 5db2bb329109862ea7a5f6fba4e86c0b5b5e5534 Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Thu, 12 Sep 2024 15:00:13 +0200 Subject: [PATCH] Contact Form: remove www prefix from sender email address (#39370) On sites that use the www. prefix, there is no need to include that prefix in the default sender email address we generate before sending the emails. Related discussion: https://wordpress.org/support/topic/wrong-domain-in-emails-sent-by-jetpack-2/ --- .../changelog/fix-contact-form-from-email-no-www | 4 ++++ .../forms/src/contact-form/class-contact-form.php | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 projects/packages/forms/changelog/fix-contact-form-from-email-no-www diff --git a/projects/packages/forms/changelog/fix-contact-form-from-email-no-www b/projects/packages/forms/changelog/fix-contact-form-from-email-no-www new file mode 100644 index 0000000000000..e45ceb825ee8b --- /dev/null +++ b/projects/packages/forms/changelog/fix-contact-form-from-email-no-www @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Email submissions: on sites using www., ensure that the sending email address does not use the www. prefix. diff --git a/projects/packages/forms/src/contact-form/class-contact-form.php b/projects/packages/forms/src/contact-form/class-contact-form.php index 0ce3ef48e775d..9b561f2dbb576 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form.php +++ b/projects/packages/forms/src/contact-form/class-contact-form.php @@ -1247,8 +1247,17 @@ public function process_submission() { $to[ $to_key ] = self::add_name_to_address( $to_value ); } - $blog_url = wp_parse_url( site_url() ); - $from_email_addr = 'wordpress@' . $blog_url['host']; + // Get the site domain and get rid of www. + $sitename = wp_parse_url( site_url(), PHP_URL_HOST ); + $from_email_addr = 'wordpress@'; + + if ( null !== $sitename ) { + if ( str_starts_with( $sitename, 'www.' ) ) { + $sitename = substr( $sitename, 4 ); + } + + $from_email_addr .= $sitename; + } if ( ! empty( $comment_author_email ) ) { $reply_to_addr = $comment_author_email;