| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
use MediaWiki\HookContainer\HookRunner; |
| 26 |
use MediaWiki\Logger\LoggerFactory; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\MediaWikiServices; |
| 29 |
use MediaWiki\SpecialPage\SpecialPage; |
| 30 |
use MediaWiki\Status\Status; |
| 31 |
use MediaWiki\Utils\MWTimestamp; |
| 32 |
use MediaWiki\WikiMap\WikiMap; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class UserMailer { |
| 45 |
|
| 46 |
private static $mErrorString; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
protected static function sendWithPear( $mailer, $dest, $headers, $body ) { |
| 59 |
$mailResult = $mailer->send( $dest, $headers, $body ); |
| 60 |
|
| 61 |
|
| 62 |
if ( PEAR::isError( $mailResult ) ) { |
| 63 |
wfDebug( "PEAR::Mail failed: " . $mailResult->getMessage() ); |
| 64 |
return Status::newFatal( 'pear-mail-error', $mailResult->getMessage() ); |
| 65 |
} else { |
| 66 |
return Status::newGood(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private static function makeMsgId() { |
| 76 |
$services = MediaWikiServices::getInstance(); |
| 77 |
|
| 78 |
$smtp = $services->getMainConfig()->get( MainConfigNames::SMTP ); |
| 79 |
$server = $services->getMainConfig()->get( MainConfigNames::Server ); |
| 80 |
$domainId = WikiMap::getCurrentWikiDbDomain()->getId(); |
| 81 |
$msgid = uniqid( $domainId . ".", true ); |
| 82 |
|
| 83 |
if ( is_array( $smtp ) && isset( $smtp['IDHost'] ) && $smtp['IDHost'] ) { |
| 84 |
$domain = $smtp['IDHost']; |
| 85 |
} else { |
| 86 |
$domain = parse_url( $server, PHP_URL_HOST ) ?? ''; |
| 87 |
} |
| 88 |
return "<$msgid@$domain>"; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public static function send( $to, $from, $subject, $body, $options = [] ) { |
| 113 |
$services = MediaWikiServices::getInstance(); |
| 114 |
$allowHTMLEmail = $services->getMainConfig()->get( |
| 115 |
MainConfigNames::AllowHTMLEmail ); |
| 116 |
|
| 117 |
if ( !isset( $options['contentType'] ) ) { |
| 118 |
$options['contentType'] = 'text/plain; charset=UTF-8'; |
| 119 |
} |
| 120 |
|
| 121 |
if ( !is_array( $to ) ) { |
| 122 |
$to = [ $to ]; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
$minBodyLen = 10; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
if ( |
| 131 |
!( |
| 132 |
!is_array( $body ) && |
| 133 |
strlen( $body ) >= $minBodyLen |
| 134 |
) |
| 135 |
&& |
| 136 |
!( |
| 137 |
is_array( $body ) && |
| 138 |
isset( $body['text'] ) && |
| 139 |
isset( $body['html'] ) && |
| 140 |
strlen( $body['text'] ) >= $minBodyLen && |
| 141 |
strlen( $body['html'] ) >= $minBodyLen |
| 142 |
) |
| 143 |
) { |
| 144 |
|
| 145 |
return Status::newFatal( 'user-mail-no-body' ); |
| 146 |
} |
| 147 |
|
| 148 |
if ( !$allowHTMLEmail && is_array( $body ) ) { |
| 149 |
|
| 150 |
$body = $body['text']; |
| 151 |
} |
| 152 |
|
| 153 |
wfDebug( __METHOD__ . ': sending mail to ' . implode( ', ', $to ) ); |
| 154 |
|
| 155 |
|
| 156 |
$has_address = false; |
| 157 |
foreach ( $to as $u ) { |
| 158 |
if ( $u->address ) { |
| 159 |
$has_address = true; |
| 160 |
break; |
| 161 |
} |
| 162 |
} |
| 163 |
if ( !$has_address ) { |
| 164 |
return Status::newFatal( 'user-mail-no-addy' ); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
if ( count( $to ) > 1 ) { |
| 170 |
$oldTo = $to; |
| 171 |
( new HookRunner( $services->getHookContainer() ) )->onUserMailerSplitTo( $to ); |
| 172 |
if ( $oldTo != $to ) { |
| 173 |
$splitTo = array_diff( $oldTo, $to ); |
| 174 |
$to = array_diff( $oldTo, $splitTo ); |
| 175 |
|
| 176 |
$status = Status::newGood(); |
| 177 |
if ( $to ) { |
| 178 |
$status->merge( self::sendInternal( |
| 179 |
$to, $from, $subject, $body, $options ) ); |
| 180 |
} |
| 181 |
foreach ( $splitTo as $newTo ) { |
| 182 |
$status->merge( self::sendInternal( |
| 183 |
[ $newTo ], $from, $subject, $body, $options ) ); |
| 184 |
} |
| 185 |
return $status; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
return self::sendInternal( $to, $from, $subject, $body, $options ); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
protected static function sendInternal( |
| 206 |
array $to, |
| 207 |
MailAddress $from, |
| 208 |
$subject, |
| 209 |
$body, |
| 210 |
$options = [] |
| 211 |
) { |
| 212 |
$services = MediaWikiServices::getInstance(); |
| 213 |
$mainConfig = $services->getMainConfig(); |
| 214 |
$smtp = $mainConfig->get( MainConfigNames::SMTP ); |
| 215 |
$enotifMaxRecips = $mainConfig->get( MainConfigNames::EnotifMaxRecips ); |
| 216 |
$additionalMailParams = $mainConfig->get( MainConfigNames::AdditionalMailParams ); |
| 217 |
|
| 218 |
$replyto = $options['replyTo'] ?? null; |
| 219 |
$contentType = $options['contentType'] ?? 'text/plain; charset=UTF-8'; |
| 220 |
$headers = $options['headers'] ?? []; |
| 221 |
|
| 222 |
$hookRunner = new HookRunner( $services->getHookContainer() ); |
| 223 |
|
| 224 |
$error = false; |
| 225 |
|
| 226 |
if ( !$hookRunner->onUserMailerTransformContent( $to, $from, $body, $error ) ) { |
| 227 |
if ( $error ) { |
| 228 |
return Status::newFatal( 'php-mail-error', $error ); |
| 229 |
} else { |
| 230 |
return Status::newFatal( 'php-mail-error-unknown' ); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
$headers['From'] = $from->toString(); |
| 264 |
$returnPath = $from->address; |
| 265 |
$extraParams = $additionalMailParams; |
| 266 |
|
| 267 |
|
| 268 |
$hookRunner->onUserMailerChangeReturnPath( $to, $returnPath ); |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
$returnPathCLI = '"' . str_replace( '"', '', $returnPath ) . '"'; |
| 279 |
$extraParams .= ' -f ' . $returnPathCLI; |
| 280 |
|
| 281 |
$headers['Return-Path'] = $returnPath; |
| 282 |
|
| 283 |
if ( $replyto ) { |
| 284 |
$headers['Reply-To'] = $replyto->toString(); |
| 285 |
} |
| 286 |
|
| 287 |
$headers['Date'] = MWTimestamp::getLocalInstance()->format( 'r' ); |
| 288 |
$headers['Message-ID'] = self::makeMsgId(); |
| 289 |
$headers['X-Mailer'] = 'MediaWiki mailer'; |
| 290 |
$headers['List-Unsubscribe'] = '<' . SpecialPage::getTitleFor( 'Preferences' ) |
| 291 |
->getFullURL( '', false, PROTO_CANONICAL ) . '>'; |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
$endl = PHP_EOL; |
| 296 |
|
| 297 |
if ( is_array( $body ) ) { |
| 298 |
|
| 299 |
wfDebug( "Assembling multipart mime email" ); |
| 300 |
if ( wfIsWindows() ) { |
| 301 |
$body['text'] = str_replace( "\n", "\r\n", $body['text'] ); |
| 302 |
$body['html'] = str_replace( "\n", "\r\n", $body['html'] ); |
| 303 |
} |
| 304 |
$mime = new Mail_mime( [ |
| 305 |
'eol' => $endl, |
| 306 |
'text_charset' => 'UTF-8', |
| 307 |
'html_charset' => 'UTF-8' |
| 308 |
] ); |
| 309 |
$mime->setTXTBody( $body['text'] ); |
| 310 |
$mime->setHTMLBody( $body['html'] ); |
| 311 |
$body = $mime->get(); |
| 312 |
$headers = $mime->headers( $headers ); |
| 313 |
} else { |
| 314 |
|
| 315 |
if ( wfIsWindows() ) { |
| 316 |
$body = str_replace( "\n", "\r\n", $body ); |
| 317 |
} |
| 318 |
$headers['MIME-Version'] = '1.0'; |
| 319 |
$headers['Content-type'] = $contentType; |
| 320 |
$headers['Content-transfer-encoding'] = '8bit'; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
if ( !$hookRunner->onUserMailerTransformMessage( |
| 325 |
$to, $from, $subject, $headers, $body, $error ) |
| 326 |
) { |
| 327 |
if ( $error ) { |
| 328 |
return Status::newFatal( 'php-mail-error', $error ); |
| 329 |
} else { |
| 330 |
return Status::newFatal( 'php-mail-error-unknown' ); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
$ret = $hookRunner->onAlternateUserMailer( $headers, $to, $from, $subject, $body ); |
| 335 |
if ( $ret === false ) { |
| 336 |
|
| 337 |
LoggerFactory::getInstance( 'usermailer' )->info( |
| 338 |
"Email to {to} from {from} with subject {subject} handled by AlternateUserMailer", |
| 339 |
[ |
| 340 |
'to' => $to[0]->toString(), |
| 341 |
'allto' => implode( ', ', array_map( 'strval', $to ) ), |
| 342 |
'from' => $from->toString(), |
| 343 |
'subject' => $subject, |
| 344 |
] |
| 345 |
); |
| 346 |
return Status::newGood(); |
| 347 |
} elseif ( $ret !== true ) { |
| 348 |
|
| 349 |
return Status::newFatal( 'php-mail-error', $ret ); |
| 350 |
} |
| 351 |
|
| 352 |
if ( is_array( $smtp ) ) { |
| 353 |
$recips = array_map( 'strval', $to ); |
| 354 |
|
| 355 |
|
| 356 |
$mail_object = Mail::factory( 'smtp', $smtp ); |
| 357 |
if ( PEAR::isError( $mail_object ) ) { |
| 358 |
wfDebug( "PEAR::Mail factory failed: " . $mail_object->getMessage() ); |
| 359 |
return Status::newFatal( 'pear-mail-error', $mail_object->getMessage() ); |
| 360 |
} |
| 361 |
'@phan-var Mail_smtp $mail_object'; |
| 362 |
|
| 363 |
wfDebug( "Sending mail via PEAR::Mail" ); |
| 364 |
|
| 365 |
$headers['Subject'] = self::quotedPrintable( $subject ); |
| 366 |
|
| 367 |
|
| 368 |
if ( count( $recips ) == 1 ) { |
| 369 |
$headers['To'] = $recips[0]; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
$chunks = array_chunk( $recips, $enotifMaxRecips ); |
| 375 |
foreach ( $chunks as $chunk ) { |
| 376 |
$status = self::sendWithPear( $mail_object, $chunk, $headers, $body ); |
| 377 |
|
| 378 |
if ( !$status->isOK() ) { |
| 379 |
return $status; |
| 380 |
} |
| 381 |
} |
| 382 |
return Status::newGood(); |
| 383 |
} |
| 384 |
|
| 385 |
return self::sendWithMailFunction( $to, $from, $subject, $body, $headers, $extraParams ); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
private static function sendWithMailFunction( |
| 394 |
array $to, |
| 395 |
MailAddress $from, |
| 396 |
string $subject, |
| 397 |
string $body, |
| 398 |
array $headers, |
| 399 |
string $extraParams |
| 400 |
): Status { |
| 401 |
if ( count( $to ) > 1 ) { |
| 402 |
$headers['To'] = 'undisclosed-recipients:;'; |
| 403 |
} |
| 404 |
|
| 405 |
wfDebug( "Sending mail via internal mail() function" ); |
| 406 |
|
| 407 |
self::$mErrorString = ''; |
| 408 |
$html_errors = ini_get( 'html_errors' ); |
| 409 |
ini_set( 'html_errors', '0' ); |
| 410 |
set_error_handler( [ self::class, 'errorHandler' ] ); |
| 411 |
|
| 412 |
try { |
| 413 |
foreach ( $to as $recip ) { |
| 414 |
$sent = mail( |
| 415 |
$recip->toString(), |
| 416 |
self::quotedPrintable( $subject ), |
| 417 |
$body, |
| 418 |
$headers, |
| 419 |
$extraParams |
| 420 |
); |
| 421 |
} |
| 422 |
} catch ( Exception $e ) { |
| 423 |
restore_error_handler(); |
| 424 |
throw $e; |
| 425 |
} |
| 426 |
|
| 427 |
restore_error_handler(); |
| 428 |
ini_set( 'html_errors', $html_errors ); |
| 429 |
|
| 430 |
if ( self::$mErrorString ) { |
| 431 |
LoggerFactory::getInstance( 'usermailer' )->warning( |
| 432 |
"Error sending mail to {to} ({recipient_count} recipient(s)): {error}", |
| 433 |
self::mailLogContext( $to, $from ) + [ 'error' => self::$mErrorString ] |
| 434 |
); |
| 435 |
return Status::newFatal( 'php-mail-error', self::$mErrorString ); |
| 436 |
} elseif ( !$sent ) { |
| 437 |
|
| 438 |
|
| 439 |
LoggerFactory::getInstance( 'usermailer' )->warning( |
| 440 |
"Unknown error sending mail to {to} ({recipient_count} recipient(s))", |
| 441 |
self::mailLogContext( $to, $from ) |
| 442 |
); |
| 443 |
return Status::newFatal( 'php-mail-error-unknown' ); |
| 444 |
} |
| 445 |
LoggerFactory::getInstance( 'usermailer' )->info( |
| 446 |
"Email sent to {to} from {from} with subject {subject}", |
| 447 |
self::mailLogContext( $to, $from ) + [ |
| 448 |
'allto' => implode( ', ', array_map( 'strval', $to ) ), |
| 449 |
'subject' => $subject, |
| 450 |
] |
| 451 |
); |
| 452 |
return Status::newGood(); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
private static function mailLogContext( array $to, MailAddress $from ): array { |
| 461 |
return [ |
| 462 |
'to' => $to[0]->toString(), |
| 463 |
'recipient_count' => count( $to ), |
| 464 |
'recipient_domains' => self::recipientDomains( $to ), |
| 465 |
'from' => $from->toString(), |
| 466 |
]; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
private static function recipientDomains( array $to ): string { |
| 475 |
$domains = array_map( |
| 476 |
static fn ( MailAddress $recip ): string => substr( strrchr( $recip->address, '@' ) ?: '', 1 ), |
| 477 |
$to |
| 478 |
); |
| 479 |
return implode( ', ', array_unique( array_filter( $domains ) ) ); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
private static function errorHandler( $code, $string ) { |
| 489 |
if ( self::$mErrorString !== '' ) { |
| 490 |
self::$mErrorString .= "\n"; |
| 491 |
} |
| 492 |
self::$mErrorString .= preg_replace( '/^mail\(\)(\s*\[.*?\])?: /', '', $string ); |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
public static function sanitizeHeaderValue( $val ) { |
| 501 |
return strtr( $val, [ "\r" => '', "\n" => '' ] ); |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
public static function quotedPrintable( $string, $charset = '' ) { |
| 518 |
|
| 519 |
if ( !$charset ) { |
| 520 |
$charset = 'UTF-8'; |
| 521 |
} |
| 522 |
$charset = strtoupper( $charset ); |
| 523 |
$charset = str_replace( 'ISO-8859', 'ISO8859', $charset ); |
| 524 |
|
| 525 |
$illegal = '\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff='; |
| 526 |
if ( !preg_match( "/[$illegal]/", $string ) ) { |
| 527 |
return $string; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
$replace = $illegal . '.,\t ?_'; |
| 533 |
|
| 534 |
$out = "=?$charset?Q?"; |
| 535 |
$out .= preg_replace_callback( "/[$replace]/", |
| 536 |
static fn ( $m ) => sprintf( "=%02X", ord( $m[0] ) ), |
| 537 |
$string |
| 538 |
); |
| 539 |
$out .= '?='; |
| 540 |
return $out; |
| 541 |
} |
| 542 |
} |