From be37f0ec30b0f545596ebff099faab080bd00cdd Mon Sep 17 00:00:00 2001 From: "Mark A. Hershberger" Date: Thu, 28 Jul 2011 16:56:03 +0000 Subject: [PATCH] Reduce mail header differences by moving all the header creation code to one place. --- includes/UserMailer.php | 77 ++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/includes/UserMailer.php b/includes/UserMailer.php index 9323e0d24b..5140dc95d9 100644 --- a/includes/UserMailer.php +++ b/includes/UserMailer.php @@ -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" ); -- 2.20.1