57 protected static function sendWithPear( $mailer, $dest, $headers, $body ) {
58 $mailResult = $mailer->send( $dest, $headers, $body );
61 if ( PEAR::isError( $mailResult ) ) {
62 wfDebug(
"PEAR::Mail failed: " . $mailResult->getMessage() );
63 return Status::newFatal(
'pear-mail-error', $mailResult->getMessage() );
65 return Status::newGood();
111 public static function send( $to, $from, $subject, $body, $options = [] ) {
112 $services = MediaWikiServices::getInstance();
113 $allowHTMLEmail = $services->getMainConfig()->get(
114 MainConfigNames::AllowHTMLEmail );
116 if ( !isset( $options[
'contentType'] ) ) {
117 $options[
'contentType'] =
'text/plain; charset=UTF-8';
120 if ( !is_array( $to ) ) {
131 !is_array( $body ) &&
132 strlen( $body ) >= $minBodyLen
137 isset( $body[
'text'] ) &&
138 isset( $body[
'html'] ) &&
139 strlen( $body[
'text'] ) >= $minBodyLen &&
140 strlen( $body[
'html'] ) >= $minBodyLen
144 return Status::newFatal(
'user-mail-no-body' );
147 if ( !$allowHTMLEmail && is_array( $body ) ) {
149 $body = $body[
'text'];
152 wfDebug( __METHOD__ .
': sending mail to ' . implode(
', ', $to ) );
155 $has_address =
false;
156 foreach ( $to as $u ) {
162 if ( !$has_address ) {
163 return Status::newFatal(
'user-mail-no-addy' );
168 if ( count( $to ) > 1 ) {
170 (
new HookRunner( $services->getHookContainer() ) )->onUserMailerSplitTo( $to );
171 if ( $oldTo != $to ) {
172 $splitTo = array_diff( $oldTo, $to );
173 $to = array_diff( $oldTo, $splitTo );
175 $status = Status::newGood();
177 $status->merge( self::sendInternal(
178 $to, $from, $subject, $body, $options ) );
180 foreach ( $splitTo as $newTo ) {
181 $status->merge( self::sendInternal(
182 [ $newTo ], $from, $subject, $body, $options ) );
188 return self::sendInternal( $to, $from, $subject, $body, $options );
211 $services = MediaWikiServices::getInstance();
212 $mainConfig = $services->getMainConfig();
213 $smtp = $mainConfig->get( MainConfigNames::SMTP );
214 $enotifMaxRecips = $mainConfig->get( MainConfigNames::EnotifMaxRecips );
215 $additionalMailParams = $mainConfig->get( MainConfigNames::AdditionalMailParams );
217 $replyto = $options[
'replyTo'] ??
null;
218 $contentType = $options[
'contentType'] ??
'text/plain; charset=UTF-8';
219 $headers = $options[
'headers'] ?? [];
221 $hookRunner =
new HookRunner( $services->getHookContainer() );
225 if ( !$hookRunner->onUserMailerTransformContent( $to, $from, $body, $error ) ) {
227 return Status::newFatal(
'php-mail-error', $error );
229 return Status::newFatal(
'php-mail-error-unknown' );
262 $headers[
'From'] = $from->
toString();
263 $returnPath = $from->address;
264 $extraParams = $additionalMailParams;
267 $hookRunner->onUserMailerChangeReturnPath( $to, $returnPath );
277 $returnPathCLI =
'"' . str_replace(
'"',
'', $returnPath ) .
'"';
278 $extraParams .=
' -f ' . $returnPathCLI;
280 $headers[
'Return-Path'] = $returnPath;
283 $headers[
'Reply-To'] = $replyto->
toString();
286 $headers[
'Date'] = MWTimestamp::getLocalInstance()->format(
'r' );
287 $headers[
'Message-ID'] = self::makeMsgId();
288 $headers[
'X-Mailer'] =
'MediaWiki mailer';
289 $headers[
'List-Unsubscribe'] =
'<' . SpecialPage::getTitleFor(
'Preferences' )
296 if ( is_array( $body ) ) {
298 wfDebug(
"Assembling multipart mime email" );
300 $body[
'text'] = str_replace(
"\n",
"\r\n", $body[
'text'] );
301 $body[
'html'] = str_replace(
"\n",
"\r\n", $body[
'html'] );
303 $mime =
new Mail_mime( [
305 'text_charset' =>
'UTF-8',
306 'html_charset' =>
'UTF-8'
308 $mime->setTXTBody( $body[
'text'] );
309 $mime->setHTMLBody( $body[
'html'] );
310 $body = $mime->get();
311 $headers = $mime->headers( $headers );
315 $body = str_replace(
"\n",
"\r\n", $body );
317 $headers[
'MIME-Version'] =
'1.0';
318 $headers[
'Content-type'] = $contentType;
319 $headers[
'Content-transfer-encoding'] =
'8bit';
323 if ( !$hookRunner->onUserMailerTransformMessage(
324 $to, $from, $subject, $headers, $body, $error )
327 return Status::newFatal(
'php-mail-error', $error );
329 return Status::newFatal(
'php-mail-error-unknown' );
333 $ret = $hookRunner->onAlternateUserMailer( $headers, $to, $from, $subject, $body );
334 if ( $ret ===
false ) {
336 LoggerFactory::getInstance(
'usermailer' )->info(
337 "Email to {to} from {from} with subject {subject} handled by AlternateUserMailer",
339 'to' => $to[0]->toString(),
340 'allto' => implode(
', ', array_map(
'strval', $to ) ),
342 'subject' => $subject,
345 return Status::newGood();
346 } elseif ( $ret !==
true ) {
348 return Status::newFatal(
'php-mail-error', $ret );
351 if ( is_array( $smtp ) ) {
352 $recips = array_map(
'strval', $to );
355 $mail_object = Mail::factory(
'smtp', $smtp );
356 if ( PEAR::isError( $mail_object ) ) {
357 wfDebug(
"PEAR::Mail factory failed: " . $mail_object->getMessage() );
358 return Status::newFatal(
'pear-mail-error', $mail_object->getMessage() );
360 '@phan-var Mail_smtp $mail_object';
362 wfDebug(
"Sending mail via PEAR::Mail" );
364 $headers[
'Subject'] = self::quotedPrintable( $subject );
367 if ( count( $recips ) == 1 ) {
368 $headers[
'To'] = $recips[0];
373 $chunks = array_chunk( $recips, $enotifMaxRecips );
374 foreach ( $chunks as $chunk ) {
375 $status = self::sendWithPear( $mail_object, $chunk, $headers, $body );
377 if ( !$status->isOK() ) {
381 return Status::newGood();
384 if ( count( $to ) > 1 ) {
385 $headers[
'To'] =
'undisclosed-recipients:;';
388 wfDebug(
"Sending mail via internal mail() function" );
390 self::$mErrorString =
'';
391 $html_errors = ini_get(
'html_errors' );
392 ini_set(
'html_errors',
'0' );
393 set_error_handler( [ self::class,
'errorHandler' ] );
396 foreach ( $to as $recip ) {
399 self::quotedPrintable( $subject ),
405 }
catch ( Exception $e ) {
406 restore_error_handler();
410 restore_error_handler();
411 ini_set(
'html_errors', $html_errors );
413 if ( self::$mErrorString ) {
414 wfDebug(
"Error sending mail: " . self::$mErrorString );
415 return Status::newFatal(
'php-mail-error', self::$mErrorString );
416 } elseif ( !$sent ) {
419 wfDebug(
"Unknown error sending mail" );
420 return Status::newFatal(
'php-mail-error-unknown' );
422 LoggerFactory::getInstance(
'usermailer' )->info(
423 "Email sent to {to} from {from} with subject {subject}",
425 'to' => $to[0]->toString(),
426 'allto' => implode(
', ', array_map(
'strval', $to ) ),
428 'subject' => $subject,
431 return Status::newGood();