From: Brion Vibber Date: Wed, 13 Feb 2008 05:05:53 +0000 (+0000) Subject: * (bug 11567) Fix error checking for PEAR::Mail. UserMailer::send() now returns X-Git-Tag: 1.31.0-rc.0~49496 X-Git-Url: http://git.cyclocoop.org/%22.%24image2.%22?a=commitdiff_plain;h=3ddc44c0aaade0116c99b3da72b308b822fd223d;p=lhc%2Fweb%2Fwiklou.git * (bug 11567) Fix error checking for PEAR::Mail. UserMailer::send() now returns true-or-WikiError, which seems to be the calling convention expected by half its callers already --- diff --git a/RELEASE-NOTES b/RELEASE-NOTES index fa42763f41..bb34ac9add 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -389,6 +389,9 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 3269) Inaccessible titles ending in '/.' or '/..' now forbidden. * (bug 12935) Fully-qualify archive URLs correctly in deletion message * (bug 12938) Fix template expansion and 404 returns for action=raw with section +* (bug 11567) Fix error checking for PEAR::Mail. UserMailer::send() now returns + true-or-WikiError, which seems to be the calling convention expected by half + its callers already == Parser changes in 1.12 == diff --git a/includes/SpecialEmailuser.php b/includes/SpecialEmailuser.php index 35b37a52be..76add30486 100644 --- a/includes/SpecialEmailuser.php +++ b/includes/SpecialEmailuser.php @@ -4,8 +4,6 @@ * @addtogroup SpecialPage */ -require_once('UserMailer.php'); - /** * @todo document */ @@ -182,7 +180,8 @@ class EmailUserForm { $mailResult = UserMailer::send( $to, $mailFrom, $subject, $this->text, $replyTo ); if( WikiError::isError( $mailResult ) ) { - $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult); + $wgOut->addHTML( wfMsg( "usermailererror" ) . + ' ' . htmlspecialchars( $mailResult->getMessage() ) ); } else { // if the user requested a copy of this mail, do this now, @@ -190,14 +189,15 @@ class EmailUserForm { if ($this->cc_me && $to != $from) { $cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject); if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text ) ) ) { - $ccResult = userMailer( $from, $from, $cc_subject, $this->text ); + $ccResult = UserMailer::send( $from, $from, $cc_subject, $this->text ); if( WikiError::isError( $ccResult ) ) { // At this stage, the user's CC mail has failed, but their // original mail has succeeded. It's unlikely, but still, what to do? // We can either show them an error, or we can say everything was fine, // or we can say we sort of failed AND sort of succeeded. Of these options, // simply saying there was an error is probably best. - $wgOut->addHTML( wfMsg( "usermailererror" ) . $ccResult); + $wgOut->addHTML( wfMsg( "usermailererror" ) . + ' ' . htmlspecialchars( $ccResult->getMessage() ) ); return; } } diff --git a/includes/User.php b/includes/User.php index 66ecaee548..8e3c776a2f 100644 --- a/includes/User.php +++ b/includes/User.php @@ -2385,10 +2385,10 @@ class User { * * @param string $subject * @param string $body - * @param strong $from Optional from address; default $wgPasswordSender will be used otherwise. + * @param string $from Optional from address; default $wgPasswordSender will be used otherwise. * @return mixed True on success, a WikiError object on failure. */ - function sendMail( $subject, $body, $from = null ) { + function sendMail( $subject, $body, $from = null, $replyto = null ) { if( is_null( $from ) ) { global $wgPasswordSender; $from = $wgPasswordSender; @@ -2396,13 +2396,7 @@ class User { $to = new MailAddress( $this ); $sender = new MailAddress( $from ); - $error = UserMailer::send( $to, $sender, $subject, $body ); - - if( $error == '' ) { - return true; - } else { - return new WikiError( $error ); - } + return UserMailer::send( $to, $sender, $subject, $body, $replyto ); } /** diff --git a/includes/UserMailer.php b/includes/UserMailer.php index 389b6301e1..d043a6b59f 100644 --- a/includes/UserMailer.php +++ b/includes/UserMailer.php @@ -76,17 +76,14 @@ class UserMailer { */ protected static function sendWithPear($mailer, $dest, $headers, $body) { - $mailResult =& $mailer->send($dest, $headers, $body); + $mailResult = $mailer->send($dest, $headers, $body); # Based on the result return an error string, - if ($mailResult === true) { - return ''; - } elseif (is_object($mailResult)) { + if( PEAR::isError( $mailResult ) ) { wfDebug( "PEAR::Mail failed: " . $mailResult->getMessage() . "\n" ); - return $mailResult->getMessage(); + return new WikiError( $mailResult->getMessage() ); } else { - wfDebug( "PEAR::Mail failed, unknown error result\n" ); - return 'Mail object return unknown error.'; + return true; } } @@ -101,6 +98,7 @@ class UserMailer { * @param $subject String: email's subject. * @param $body String: email's text. * @param $replyto String: optional reply-to email (default: null). + * @return mixed True on success, a WikiError object on failure. */ static function send( $to, $from, $subject, $body, $replyto=null ) { global $wgSMTP, $wgOutputEncoding, $wgErrorString, $wgEnotifImpersonal; @@ -148,20 +146,16 @@ class UserMailer { $mail_object =& Mail::factory('smtp', $wgSMTP); if( PEAR::isError( $mail_object ) ) { wfDebug( "PEAR::Mail factory failed: " . $mail_object->getMessage() . "\n" ); - return $mail_object->getMessage(); + return new WikiError( $mail_object->getMessage() ); } wfDebug( "Sending mail via PEAR::Mail to $dest\n" ); - if (is_array($dest)) { - $chunks = array_chunk($dest, $wgEnotifMaxRecips); - foreach ($chunks as $chunk) { - $e = self::sendWithPear($mail_object, $chunk, $headers, $body); - if ($e != '') - return $e; - } - } else - return $mail_object->send($dest, $headers, $body); - + $chunks = array_chunk( (array)$dest, $wgEnotifMaxRecips ); + foreach ($chunks as $chunk) { + $e = self::sendWithPear($mail_object, $chunk, $headers, $body); + if( WikiError::isError( $e ) ) + return $e; + } } else { # In the following $headers = expression we removed "Reply-To: {$from}\r\n" , because it is treated differently # (fifth parameter of the PHP mail function, see some lines below) @@ -207,13 +201,13 @@ class UserMailer { if ( $wgErrorString ) { wfDebug( "Error sending mail: $wgErrorString\n" ); - return $wgErrorString; + return new WikiError( $wgErrorString ); } elseif (! $sent) { //mail function only tells if there's an error wfDebug( "Error sending mail\n" ); - return 'mailer error'; + return new WikiError( 'mailer error' ); } else { - return ''; + return true; } } } @@ -597,6 +591,3 @@ function wfRFC822Phrase( $s ) { function userMailer( $to, $from, $subject, $body, $replyto=null ) { return UserMailer::send( $to, $from, $subject, $body, $replyto ); } - - -