52 protected static function sendWithPear( $mailer, $dest, $headers, $body ) {
53 $mailResult = $mailer->send( $dest, $headers, $body );
56 if ( PEAR::isError( $mailResult ) ) {
57 wfDebug(
"PEAR::Mail failed: " . $mailResult->getMessage() );
58 return Status::newFatal(
'pear-mail-error', $mailResult->getMessage() );
60 return Status::newGood();
99 public static function send( $to, $from, $subject, $body, $options = [] ) {
100 $services = MediaWikiServices::getInstance();
101 $allowHTMLEmail = $services->getMainConfig()->get(
102 MainConfigNames::AllowHTMLEmail );
104 if ( !isset( $options[
'contentType'] ) ) {
105 $options[
'contentType'] =
'text/plain; charset=UTF-8';
108 if ( !is_array( $to ) ) {
119 !is_array( $body ) &&
120 strlen( $body ) >= $minBodyLen
125 isset( $body[
'text'] ) &&
126 isset( $body[
'html'] ) &&
127 strlen( $body[
'text'] ) >= $minBodyLen &&
128 strlen( $body[
'html'] ) >= $minBodyLen
132 return Status::newFatal(
'user-mail-no-body' );
135 if ( !$allowHTMLEmail && is_array( $body ) ) {
137 $body = $body[
'text'];
140 wfDebug( __METHOD__ .
': sending mail to ' . implode(
', ', $to ) );
143 $has_address =
false;
144 foreach ( $to as $u ) {
150 if ( !$has_address ) {
151 return Status::newFatal(
'user-mail-no-addy' );
156 if ( count( $to ) > 1 ) {
158 (
new HookRunner( $services->getHookContainer() ) )->onUserMailerSplitTo( $to );
159 if ( $oldTo != $to ) {
160 $splitTo = array_diff( $oldTo, $to );
161 $to = array_diff( $oldTo, $splitTo );
163 $status = Status::newGood();
165 $status->merge( self::sendInternal(
166 $to, $from, $subject, $body, $options ) );
168 foreach ( $splitTo as $newTo ) {
169 $status->merge( self::sendInternal(
170 [ $newTo ], $from, $subject, $body, $options ) );
176 return self::sendInternal( $to, $from, $subject, $body, $options );
199 $services = MediaWikiServices::getInstance();
200 $mainConfig = $services->getMainConfig();
201 $smtp = $mainConfig->get( MainConfigNames::SMTP );
202 $enotifMaxRecips = $mainConfig->get( MainConfigNames::EnotifMaxRecips );
203 $additionalMailParams = $mainConfig->get( MainConfigNames::AdditionalMailParams );
205 $replyto = $options[
'replyTo'] ??
null;
206 $contentType = $options[
'contentType'] ??
'text/plain; charset=UTF-8';
207 $headers = $options[
'headers'] ?? [];
209 $hookRunner =
new HookRunner( $services->getHookContainer() );
213 if ( !$hookRunner->onUserMailerTransformContent( $to, $from, $body, $error ) ) {
215 return Status::newFatal(
'php-mail-error', $error );
217 return Status::newFatal(
'php-mail-error-unknown' );
250 $headers[
'From'] = $from->
toString();
251 $returnPath = $from->address;
252 $extraParams = $additionalMailParams;
255 $hookRunner->onUserMailerChangeReturnPath( $to, $returnPath );
265 $returnPathCLI =
'"' . str_replace(
'"',
'', $returnPath ) .
'"';
266 $extraParams .=
' -f ' . $returnPathCLI;
268 $headers[
'Return-Path'] = $returnPath;
271 $headers[
'Reply-To'] = $replyto->
toString();
274 $headers[
'Date'] = MWTimestamp::getLocalInstance()->format(
'r' );
275 $headers[
'Message-ID'] = self::makeMsgId();
276 $headers[
'X-Mailer'] =
'MediaWiki mailer';
277 $headers[
'List-Unsubscribe'] =
'<' . SpecialPage::getTitleFor(
'Preferences' )
284 if ( is_array( $body ) ) {
286 wfDebug(
"Assembling multipart mime email" );
288 $body[
'text'] = str_replace(
"\n",
"\r\n", $body[
'text'] );
289 $body[
'html'] = str_replace(
"\n",
"\r\n", $body[
'html'] );
291 $mime =
new Mail_mime( [
293 'text_charset' =>
'UTF-8',
294 'html_charset' =>
'UTF-8'
296 $mime->setTXTBody( $body[
'text'] );
297 $mime->setHTMLBody( $body[
'html'] );
298 $body =
$mime->get();
299 $headers =
$mime->headers( $headers );
303 $body = str_replace(
"\n",
"\r\n", $body );
305 $headers[
'MIME-Version'] =
'1.0';
306 $headers[
'Content-type'] = $contentType;
307 $headers[
'Content-transfer-encoding'] =
'8bit';
311 if ( !$hookRunner->onUserMailerTransformMessage(
312 $to, $from, $subject, $headers, $body, $error )
315 return Status::newFatal(
'php-mail-error', $error );
317 return Status::newFatal(
'php-mail-error-unknown' );
321 $ret = $hookRunner->onAlternateUserMailer( $headers, $to, $from, $subject, $body );
322 if ( $ret ===
false ) {
324 return Status::newGood();
325 } elseif ( $ret !==
true ) {
327 return Status::newFatal(
'php-mail-error', $ret );
330 if ( is_array( $smtp ) ) {
331 $recips = array_map(
'strval', $to );
334 $mail_object = Mail::factory(
'smtp', $smtp );
335 if ( PEAR::isError( $mail_object ) ) {
336 wfDebug(
"PEAR::Mail factory failed: " . $mail_object->getMessage() );
337 return Status::newFatal(
'pear-mail-error', $mail_object->getMessage() );
339 '@phan-var Mail_smtp $mail_object';
341 wfDebug(
"Sending mail via PEAR::Mail" );
343 $headers[
'Subject'] = self::quotedPrintable( $subject );
346 if ( count( $recips ) == 1 ) {
347 $headers[
'To'] = $recips[0];
352 $chunks = array_chunk( $recips, $enotifMaxRecips );
353 foreach ( $chunks as $chunk ) {
354 $status = self::sendWithPear( $mail_object, $chunk, $headers, $body );
356 if ( !$status->isOK() ) {
360 return Status::newGood();
363 if ( count( $to ) > 1 ) {
364 $headers[
'To'] =
'undisclosed-recipients:;';
367 wfDebug(
"Sending mail via internal mail() function" );
369 self::$mErrorString =
'';
370 $html_errors = ini_get(
'html_errors' );
371 ini_set(
'html_errors',
'0' );
372 set_error_handler( [ self::class,
'errorHandler' ] );
375 foreach ( $to as $recip ) {
378 self::quotedPrintable( $subject ),
384 }
catch ( Exception $e ) {
385 restore_error_handler();
389 restore_error_handler();
390 ini_set(
'html_errors', $html_errors );
392 if ( self::$mErrorString ) {
393 wfDebug(
"Error sending mail: " . self::$mErrorString );
394 return Status::newFatal(
'php-mail-error', self::$mErrorString );
395 } elseif ( !$sent ) {
398 wfDebug(
"Unknown error sending mail" );
399 return Status::newFatal(
'php-mail-error-unknown' );
401 return Status::newGood();