Make WordPress Core

Ticket #15448: 15448-multi.diff

File 15448-multi.diff, 4.3 KB (added by rmccue, 14 years ago)

Add support for multiple attachments of the same type

  • pluggable.php

     
    247247 * However, you can set the content type of the email by using the
    248248 * 'wp_mail_content_type' filter.
    249249 *
     250 * If $message is an array, the key of each is used to add as an attachment
     251 * with the value used as the body. The 'text/plain' element is used as the
     252 * text version of the body, with the 'text/html' element used as the HTML
     253 * version of the body. All other types are added as attachments.
     254 *
    250255 * The default charset is based on the charset used on the blog. The charset can
    251256 * be set using the 'wp_mail_charset' filter.
    252257 *
     
    263268 *
    264269 * @param string|array $to Array or comma-separated list of email addresses to send message.
    265270 * @param string $subject Email subject
    266  * @param string $message Message contents
     271 * @param string|array $message Message contents
    267272 * @param string|array $headers Optional. Additional headers.
    268273 * @param string|array $attachments Optional. Files to attach.
    269274 * @return bool Whether the email contents were sent successfully.
     
    332337                                                }
    333338                                                break;
    334339                                        case 'content-type':
     340                                                if ( is_array($message) ) {
     341                                                        // Multipart email, ignore the content-type header
     342                                                        break;
     343                                                }
    335344                                                if ( strpos( $content, ';' ) !== false ) {
    336345                                                        list( $type, $charset ) = explode( ';', $content );
    337346                                                        $content_type = trim( $type );
     
    403412                $phpmailer->AddAddress( trim( $recipient ) );
    404413        }
    405414
     415        // If we don't have a charset from the input headers
     416        if ( !isset( $charset ) )
     417                $charset = get_bloginfo( 'charset' );
     418
     419        // Set the content-type and charset
     420        $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
     421
    406422        // Set mail's subject and body
    407423        $phpmailer->Subject = $subject;
    408         $phpmailer->Body    = $message;
    409424
     425        if ( is_string($message) ) {
     426                $phpmailer->Body    = $message;
     427
     428                // Set Content-Type and charset
     429                // If we don't have a content-type from the input headers
     430                if ( !isset( $content_type ) )
     431                        $content_type = 'text/plain';
     432
     433                $content_type = apply_filters( 'wp_mail_content_type', $content_type );
     434
     435                $phpmailer->ContentType = $content_type;
     436
     437                // Set whether it's plaintext, depending on $content_type
     438                if ( 'text/html' == $content_type )
     439                        $phpmailer->IsHTML( true );
     440
     441                // For backwards compatibility, new multipart emails should use
     442                // the array style $message. This never really worked well anyway
     443                if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
     444                        $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
     445        }
     446        elseif ( is_array($message) ) {
     447                foreach ($message as $type => $bodies) {
     448                        foreach ((array) $bodies as $body) {
     449                                if ($type === 'text/html') {
     450                                        $phpmailer->Body = $body;
     451                                }
     452                                elseif ($type === 'text/plain') {
     453                                        $phpmailer->AltBody = $body;
     454                                }
     455                                else {
     456                                        $phpmailer->AddAttachment($body, '', 'base64', $type);
     457                                }
     458                        }
     459                }
     460        }
     461
    410462        // Add any CC and BCC recipients
    411463        if ( !empty( $cc ) ) {
    412464                foreach ( (array) $cc as $recipient ) {
     
    423475        // Set to use PHP's mail()
    424476        $phpmailer->IsMail();
    425477
    426         // Set Content-Type and charset
    427         // If we don't have a content-type from the input headers
    428         if ( !isset( $content_type ) )
    429                 $content_type = 'text/plain';
    430 
    431         $content_type = apply_filters( 'wp_mail_content_type', $content_type );
    432 
    433         $phpmailer->ContentType = $content_type;
    434 
    435         // Set whether it's plaintext, depending on $content_type
    436         if ( 'text/html' == $content_type )
    437                 $phpmailer->IsHTML( true );
    438 
    439         // If we don't have a charset from the input headers
    440         if ( !isset( $charset ) )
    441                 $charset = get_bloginfo( 'charset' );
    442 
    443         // Set the content-type and charset
    444         $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
    445 
    446478        // Set custom headers
    447479        if ( !empty( $headers ) ) {
    448480                foreach( (array) $headers as $name => $content ) {
    449481                        $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
    450482                }
    451 
    452                 if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
    453                         $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
    454483        }
    455484
    456485        if ( !empty( $attachments ) ) {