[wp-trac] [WordPress Trac] #51030: Error in wp_mail function

WordPress Trac noreply at wordpress.org
Thu Apr 25 10:43:23 UTC 2024


#51030: Error in wp_mail function
-------------------------------------------------+-------------------------
 Reporter:  hichembelhadj                        |       Owner:  (none)
     Type:  defect (bug)                         |      Status:  closed
 Priority:  normal                               |   Milestone:
Component:  Mail                                 |     Version:
 Severity:  normal                               |  Resolution:  wontfix
 Keywords:  has-patch has-unit-tests needs-      |     Focuses:
  testing dev-feedback 2nd-opinion               |
-------------------------------------------------+-------------------------

Comment (by bmenant):

 I’d like to reopen this ticket since I have a different opinion and
 understanding of the original ticket. I keep coming back to this issue
 each time I set up a new development environment and notice `wp_mail`
 doesn’t fire anything because of this issue I forgot about... when I feel
 like it should just work out of the box.

 I agree `wp_mail` should prevent sending emails **TO** unrealistic email
 addresses (as per `is_email`).

 I’m not quite sure to agree `wp_mail` should not allow sending **FROM**
 `wordpress at localhost` email address.

 Notably because `wp_mail` may infer its sender address from the hostname.
 On a local development instance (e.g. container), `localhost` is a common
 hostname.

 And on such instance, it is fairly common to test emails using a mail
 catcher system (e.g. [https://mailpit.axllent.org/ Mailpit]) acting as a
 SMTP server (with a docker-compose configuration or with a podman pod for
 example).

 A fix could be made over here, with a check for the lack of tld, which is
 the root cause of the issue:
 https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-
 includes/pluggable.php#L368-L387

 {{{#!php
                 /*
                  * If we don't have an email from the input headers,
 default to wordpress@$sitename
                  * Some hosts will block outgoing mail from this address
 if it doesn't exist,
                  * but there's no easy alternative. Defaulting to
 admin_email might appear to be
                  * another option, but some hosts may refuse to relay mail
 from an unknown domain.
                  * See https://core.trac.wordpress.org/ticket/5007.
                  */
                 if ( ! isset( $from_email ) ) {
                         // Get the site domain and get rid of www.
                         $sitename   = wp_parse_url( network_home_url(),
 PHP_URL_HOST );
                         $from_email = 'wordpress@';

                         if ( null !== $sitename ) {
                                 if ( str_starts_with( $sitename, 'www.' )
 ) {
                                         $sitename = substr( $sitename, 4
 );
                                 }
 +                               if ( ! str_contains( $sitename, '.' ) ) {
 +                                       $sitename .= '.localdomain';
 +                               }
                                 $from_email .= $sitename;
                         }
                 }
 }}}

 Or alternatively with a more specific test `if ( 'localhost' === $sitename
 )` which is stricter but wouldn’t cover mutlisites development
 environments with custom `/etc/hosts` or alike setups.

 Another option would be to leave it as is, but to add a test in the site-
 health system so users can quickly figure out emailings won’t work on a
 tld-less hostname.

 What do you think?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51030#comment:15>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list