Reduce mail header differences by moving all the header creation code
authorMark A. Hershberger <mah@users.mediawiki.org>
Thu, 28 Jul 2011 16:56:03 +0000 (16:56 +0000)
committerMark A. Hershberger <mah@users.mediawiki.org>
Thu, 28 Jul 2011 16:56:03 +0000 (16:56 +0000)
to one place.

includes/UserMailer.php

index 9323e0d..5140dc9 100644 (file)
@@ -104,6 +104,21 @@ class UserMailer {
                }
        }
 
+       /**
+        * Creates a single string from an associative array
+        *
+        * @param $header Associative Array: keys are header field names,
+        *                values are ... values.
+        * @param $endl String: The end of line character.  Defaults to "\n"
+        * @return String
+        */
+       static function arrayToHeaderString( $headers, $endl = "\n" ) {
+               foreach( $headers as $name => $value ) {
+                       $string[] = "$name: $value";
+               }
+               return implode( $endl, $string );
+       }
+
        /**
         * This function will perform a direct (authenticated) login to
         * a SMTP Server to use for mail relaying if 'wgSMTP' specifies an
@@ -127,7 +142,7 @@ class UserMailer {
                        // This wouldn't be necessary if implode() worked on arrays of
                        // objects using __toString(). http://bugs.php.net/bug.php?id=36612
                        foreach ( $to as $t ) {
-                               $emails .= $t->toString() . ",";
+                                       $emails .= $t->toString() . ",";
                        }
                        $emails = rtrim( $emails, ',' );
                        wfDebug( __METHOD__ . ': sending mail to ' . $emails . "\n" );
@@ -135,6 +150,30 @@ class UserMailer {
                        wfDebug( __METHOD__ . ': sending mail to ' . implode( ',', array( $to->toString() ) ) . "\n" );
                }
 
+               $headers['From'] = $from->toString();
+               $headers['Return-Path'] = $from->toString();
+
+               if ( $wgEnotifImpersonal ) {
+                       $headers['To'] = 'undisclosed-recipients:;';
+               }
+               else {
+                       $headers['To'] = implode( ", ", (array )$dest );
+               }
+
+               if ( $replyto ) {
+                       $headers['Reply-To'] = $replyto->toString();
+               }
+               $headers['Subject'] = self::quotedPrintable( $subject );
+               $headers['Date'] = date( 'r' );
+               $headers['MIME-Version'] = '1.0';
+               $headers['Content-type'] = ( is_null( $contentType ) ?
+                       'text/plain; charset=UTF-8' : $contentType );
+               $headers['Content-transfer-encoding'] = '8bit';
+               // @todo FIXME
+               $headers['Message-ID'] = "<$msgid@" . $wgSMTP['IDHost'] . '>';
+               $headers['X-Mailer'] = 'MediaWiki mailer';
+               $headers['From'] = $from->toString();
+
                if ( is_array( $wgSMTP ) ) {
                        if ( function_exists( 'stream_resolve_include_path' ) ) {
                                $found = stream_resolve_include_path( 'Mail.php' );
@@ -160,29 +199,6 @@ class UserMailer {
                                $dest = $to->address;
                        }
 
-                       $headers['From'] = $from->toString();
-                       $headers['Return-Path'] = $from->toString();
-
-                       if ( $wgEnotifImpersonal ) {
-                               $headers['To'] = 'undisclosed-recipients:;';
-                       }
-                       else {
-                               $headers['To'] = implode( ', ', (array)$dest );
-                       }
-
-                       if ( $replyto ) {
-                               $headers['Reply-To'] = $replyto->toString();
-                       }
-                       $headers['Subject'] = self::quotedPrintable( $subject );
-                       $headers['Date'] = date( 'r' );
-                       $headers['MIME-Version'] = '1.0';
-                       $headers['Content-type'] = ( is_null( $contentType ) ?
-                                       'text/plain; charset=UTF-8' : $contentType );
-                       $headers['Content-transfer-encoding'] = '8bit';
-                       // @todo FIXME
-                       $headers['Message-ID'] = "<$msgid@" . $wgSMTP['IDHost'] . '>';
-                       $headers['X-Mailer'] = 'MediaWiki mailer';
-
                        wfSuppressWarnings();
 
                        // Create the mail object using the Mail::factory method
@@ -214,18 +230,7 @@ class UserMailer {
                                $endl = "\n";
                        }
 
-                       $headers = array(
-                               'MIME-Version: 1.0',
-                               "Content-type: $contentType",
-                               'Content-Transfer-Encoding: 8bit',
-                               'X-Mailer: MediaWiki mailer',
-                               'From: ' . $from->toString(),
-                       );
-                       if ( $replyto ) {
-                               $headers[] = 'Reply-To: ' . $replyto->toString();
-                       }
-
-                       $headers = implode( $endl, $headers );
+                       $headers = self::arrayToHeaderString( $headers, $endl );
 
                        wfDebug( "Sending mail via internal mail() function\n" );