Make WordPress Core

Changeset 61010


Ignore:
Timestamp:
10/21/2025 08:04:22 AM (3 weeks ago)
Author:
dmsnell
Message:

wp_mail(): Set sender address by default.

WordPress has been calling $phpmailer->setFrom() with a false value for an attribute telling it to set the sender address for each message. This sender address is also known by other names: Envelope-From, MAIL FROM, Return-Path, etc... Unfortunately, this configuration can easily lead to mail being rejected by numerous mail hosts due to an invalid domain being generated by the local mail server/MTA.

The flag was originally added with the note that its absence “causes outgoing email to fail on some server environments.” However, it is likely that this led to the opposite effect, as evidenced by numerous reports, plugins, and workarounds over the years.

In this patch the flag is being removed, which has the effect of letting $phpmailer set the Sender value, which it does by default using the domain “where the front end is accessible” and which is is likely correct.

After this change there is a chance of mail failure for sites with SPF configured but which does not allow mail to be sent on behalf of this domain and if those sites also do not have a properly configured DKIM and DMARC setup. Those sites should review their SPF policies or the wp_mail_from filter.

Developed in https://github.com/WordPress/wordpress-develop/pull/9412
Discussed in https://core.trac.wordpress.org/ticket/49687

Follow-up to [38286].

Props cbutlerjr, dmsnell, jamieburchell, knutsp, kub1x, lordandy1984, piskvorky, SergeyBiryukov, siliconforks, SirLouen, stankea, vbbp, websupporter.

Fixes #49687.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/pluggable.php

    r60989 r61010  
    433433
    434434        try {
    435             $phpmailer->setFrom( $from_email, $from_name, false );
     435            $phpmailer->setFrom( $from_email, $from_name );
    436436        } catch ( PHPMailer\PHPMailer\Exception $e ) {
    437437            $mail_error_data                             = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
  • trunk/tests/phpunit/tests/pluggable/wpMail.php

    r60699 r61010  
    409409
    410410    /**
    411      * Test that the Sender field in the SMTP envelope is not set by Core.
    412      *
    413      * Correctly setting the Sender requires knowledge that is not available
    414      * to Core. An incorrect value will often lead to messages being rejected
    415      * by the receiving MTA, so it's the admin's responsibility to
    416      * set it correctly.
    417      *
    418      * @ticket 37736
    419      */
    420     public function test_wp_mail_sender_not_set() {
    421         wp_mail( 'user@example.org', 'Testing the Sender field', 'The Sender field should not have been set.' );
    422 
    423         $mailer = tests_retrieve_phpmailer_instance();
    424 
    425         $this->assertSame( '', $mailer->Sender );
     411     * Test that the Sender field in the SMTP envelope is set by Core.
     412     *
     413     * A missing Sender field can lead to messages failing DMARC SPF checks.
     414     *
     415     * @ticket 49687
     416     */
     417    public function test_wp_mail_sender_set() {
     418        wp_mail( 'user@example.org', 'Testing the Sender field', 'The Sender field should have been set.' );
     419
     420        $mailer = tests_retrieve_phpmailer_instance();
     421
     422        $this->assertSame( 'wordpress@example.org', $mailer->Sender );
    426423    }
    427424
Note: See TracChangeset for help on using the changeset viewer.