Make WordPress Core

Changeset 13456


Ignore:
Timestamp:
02/27/2010 04:10:45 PM (13 years ago)
Author:
nacin
Message:

Allow multiple To: recipients in wp_mail(). Improve handling of \r\n in headers and multiple CC/BCC headers. fixes #10420

File:
1 edited

Legend:

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

    r13432 r13456  
    245245 * @
    246246 *
    247  * @param string $to Email address to send message
     247 * @param string|array $to Array or comma-separated list of email addresses to send message.
    248248 * @param string $subject Email subject
    249249 * @param string $message Message contents
     
    257257
    258258    if ( !is_array($attachments) )
    259         $attachments = explode( "\n", $attachments );
     259        $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
    260260
    261261    global $phpmailer;
     
    275275            // Explode the headers out, so this function can take both
    276276            // string headers and an array of headers.
    277             $tempheaders = (array) explode( "\n", $headers );
     277            $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
    278278        } else {
    279279            $tempheaders = $headers;
     
    296296
    297297                // Cleanup crew
    298                 $name = trim( $name );
     298                $name    = trim( $name    );
    299299                $content = trim( $content );
    300300
    301                 // Mainly for legacy -- process a From: header if it's there
    302                 if ( 'from' == strtolower($name) ) {
    303                     if ( strpos($content, '<' ) !== false ) {
    304                         // So... making my life hard again?
    305                         $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
    306                         $from_name = str_replace( '"', '', $from_name );
    307                         $from_name = trim( $from_name );
    308 
    309                         $from_email = substr( $content, strpos( $content, '<' ) + 1 );
    310                         $from_email = str_replace( '>', '', $from_email );
    311                         $from_email = trim( $from_email );
    312                     } else {
    313                         $from_email = trim( $content );
    314                     }
    315                 } elseif ( 'content-type' == strtolower($name) ) {
    316                     if ( strpos( $content,';' ) !== false ) {
    317                         list( $type, $charset ) = explode( ';', $content );
    318                         $content_type = trim( $type );
    319                         if ( false !== stripos( $charset, 'charset=' ) ) {
    320                             $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
    321                         } elseif ( false !== stripos( $charset, 'boundary=' ) ) {
    322                             $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
    323                             $charset = '';
     301                switch ( strtolower( $name ) ) {
     302                    // Mainly for legacy -- process a From: header if it's there
     303                    case 'from':
     304                        if ( strpos($content, '<' ) !== false ) {
     305                            // So... making my life hard again?
     306                            $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
     307                            $from_name = str_replace( '"', '', $from_name );
     308                            $from_name = trim( $from_name );
     309
     310                            $from_email = substr( $content, strpos( $content, '<' ) + 1 );
     311                            $from_email = str_replace( '>', '', $from_email );
     312                            $from_email = trim( $from_email );
     313                        } else {
     314                            $from_email = trim( $content );
    324315                        }
    325                     } else {
    326                         $content_type = trim( $content );
    327                     }
    328                 } elseif ( 'cc' == strtolower($name) ) {
    329                     $cc = explode(",", $content);
    330                 } elseif ( 'bcc' == strtolower($name) ) {
    331                     $bcc = explode(",", $content);
    332                 } else {
    333                     // Add it to our grand headers array
    334                     $headers[trim( $name )] = trim( $content );
     316                        break;
     317                    case 'content-type':
     318                        if ( strpos( $content, ';' ) !== false ) {
     319                            list( $type, $charset ) = explode( ';', $content );
     320                            $content_type = trim( $type );
     321                            if ( false !== stripos( $charset, 'charset=' ) ) {
     322                                $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
     323                            } elseif ( false !== stripos( $charset, 'boundary=' ) ) {
     324                                $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
     325                                $charset = '';
     326                            }
     327                        } else {
     328                            $content_type = trim( $content );
     329                        }
     330                        break;
     331                    case 'cc':
     332                        $cc = array_merge( (array) $cc, explode( ',', $content ) );
     333                        break;
     334                    case 'bcc':
     335                        $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
     336                        break;
     337                    default:
     338                        // Add it to our grand headers array
     339                        $headers[trim( $name )] = trim( $content );
     340                        break;
    335341                }
    336342            }
     
    349355    // From email and name
    350356    // If we don't have a name from the input headers
    351     if ( !isset( $from_name ) ) {
     357    if ( !isset( $from_name ) )
    352358        $from_name = 'WordPress';
    353     }
    354359
    355360    /* If we don't have an email from the input headers default to wordpress@$sitename
     
    371376
    372377    // Plugin authors can override the potentially troublesome default
    373     $phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
    374     $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
    375 
    376     // Set destination address
    377     $phpmailer->AddAddress( $to );
     378    $phpmailer->From     = apply_filters( 'wp_mail_from'     , $from_email );
     379    $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name  );
     380
     381    // Set destination addresses
     382    if ( !is_array( $to ) )
     383        $to = explode( ',', $to );
     384
     385    foreach ( (array) $to as $recipient ) {
     386        $phpmailer->AddAddress( trim( $recipient ) );
     387    }
    378388
    379389    // Set mail's subject and body
    380390    $phpmailer->Subject = $subject;
    381     $phpmailer->Body = $message;
     391    $phpmailer->Body    = $message;
    382392
    383393    // Add any CC and BCC recipients
    384     if ( !empty($cc) ) {
     394    if ( !empty( $cc ) ) {
    385395        foreach ( (array) $cc as $recipient ) {
    386396            $phpmailer->AddCc( trim($recipient) );
    387397        }
    388398    }
    389     if ( !empty($bcc) ) {
     399
     400    if ( !empty( $bcc ) ) {
    390401        foreach ( (array) $bcc as $recipient) {
    391402            $phpmailer->AddBcc( trim($recipient) );
     
    398409    // Set Content-Type and charset
    399410    // If we don't have a content-type from the input headers
    400     if ( !isset( $content_type ) ) {
     411    if ( !isset( $content_type ) )
    401412        $content_type = 'text/plain';
    402     }
    403413
    404414    $content_type = apply_filters( 'wp_mail_content_type', $content_type );
     
    407417
    408418    // Set whether it's plaintext, depending on $content_type
    409     if ( $content_type == 'text/html' ) {
     419    if ( 'text/html' == $content_type )
    410420        $phpmailer->IsHTML( true );
    411     }
    412421
    413422    // If we don't have a charset from the input headers
    414     if ( !isset( $charset ) ) {
     423    if ( !isset( $charset ) )
    415424        $charset = get_bloginfo( 'charset' );
    416     }
    417425
    418426    // Set the content-type and charset
     
    424432            $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
    425433        }
    426         if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) ) {
     434
     435        if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
    427436            $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
    428         }
    429437    }
    430438
Note: See TracChangeset for help on using the changeset viewer.