40 private static $mErrorString;
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();
69 private static function makeMsgId() {
70 $smtp = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::SMTP );
71 $server = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Server );
72 $domainId = WikiMap::getCurrentWikiDbDomain()->getId();
73 $msgid = uniqid( $domainId .
".",
true );
74 if ( is_array( $smtp ) && isset( $smtp[
'IDHost'] ) && $smtp[
'IDHost'] ) {
75 $domain = $smtp[
'IDHost'];
78 $domain = $url[
'host'];
80 return "<$msgid@$domain>";
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 ) );
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" );
346 if ( count( $recips ) == 1 ) {
347 $headers[
'To'] = $recips[0];
352 $chunks = array_chunk( $recips, $enotifMaxRecips );
353 foreach ( $chunks as $chunk ) {
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();
412 private static function errorHandler( $code, $string ) {
413 self::$mErrorString = preg_replace(
'/^mail\(\)(\s*\[.*?\])?: /',
'', $string );
422 return strtr( $val, [
"\r" =>
'',
"\n" =>
'' ] );
434 $phrase = str_replace(
'"',
'', $phrase );
435 return '"' . $phrase .
'"';
456 $charset = strtoupper( $charset );
457 $charset = str_replace(
'ISO-8859',
'ISO8859', $charset );
459 $illegal =
'\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff=';
460 $replace = $illegal .
'\t ?_';
461 if ( !preg_match(
"/[$illegal]/", $string ) ) {
464 $out =
"=?$charset?Q?";
465 $out .= preg_replace_callback(
"/([$replace])/",
467 return sprintf(
"=%02X", ord(
$matches[1] ) );
wfIsWindows()
Check if the operating system is Windows.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Stores a single person's name and email address.
toString()
Return formatted and quoted address to insert into SMTP headers.
A class containing constants representing the names of configuration variables.
Parent class for all special pages.
Collection of static functions for sending mail.
static rfc822Phrase( $phrase)
Converts a string into a valid RFC 822 "phrase", such as is used for the sender name.
static sanitizeHeaderValue( $val)
Strips bad characters from a header value to prevent PHP mail header injection attacks.
static send( $to, $from, $subject, $body, $options=[])
This function will perform a direct (authenticated) login to a SMTP Server to use for mail relaying i...
static sendWithPear( $mailer, $dest, $headers, $body)
Send mail using a PEAR mailer.
static quotedPrintable( $string, $charset='')
Converts a string into quoted-printable format.
static sendInternal(array $to, MailAddress $from, $subject, $body, $options=[])
Helper function fo UserMailer::send() which does the actual sending.