Make WordPress Core

Changeset 5639


Ignore:
Timestamp:
06/02/2007 03:18:24 AM (16 years ago)
Author:
rob1n
Message:

wp_mail() rewrite that handles HTML mail. fixes #4296

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2/wp-includes/pluggable.php

    r5440 r5639  
    157157endif;
    158158
    159 if ( !function_exists('wp_mail') ) :
    160 function wp_mail($to, $subject, $message, $headers = '') {
     159if ( !function_exists( 'wp_mail' ) ) :
     160function wp_mail( $to, $subject, $message, $headers = '' ) {
    161161    global $phpmailer;
    162 
    163     if ( !is_object( $phpmailer ) ) {
    164         require_once(ABSPATH . WPINC . '/class-phpmailer.php');
    165         require_once(ABSPATH . WPINC . '/class-smtp.php');
     162   
     163    // (Re)create it, if it's gone missing
     164    if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
     165        require_once ABSPATH . WPINC . '/class-phpmailer.php';
     166        require_once ABSPATH . WPINC . '/class-smtp.php';
    166167        $phpmailer = new PHPMailer();
    167168    }
    168 
    169     $mail = compact('to', 'subject', 'message', 'headers');
    170     $mail = apply_filters('wp_mail', $mail);
    171     extract($mail);
    172 
    173     if ( $headers == '' ) {
    174         $headers = "MIME-Version: 1.0\n" .
    175             "From: " . apply_filters('wp_mail_from', "wordpress@" . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']))) . "\n" .
    176             "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
    177     }
    178 
     169   
     170    // Compact the input, apply the filters, and extract them back out
     171    extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers' ) ) );
     172   
     173    // Default headers
     174    if ( empty( $headers ) ) {
     175        $headers = array(
     176            'MIME-Version' => '1.0'
     177        );
     178    } elseif ( !is_array( $headers ) ) {
     179        // Explode the headers out, so this function can take both
     180        // string headers and an array of headers.
     181        $tempheaders = (array) explode( "\n", $headers );
     182        $headers = array();
     183       
     184        // If it's actually got contents
     185        if ( !empty( $tempheaders ) ) {
     186            // Iterate through the raw headers
     187            foreach ( $tempheaders as $header ) {
     188                // Explode them out
     189                list( $name, $content ) = explode( ':', trim( $header ), 2 );
     190               
     191                // Cleanup crew
     192                $name = trim( $name );
     193                $content = trim( $content );
     194               
     195                // Mainly for legacy -- process a From: header if it's there
     196                if ( $name == 'From' ) {
     197                    if ( strpos( '<', $content ) !== false ) {
     198                        // So... making my life hard again?
     199                        $from_name = substr( $content, 0, strpos( '<', $content ) - 1 );
     200                        $from_name = str_replace( '"', '', $from_name );
     201                        $from_name = trim( $from_name );
     202                       
     203                        $from_email = substr( $content, strpos( '<', $content ) + 1 );
     204                        $from_email = str_replace( '>', '', $from_email );
     205                        $from_email = trim( $from_email );
     206                    } else {
     207                        $from_name = trim( $content );
     208                    }
     209                } elseif ( $name == 'Content-Type' ) {
     210                    if ( strpos( ';', $content ) !== false ) {
     211                        list( $type, $charset ) = explode( ';', $content );
     212                        $content_type = trim( $content_type );
     213                        $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
     214                    } else {
     215                        $content_type = trim( $content );
     216                    }
     217                } else {
     218                    // Add it to our grand headers array
     219                    $headers[trim( $name )] = trim( $content );
     220                }
     221            }
     222        }
     223    }
     224   
     225    // Empty out the values that may be set
    179226    $phpmailer->ClearAddresses();
     227    $phpmailer->ClearAllRecipients();
     228    $phpmailer->ClearAttachments();
     229    $phpmailer->ClearBCCs();
    180230    $phpmailer->ClearCCs();
    181     $phpmailer->ClearBCCs();
     231    $phpmailer->ClearCustomHeaders();
    182232    $phpmailer->ClearReplyTos();
    183     $phpmailer->ClearAllRecipients();
    184     $phpmailer->ClearCustomHeaders();
    185 
    186     $phpmailer->FromName = "WordPress";
    187     $phpmailer->AddAddress("$to", "");
     233   
     234    // From email and name
     235    // If we don't have a name from the input headers
     236    if ( !isset( $from_name ) ) {
     237        $from_name = 'WordPress';
     238    }
     239   
     240    // If we don't have an email from the input headers
     241    if ( !isset( $from_email ) ) {
     242        // Get the site domain and get rid of www.
     243        $sitename = strtolower( $_SERVER['SERVER_NAME'] );
     244        if ( substr( $sitename, 0, 4 ) == 'www.' ) {
     245            $sitename = substr( $sitename, 4 );
     246        }
     247       
     248        $from_email = 'wordpress@' . $sitename;
     249    }
     250   
     251    // Set the from name and email
     252    $phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
     253    $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
     254   
     255    // Set destination address
     256    $phpmailer->AddAddress( $to );
     257   
     258    // Set mail's subject and body
    188259    $phpmailer->Subject = $subject;
    189     $phpmailer->Body    = $message;
    190     $phpmailer->IsHTML(false);
    191     $phpmailer->IsMail(); // set mailer to use php mail()
    192 
    193     do_action_ref_array('phpmailer_init', array(&$phpmailer));
    194 
    195     $mailheaders = (array) explode( "\n", $headers );
    196     foreach ( $mailheaders as $line ) {
    197         $header = explode( ":", $line );
    198         switch ( trim( $header[0] ) ) {
    199             case "From":
    200                 $from = trim( str_replace( '"', '', $header[1] ) );
    201                 if ( strpos( $from, '<' ) ) {
    202                     $phpmailer->FromName = str_replace( '"', '', substr( $header[1], 0, strpos( $header[1], '<' ) - 1 ) );
    203                     $from = trim( substr( $from, strpos( $from, '<' ) + 1 ) );
    204                     $from = str_replace( '>', '', $from );
    205                 } else {
    206                     $phpmailer->FromName = $from;
    207                 }
    208                 $phpmailer->From = trim( $from );
    209                 break;
    210             default:
    211                 if ( $line != '' && $header[0] != 'MIME-Version' && $header[0] != 'Content-Type' )
    212                     $phpmailer->AddCustomHeader( $line );
    213                 break;
     260    $phpmailer->Body = $message;
     261   
     262    // Set to use PHP's mail()
     263    $phpmailer->IsMail();
     264   
     265    // Set Content-Type and charset
     266    // If we don't have a content-type from the input headers
     267    if ( !isset( $content_type ) ) {
     268        $content_type = 'text/plain';
     269    }
     270   
     271    // Set whether it's plaintext or not, depending on $content_type
     272    if ( $content_type == 'text/html' ) {
     273        $phpmailer->IsHTML( true );
     274    } else {
     275        $phpmailer->IsHTML( false );
     276    }
     277   
     278    // If we don't have a charset from the input headers
     279    if ( !isset( $charset ) ) {
     280        $charset = get_bloginfo( 'charset' );
     281    }
     282   
     283    // Set the content-type and charset
     284    $phpmailer->ContentType = apply_filters( 'wp_mail_content_type', 'text/plain' );
     285    $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
     286   
     287    // Set custom headers
     288    if ( !empty( $headers ) ) {
     289        foreach ( $headers as $name => $content ) {
     290            $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
    214291        }
    215292    }
    216 
     293   
     294    do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
     295   
     296    // Send!
    217297    $result = @$phpmailer->Send();
    218 
     298   
    219299    return $result;
    220300}
  • trunk/wp-includes/pluggable.php

    r5441 r5639  
    157157endif;
    158158
    159 if ( !function_exists('wp_mail') ) :
    160 function wp_mail($to, $subject, $message, $headers = '') {
     159if ( !function_exists( 'wp_mail' ) ) :
     160function wp_mail( $to, $subject, $message, $headers = '' ) {
    161161    global $phpmailer;
    162 
    163     if ( !is_object( $phpmailer ) ) {
    164         require_once(ABSPATH . WPINC . '/class-phpmailer.php');
    165         require_once(ABSPATH . WPINC . '/class-smtp.php');
     162   
     163    // (Re)create it, if it's gone missing
     164    if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
     165        require_once ABSPATH . WPINC . '/class-phpmailer.php';
     166        require_once ABSPATH . WPINC . '/class-smtp.php';
    166167        $phpmailer = new PHPMailer();
    167168    }
    168 
    169     $mail = compact('to', 'subject', 'message', 'headers');
    170     $mail = apply_filters('wp_mail', $mail);
    171     extract($mail);
    172 
    173     if ( $headers == '' ) {
    174         $headers = "MIME-Version: 1.0\n" .
    175             "From: " . apply_filters('wp_mail_from', "wordpress@" . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']))) . "\n" .
    176             "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
    177     }
    178 
     169   
     170    // Compact the input, apply the filters, and extract them back out
     171    extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers' ) ) );
     172   
     173    // Default headers
     174    if ( empty( $headers ) ) {
     175        $headers = array(
     176            'MIME-Version' => '1.0'
     177        );
     178    } elseif ( !is_array( $headers ) ) {
     179        // Explode the headers out, so this function can take both
     180        // string headers and an array of headers.
     181        $tempheaders = (array) explode( "\n", $headers );
     182        $headers = array();
     183       
     184        // If it's actually got contents
     185        if ( !empty( $tempheaders ) ) {
     186            // Iterate through the raw headers
     187            foreach ( $tempheaders as $header ) {
     188                // Explode them out
     189                list( $name, $content ) = explode( ':', trim( $header ), 2 );
     190               
     191                // Cleanup crew
     192                $name = trim( $name );
     193                $content = trim( $content );
     194               
     195                // Mainly for legacy -- process a From: header if it's there
     196                if ( $name == 'From' ) {
     197                    if ( strpos( '<', $content ) !== false ) {
     198                        // So... making my life hard again?
     199                        $from_name = substr( $content, 0, strpos( '<', $content ) - 1 );
     200                        $from_name = str_replace( '"', '', $from_name );
     201                        $from_name = trim( $from_name );
     202                       
     203                        $from_email = substr( $content, strpos( '<', $content ) + 1 );
     204                        $from_email = str_replace( '>', '', $from_email );
     205                        $from_email = trim( $from_email );
     206                    } else {
     207                        $from_name = trim( $content );
     208                    }
     209                } elseif ( $name == 'Content-Type' ) {
     210                    if ( strpos( ';', $content ) !== false ) {
     211                        list( $type, $charset ) = explode( ';', $content );
     212                        $content_type = trim( $content_type );
     213                        $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
     214                    } else {
     215                        $content_type = trim( $content );
     216                    }
     217                } else {
     218                    // Add it to our grand headers array
     219                    $headers[trim( $name )] = trim( $content );
     220                }
     221            }
     222        }
     223    }
     224   
     225    // Empty out the values that may be set
    179226    $phpmailer->ClearAddresses();
     227    $phpmailer->ClearAllRecipients();
     228    $phpmailer->ClearAttachments();
     229    $phpmailer->ClearBCCs();
    180230    $phpmailer->ClearCCs();
    181     $phpmailer->ClearBCCs();
     231    $phpmailer->ClearCustomHeaders();
    182232    $phpmailer->ClearReplyTos();
    183     $phpmailer->ClearAllRecipients();
    184     $phpmailer->ClearCustomHeaders();
    185 
    186     $phpmailer->FromName = "WordPress";
    187     $phpmailer->AddAddress("$to", "");
     233   
     234    // From email and name
     235    // If we don't have a name from the input headers
     236    if ( !isset( $from_name ) ) {
     237        $from_name = 'WordPress';
     238    }
     239   
     240    // If we don't have an email from the input headers
     241    if ( !isset( $from_email ) ) {
     242        // Get the site domain and get rid of www.
     243        $sitename = strtolower( $_SERVER['SERVER_NAME'] );
     244        if ( substr( $sitename, 0, 4 ) == 'www.' ) {
     245            $sitename = substr( $sitename, 4 );
     246        }
     247       
     248        $from_email = 'wordpress@' . $sitename;
     249    }
     250   
     251    // Set the from name and email
     252    $phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
     253    $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
     254   
     255    // Set destination address
     256    $phpmailer->AddAddress( $to );
     257   
     258    // Set mail's subject and body
    188259    $phpmailer->Subject = $subject;
    189     $phpmailer->Body    = $message;
    190     $phpmailer->IsHTML(false);
    191     $phpmailer->IsMail(); // set mailer to use php mail()
    192 
    193     do_action_ref_array('phpmailer_init', array(&$phpmailer));
    194 
    195     $mailheaders = (array) explode( "\n", $headers );
    196     foreach ( $mailheaders as $line ) {
    197         $header = explode( ":", $line );
    198         switch ( trim( $header[0] ) ) {
    199             case "From":
    200                 $from = trim( str_replace( '"', '', $header[1] ) );
    201                 if ( strpos( $from, '<' ) ) {
    202                     $phpmailer->FromName = str_replace( '"', '', substr( $header[1], 0, strpos( $header[1], '<' ) - 1 ) );
    203                     $from = trim( substr( $from, strpos( $from, '<' ) + 1 ) );
    204                     $from = str_replace( '>', '', $from );
    205                 } else {
    206                     $phpmailer->FromName = $from;
    207                 }
    208                 $phpmailer->From = trim( $from );
    209                 break;
    210             default:
    211                 if ( $line != '' && $header[0] != 'MIME-Version' && $header[0] != 'Content-Type' )
    212                     $phpmailer->AddCustomHeader( $line );
    213                 break;
     260    $phpmailer->Body = $message;
     261   
     262    // Set to use PHP's mail()
     263    $phpmailer->IsMail();
     264   
     265    // Set Content-Type and charset
     266    // If we don't have a content-type from the input headers
     267    if ( !isset( $content_type ) ) {
     268        $content_type = 'text/plain';
     269    }
     270   
     271    // Set whether it's plaintext or not, depending on $content_type
     272    if ( $content_type == 'text/html' ) {
     273        $phpmailer->IsHTML( true );
     274    } else {
     275        $phpmailer->IsHTML( false );
     276    }
     277   
     278    // If we don't have a charset from the input headers
     279    if ( !isset( $charset ) ) {
     280        $charset = get_bloginfo( 'charset' );
     281    }
     282   
     283    // Set the content-type and charset
     284    $phpmailer->ContentType = apply_filters( 'wp_mail_content_type', 'text/plain' );
     285    $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
     286   
     287    // Set custom headers
     288    if ( !empty( $headers ) ) {
     289        foreach ( $headers as $name => $content ) {
     290            $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
    214291        }
    215292    }
    216 
     293   
     294    do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
     295   
     296    // Send!
    217297    $result = @$phpmailer->Send();
    218 
     298   
    219299    return $result;
    220300}
Note: See TracChangeset for help on using the changeset viewer.