53 protected static function sendWithPear( $mailer, $dest, $headers, $body ) {
54 $mailResult = $mailer->send( $dest, $headers, $body );
57 if ( PEAR::isError( $mailResult ) ) {
58 wfDebug(
"PEAR::Mail failed: " . $mailResult->getMessage() );
59 return Status::newFatal(
'pear-mail-error', $mailResult->getMessage() );
61 return Status::newGood();
102 public static function send( $to, $from, $subject, $body, $options = [] ) {
103 $services = MediaWikiServices::getInstance();
104 $allowHTMLEmail = $services->getMainConfig()->get(
105 MainConfigNames::AllowHTMLEmail );
107 if ( !isset( $options[
'contentType'] ) ) {
108 $options[
'contentType'] =
'text/plain; charset=UTF-8';
111 if ( !is_array( $to ) ) {
122 !is_array( $body ) &&
123 strlen( $body ) >= $minBodyLen
128 isset( $body[
'text'] ) &&
129 isset( $body[
'html'] ) &&
130 strlen( $body[
'text'] ) >= $minBodyLen &&
131 strlen( $body[
'html'] ) >= $minBodyLen
135 return Status::newFatal(
'user-mail-no-body' );
138 if ( !$allowHTMLEmail && is_array( $body ) ) {
140 $body = $body[
'text'];
143 wfDebug( __METHOD__ .
': sending mail to ' . implode(
', ', $to ) );
146 $has_address =
false;
147 foreach ( $to as $u ) {
153 if ( !$has_address ) {
154 return Status::newFatal(
'user-mail-no-addy' );
159 if ( count( $to ) > 1 ) {
161 (
new HookRunner( $services->getHookContainer() ) )->onUserMailerSplitTo( $to );
162 if ( $oldTo != $to ) {
163 $splitTo = array_diff( $oldTo, $to );
164 $to = array_diff( $oldTo, $splitTo );
166 $status = Status::newGood();
168 $status->merge( self::sendInternal(
169 $to, $from, $subject, $body, $options ) );
171 foreach ( $splitTo as $newTo ) {
172 $status->merge( self::sendInternal(
173 [ $newTo ], $from, $subject, $body, $options ) );
179 return self::sendInternal( $to, $from, $subject, $body, $options );
202 $services = MediaWikiServices::getInstance();
203 $mainConfig = $services->getMainConfig();
204 $smtp = $mainConfig->get( MainConfigNames::SMTP );
205 $enotifMaxRecips = $mainConfig->get( MainConfigNames::EnotifMaxRecips );
206 $additionalMailParams = $mainConfig->get( MainConfigNames::AdditionalMailParams );
208 $replyto = $options[
'replyTo'] ??
null;
209 $contentType = $options[
'contentType'] ??
'text/plain; charset=UTF-8';
210 $headers = $options[
'headers'] ?? [];
212 $hookRunner =
new HookRunner( $services->getHookContainer() );
216 if ( !$hookRunner->onUserMailerTransformContent( $to, $from, $body, $error ) ) {
218 return Status::newFatal(
'php-mail-error', $error );
220 return Status::newFatal(
'php-mail-error-unknown' );
253 $headers[
'From'] = $from->
toString();
254 $returnPath = $from->address;
255 $extraParams = $additionalMailParams;
258 $hookRunner->onUserMailerChangeReturnPath( $to, $returnPath );
268 $returnPathCLI =
'"' . str_replace(
'"',
'', $returnPath ) .
'"';
269 $extraParams .=
' -f ' . $returnPathCLI;
271 $headers[
'Return-Path'] = $returnPath;
274 $headers[
'Reply-To'] = $replyto->
toString();
277 $headers[
'Date'] = MWTimestamp::getLocalInstance()->format(
'r' );
278 $headers[
'Message-ID'] = self::makeMsgId();
279 $headers[
'X-Mailer'] =
'MediaWiki mailer';
280 $headers[
'List-Unsubscribe'] =
'<' . SpecialPage::getTitleFor(
'Preferences' )
287 if ( is_array( $body ) ) {
289 wfDebug(
"Assembling multipart mime email" );
291 $body[
'text'] = str_replace(
"\n",
"\r\n", $body[
'text'] );
292 $body[
'html'] = str_replace(
"\n",
"\r\n", $body[
'html'] );
294 $mime =
new Mail_mime( [
296 'text_charset' =>
'UTF-8',
297 'html_charset' =>
'UTF-8'
299 $mime->setTXTBody( $body[
'text'] );
300 $mime->setHTMLBody( $body[
'html'] );
301 $body = $mime->get();
302 $headers = $mime->headers( $headers );
306 $body = str_replace(
"\n",
"\r\n", $body );
308 $headers[
'MIME-Version'] =
'1.0';
309 $headers[
'Content-type'] = $contentType;
310 $headers[
'Content-transfer-encoding'] =
'8bit';
314 if ( !$hookRunner->onUserMailerTransformMessage(
315 $to, $from, $subject, $headers, $body, $error )
318 return Status::newFatal(
'php-mail-error', $error );
320 return Status::newFatal(
'php-mail-error-unknown' );
324 $ret = $hookRunner->onAlternateUserMailer( $headers, $to, $from, $subject, $body );
325 if ( $ret ===
false ) {
327 LoggerFactory::getInstance(
'usermailer' )->info(
328 "Email to {to} from {from} with subject {subject} handled by AlternateUserMailer",
330 'to' => $to[0]->toString(),
331 'allto' => implode(
', ', array_map(
'strval', $to ) ),
333 'subject' => $subject,
336 return Status::newGood();
337 } elseif ( $ret !==
true ) {
339 return Status::newFatal(
'php-mail-error', $ret );
342 if ( is_array( $smtp ) ) {
343 $recips = array_map(
'strval', $to );
346 $mail_object = Mail::factory(
'smtp', $smtp );
347 if ( PEAR::isError( $mail_object ) ) {
348 wfDebug(
"PEAR::Mail factory failed: " . $mail_object->getMessage() );
349 return Status::newFatal(
'pear-mail-error', $mail_object->getMessage() );
351 '@phan-var Mail_smtp $mail_object';
353 wfDebug(
"Sending mail via PEAR::Mail" );
355 $headers[
'Subject'] = self::quotedPrintable( $subject );
358 if ( count( $recips ) == 1 ) {
359 $headers[
'To'] = $recips[0];
364 $chunks = array_chunk( $recips, $enotifMaxRecips );
365 foreach ( $chunks as $chunk ) {
366 $status = self::sendWithPear( $mail_object, $chunk, $headers, $body );
368 if ( !$status->isOK() ) {
372 return Status::newGood();
375 if ( count( $to ) > 1 ) {
376 $headers[
'To'] =
'undisclosed-recipients:;';
379 wfDebug(
"Sending mail via internal mail() function" );
381 self::$mErrorString =
'';
382 $html_errors = ini_get(
'html_errors' );
383 ini_set(
'html_errors',
'0' );
384 set_error_handler( [ self::class,
'errorHandler' ] );
387 foreach ( $to as $recip ) {
390 self::quotedPrintable( $subject ),
396 }
catch ( Exception $e ) {
397 restore_error_handler();
401 restore_error_handler();
402 ini_set(
'html_errors', $html_errors );
404 if ( self::$mErrorString ) {
405 wfDebug(
"Error sending mail: " . self::$mErrorString );
406 return Status::newFatal(
'php-mail-error', self::$mErrorString );
407 } elseif ( !$sent ) {
410 wfDebug(
"Unknown error sending mail" );
411 return Status::newFatal(
'php-mail-error-unknown' );
413 LoggerFactory::getInstance(
'usermailer' )->info(
414 "Email sent to {to} from {from} with subject {subject}",
416 'to' => $to[0]->toString(),
417 'allto' => implode(
', ', array_map(
'strval', $to ) ),
419 'subject' => $subject,
422 return Status::newGood();