Make WordPress Core

Changeset 32070


Ignore:
Timestamp:
04/07/2015 08:09:46 PM (9 years ago)
Author:
boonebgorges
Message:

Improve handling of incomplete From and Content-Type headers in wp_mail().

When an incomplete header is provided (eg, 'From' with an email address but no
name), ensure that the WP defaults are filled in properly.

Props valendesigns.
Fixes #30266.

Location:
trunk
Files:
2 edited

Legend:

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

    r32045 r32070  
    303303                    // Mainly for legacy -- process a From: header if it's there
    304304                    case 'from':
    305                         if ( strpos($content, '<' ) !== false ) {
    306                             // So... making my life hard again?
    307                             $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
    308                             $from_name = str_replace( '"', '', $from_name );
    309                             $from_name = trim( $from_name );
    310 
    311                             $from_email = substr( $content, strpos( $content, '<' ) + 1 );
     305                        $bracket_pos = strpos( $content, '<' );
     306                        if ( $bracket_pos !== false ) {
     307                            // Text before the bracketed email is the "From" name.
     308                            if ( $bracket_pos > 0 ) {
     309                                $from_name = substr( $content, 0, $bracket_pos - 1 );
     310                                $from_name = str_replace( '"', '', $from_name );
     311                                $from_name = trim( $from_name );
     312                            }
     313
     314                            $from_email = substr( $content, $bracket_pos + 1 );
    312315                            $from_email = str_replace( '>', '', $from_email );
    313316                            $from_email = trim( $from_email );
    314                         } else {
     317
     318                        // Avoid setting an empty $from_email.
     319                        } elseif ( '' !== trim( $content ) ) {
    315320                            $from_email = trim( $content );
    316321                        }
     
    318323                    case 'content-type':
    319324                        if ( strpos( $content, ';' ) !== false ) {
    320                             list( $type, $charset ) = explode( ';', $content );
     325                            list( $type, $charset_content ) = explode( ';', $content );
    321326                            $content_type = trim( $type );
    322                             if ( false !== stripos( $charset, 'charset=' ) ) {
    323                                 $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
    324                             } elseif ( false !== stripos( $charset, 'boundary=' ) ) {
    325                                 $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
     327                            if ( false !== stripos( $charset_content, 'charset=' ) ) {
     328                                $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) );
     329                            } elseif ( false !== stripos( $charset_content, 'boundary=' ) ) {
     330                                $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) );
    326331                                $charset = '';
    327332                            }
    328                         } else {
     333
     334                        // Avoid setting an empty $content_type.
     335                        } elseif ( '' !== trim( $content ) ) {
    329336                            $content_type = trim( $content );
    330337                        }
  • trunk/tests/phpunit/tests/mail.php

    r31622 r32070  
    157157        $this->assertFalse( wp_mail( 'invalid.address', 'subject', 'body', '', array() ) );
    158158    }
     159
     160    /**
     161     * @ticket 30266
     162     */
     163    public function test_wp_mail_with_valid_from_header() {
     164        $to       = "address@tld.com";
     165        $subject  = "Testing";
     166        $message  = "Test Message";
     167        $headers  = "From: Foo <bar@example.com>";
     168        $expected = "From: Foo <bar@example.com>";
     169
     170        wp_mail( $to, $subject, $message, $headers );
     171
     172        $this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
     173    }
     174
     175    /**
     176     * @ticket 30266
     177     */
     178    public function test_wp_mail_with_empty_from_header() {
     179        $to       = "address@tld.com";
     180        $subject  = "Testing";
     181        $message  = "Test Message";
     182        $headers  = "From: ";
     183        $expected = "From: WordPress <wordpress@example.com>";
     184
     185        wp_mail( $to, $subject, $message, $headers );
     186
     187        $this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
     188    }
     189
     190    /**
     191     * @ticket 30266
     192     */
     193    public function test_wp_mail_with_empty_from_name_for_the_from_header() {
     194        $to       = "address@tld.com";
     195        $subject  = "Testing";
     196        $message  = "Test Message";
     197        $headers  = "From: <wordpress@example.com>";
     198        $expected = "From: WordPress <wordpress@example.com>";
     199
     200        wp_mail( $to, $subject, $message, $headers );
     201
     202        $this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
     203    }
     204
     205    /**
     206     * @ticket 30266
     207     */
     208    public function test_wp_mail_with_valid_content_type_header() {
     209        $to       = "address@tld.com";
     210        $subject  = "Testing";
     211        $message  = "Test Message";
     212        $headers  = "Content-Type: text/html; charset=iso-8859-1";
     213        $expected = "Content-Type: text/html; charset=iso-8859-1";
     214
     215        wp_mail( $to, $subject, $message, $headers );
     216
     217        $this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
     218    }
     219
     220    /**
     221     * @ticket 30266
     222     */
     223    public function test_wp_mail_with_empty_content_type_header() {
     224        $to       = "address@tld.com";
     225        $subject  = "Testing";
     226        $message  = "Test Message";
     227        $headers  = "Content-Type: ";
     228        $expected = "Content-Type: text/plain; charset=UTF-8";
     229
     230        wp_mail( $to, $subject, $message, $headers );
     231
     232        $this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
     233    }
     234
     235    /**
     236     * @ticket 30266
     237     */
     238    public function test_wp_mail_with_empty_charset_for_the_content_type_header() {
     239        $to       = "address@tld.com";
     240        $subject  = "Testing";
     241        $message  = "Test Message";
     242        $headers  = "Content-Type: text/plain;";
     243        $expected = "Content-Type: text/plain; charset=UTF-8";
     244
     245        wp_mail( $to, $subject, $message, $headers );
     246
     247        $this->assertTrue( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], $expected ) > 0 );
     248    }
    159249}
Note: See TracChangeset for help on using the changeset viewer.