/** * Return formatted and quoted address to insert into SMTP headers * @return string */ function toString() { if ($this->name != '') { $quoted = wfQuotedPrintable($this->name); if (strpos($quoted, '.') !== false) { $quoted = '"' . $quoted . '"'; } return "{$quoted} <{$this->address}>"; } else { return $this->address; } }
/** * Return formatted and quoted address to insert into SMTP headers * @return string */ function toString() { # PHP's mail() implementation under Windows is somewhat s***e, and # can't handle "Joe Bloggs <*****@*****.**>" format email addresses, # so don't bother generating them if ($this->name != '' && !wfIsWindows()) { $quoted = wfQuotedPrintable($this->name); if (strpos($quoted, '.') !== false) { $quoted = '"' . $quoted . '"'; } return "{$quoted} <{$this->address}>"; } else { return $this->address; } }
/** * This function will perform a direct (authenticated) login to * a SMTP Server to use for mail relaying if 'SMTP_params' specifies an * array of parameters. It requires PEAR:Mail to do that. * See Mail::factory() at http://pear.php.net/manual/en/package.mail.mail.factory.php * for a full description of these settings. (Scroll down to the smtp parameter.) * Otherwise it just uses the standard PHP 'mail' function. * * @param string $to recipient's email * @param string $from sender's email * @param string $subject email's subject * @param string $body email's text * @param string $encoding code page in which email's text is encoded */ function SendEMail($to, $from, $subject, $body, $encoding = "ISO-8859-1") { global $SMTP_params; $qto = wfQuotedPrintable($to); if (is_array($SMTP_params)) { // echo "Using SMTP_params, before requiring Mail.php<br>\r\n"; require_once 'pear/Mail.php'; // echo "Mail.php required just fine<br>\r\n"; $timestamp = time(); $headers['From'] = $from; $headers['To'] = $qto; $headers['Subject'] = $subject; $headers['MIME-Version'] = '1.0'; $headers['Content-type'] = 'text/plain; charset=' . $encoding; $headers['Content-transfer-encoding'] = '8bit'; $headers['Message-ID'] = "<{$timestamp}" . "jedihelp" . '@' . $SMTP_params['IDHost'] . '>'; $headers['X-Mailer'] = 'JediHelp e-mailer'; // Create the mail object using the Mail::factory method // echo "Creating mail object<br>\r\n"; $mail_object =& Mail::factory('smtp', $SMTP_params); // echo "Calling send method<br>\r\n"; $mailResult =& $mail_object->send($to, $headers, $body); // echo "before returning from SendEMail<br>\r\n"; # Based on the result return an error string, if ($mailResult === true) { return ''; } else { if (is_object($mailResult)) { return $mailResult->getMessage(); } else { return 'Mail object return unknown error.'; } } } else { // echo "No SMTP_Params, using mail function<br>\r\n"; $headers = "MIME-Version: 1.0\n" . "Content-type: text/plain; charset={$encoding}\n" . "Content-transfer-encoding: 8bit\n" . "From: {$from}\n" . "X-Mailer: JediHelp e-mailer"; $mailresult = mail($to, $subject, $body, $headers); if (!$mailresult) { return "Error sending mail with the mail() function."; } else { return ''; } } }
function notifyRequest($titleObj, $actualTitleObj) { global $wgUser, $wgServer, $wgScriptPath, $wgScript; $dbkey = $titleObj->getDBKey(); if ($actualTitleObj != null) { $dbkey = $actualTitleObj->getDBKey(); } //$sql = "SELECT user_name, user_real_name, user_email from page, revision, user where rev_user=user_id and page_namespace = " . NS_ARTICLE_REQUEST . " and page_title='" . $dbkey . "' and rev_page = page_id;"; $author_name = ""; if ($wgUser->getID() > 0) { $author_name = $wgUser->getRealName(); if ($author_name == "") { $author_name = $wgUser->getName(); } } $subject = wfMsg('howto', $titleObj->getText()); $text = wfMsg('howto', $titleObj->getText()) . "\n\n"; if ($wgUser->getID() > 0) { $text = wfMsg('request_answered_email_by_logged_in_user', $titleObj->getText(), $wgServer . $wgScriptPath . "/" . $titleObj->getDBKey(), $author_name, $wgServer . $wgScriptPath . "/User_talk:" . $wgUser->getName()); } else { $text = wfMsg('request_answered_email', $titleObj->getText(), $wgServer . $wgScriptPath . "/" . $titleObj->getDBKey()); } $dbr =& wfGetDB(DB_READ); $res = $dbr->select(array('page', 'revision', 'user'), array('user_name', 'user_real_name', 'user_email'), array('rev_user=user_id', 'page_namespace=' . NS_ARTICLE_REQUEST, 'page_title' => $dbkey, 'rev_page=page_id'), "notifyRequest", array('ORDER BY' => 'rev_id', 'LIMIT' => 1)); while (($row = $dbr->fetchObject($res)) != NULL) { $name = $row->user_real_name; if ($name == "") { $name = $row->user_name; } $email = $row->user_email; if ($email != "") { $to = new MailAddress($email); $from = new MailAddress("\"wikiHow\" <*****@*****.**>"); $mailResult = userMailer($to, $from, wfQuotedPrintable($subject), $text, false); } } $dbr->freeResult($res); }
function notifyRequest($titleObj, $actualTitleObj) { global $wgUser, $wgServer, $wgScriptPath, $wgScript; $dbkey = $titleObj->getDBKey(); if ($actualTitleObj) { $dbkey = $actualTitleObj->getDBKey(); } $author_name = ""; if ($wgUser->getID() > 0) { $author_name = $wgUser->getRealName(); if (!$author_name) { $author_name = $wgUser->getName(); } } $subject = wfMsg('howto', $titleObj->getText()); $text = wfMsg('howto', $titleObj->getText()) . "\n\n"; if ($wgUser->getID() > 0) { $text = wfMsg('request_answered_email_by_logged_in_user', $titleObj->getText(), $wgServer . $wgScriptPath . "/" . $titleObj->getDBKey(), $author_name, $wgServer . $wgScriptPath . "/User_talk:" . $wgUser->getName()); } else { $text = wfMsg('request_answered_email', $titleObj->getText(), $wgServer . $wgScriptPath . "/" . $titleObj->getDBKey()); } $dbr = wfGetDB(DB_READ); $res = $dbr->select(array('page', 'revision', 'user'), array('user_name', 'user_real_name', 'user_email'), array('rev_user=user_id', 'page_namespace=' . NS_ARTICLE_REQUEST, 'page_title' => $dbkey, 'rev_page=page_id'), __METHOD__, array('ORDER BY' => 'rev_id', 'LIMIT' => 1)); foreach ($res as $row) { $name = $row->user_real_name; if (!$name) { $name = $row->user_name; } $email = $row->user_email; if ($email) { $to = new MailAddress($email); $from = new MailAddress('"wikiHow" <*****@*****.**>'); $mailResult = userMailer($to, $from, wfQuotedPrintable($subject), $text, false); } } $dbr->freeResult($res); }
function testQuotedPrintable() { $this->assertEquals("=?UTF-8?Q?=C4=88u=20legebla=3F?=", wfQuotedPrintable("Ĉu legebla?", "UTF-8")); }
/** * This function will perform a direct (authenticated) login to * a SMTP Server to use for mail relaying if 'wgSMTP' specifies an * array of parameters. It requires PEAR:Mail to do that. * Otherwise it just uses the standard PHP 'mail' function. * * @param $to MailAddress: recipient's email * @param $from MailAddress: sender's email * @param $subject String: email's subject. * @param $body String: email's text. * @param $replyto MailAddress: optional reply-to email (default: null). * @param $contentType String: optional custom Content-Type * @return mixed True on success, a WikiError object on failure. */ static function send($to, $from, $subject, $body, $replyto = null, $contentType = null) { global $wgSMTP, $wgOutputEncoding, $wgErrorString, $wgEnotifImpersonal; global $wgEnotifMaxRecips; if (is_array($to)) { wfDebug(__METHOD__ . ': sending mail to ' . implode(',', $to) . "\n"); } else { wfDebug(__METHOD__ . ': sending mail to ' . implode(',', array($to->toString())) . "\n"); } if (is_array($wgSMTP)) { require_once 'Mail.php'; $msgid = str_replace(" ", "_", microtime()); if (function_exists('posix_getpid')) { $msgid .= '.' . posix_getpid(); } if (is_array($to)) { $dest = array(); foreach ($to as $u) { $dest[] = $u->address; } } else { $dest = $to->address; } $headers['From'] = $from->toString(); if ($wgEnotifImpersonal) { $headers['To'] = 'undisclosed-recipients:;'; } else { $headers['To'] = implode(", ", (array) $dest); } if ($replyto) { $headers['Reply-To'] = $replyto->toString(); } $headers['Subject'] = wfQuotedPrintable($subject); $headers['Date'] = date('r'); $headers['MIME-Version'] = '1.0'; $headers['Content-type'] = is_null($contentType) ? 'text/plain; charset=' . $wgOutputEncoding : $contentType; $headers['Content-transfer-encoding'] = '8bit'; $headers['Message-ID'] = "<{$msgid}@" . $wgSMTP['IDHost'] . '>'; // FIXME $headers['X-Mailer'] = 'MediaWiki mailer'; // Create the mail object using the Mail::factory method $mail_object =& Mail::factory('smtp', $wgSMTP); if (PEAR::isError($mail_object)) { wfDebug("PEAR::Mail factory failed: " . $mail_object->getMessage() . "\n"); return new WikiError($mail_object->getMessage()); } wfDebug("Sending mail via PEAR::Mail to {$dest}\n"); $chunks = array_chunk((array) $dest, $wgEnotifMaxRecips); foreach ($chunks as $chunk) { $e = self::sendWithPear($mail_object, $chunk, $headers, $body); if (WikiError::isError($e)) { return $e; } } } else { # In the following $headers = expression we removed "Reply-To: {$from}\r\n" , because it is treated differently # (fifth parameter of the PHP mail function, see some lines below) # Line endings need to be different on Unix and Windows due to # the bug described at http://trac.wordpress.org/ticket/2603 if (wfIsWindows()) { $body = str_replace("\n", "\r\n", $body); $endl = "\r\n"; } else { $endl = "\n"; } $ctype = is_null($contentType) ? 'text/plain; charset=' . $wgOutputEncoding : $contentType; $headers = "MIME-Version: 1.0{$endl}" . "Content-type: {$ctype}{$endl}" . "Content-Transfer-Encoding: 8bit{$endl}" . "X-Mailer: MediaWiki mailer{$endl}" . 'From: ' . $from->toString(); if ($replyto) { $headers .= "{$endl}Reply-To: " . $replyto->toString(); } $wgErrorString = ''; $html_errors = ini_get('html_errors'); ini_set('html_errors', '0'); set_error_handler(array('UserMailer', 'errorHandler')); wfDebug("Sending mail via internal mail() function\n"); if (function_exists('mail')) { if (is_array($to)) { foreach ($to as $recip) { $sent = mail($recip->toString(), wfQuotedPrintable($subject), $body, $headers); } } else { $sent = mail($to->toString(), wfQuotedPrintable($subject), $body, $headers); } } else { $wgErrorString = 'PHP is not configured to send mail'; } restore_error_handler(); ini_set('html_errors', $html_errors); if ($wgErrorString) { wfDebug("Error sending mail: {$wgErrorString}\n"); return new WikiError($wgErrorString); } elseif (!$sent) { //mail function only tells if there's an error wfDebug("Error sending mail\n"); return new WikiError('mailer error'); } else { return true; } } }
function doSubmit() { global $wgOut, $wgUser, $wgLang, $wgOutputEncoding; $from = wfQuotedPrintable($wgUser->getName()) . " <" . $wgUser->getEmail() . ">"; $subject = wfQuotedPrintable($this->subject); if (wfRunHooks('EmailUser', array(&$this->mAddress, &$from, &$subject, &$this->text))) { $mailResult = userMailer($this->mAddress, $from, $subject, $this->text); if (WikiError::isError($mailResult)) { $wgOut->addHTML(wfMsg("usermailererror") . $mailResult); } else { $titleObj = Title::makeTitle(NS_SPECIAL, "Emailuser"); $encTarget = wfUrlencode($this->target); $wgOut->redirect($titleObj->getFullURL("target={$encTarget}&action=success")); wfRunHooks('EmailUserComplete', array($this->mAddress, $from, $subject, $this->text)); } } }
/** * This function will perform a direct (authenticated) login to * a SMTP Server to use for mail relaying if 'wgSMTP' specifies an * array of parameters. It requires PEAR:Mail to do that. * Otherwise it just uses the standard PHP 'mail' function. * * @param $to MailAddress: recipient's email (or an array of them) * @param $from MailAddress: sender's email * @param $subject String: email's subject. * @param $body String: email's text. * @param $replyto MailAddress: optional reply-to email (default: null). * @param $contentType String: optional custom Content-Type * @return Status object */ public static function send($to, $from, $subject, $body, $replyto = null, $contentType = null) { global $wgSMTP, $wgOutputEncoding, $wgEnotifImpersonal; global $wgEnotifMaxRecips, $wgAdditionalMailParams; if (is_array($to)) { // This wouldn't be necessary if implode() worked on arrays of // objects using __toString(). http://bugs.php.net/bug.php?id=36612 foreach ($to as $t) { $emails .= $t->toString() . ","; } $emails = rtrim($emails, ','); wfDebug(__METHOD__ . ': sending mail to ' . $emails . "\n"); } else { wfDebug(__METHOD__ . ': sending mail to ' . implode(',', array($to->toString())) . "\n"); } if (is_array($wgSMTP)) { $found = false; $pathArray = explode(PATH_SEPARATOR, get_include_path()); foreach ($pathArray as $path) { if (file_exists($path . DIRECTORY_SEPARATOR . 'Mail.php')) { $found = true; break; } } if (!$found) { throw new MWException('PEAR mail package is not installed'); } require_once 'Mail.php'; $msgid = str_replace(" ", "_", microtime()); if (function_exists('posix_getpid')) { $msgid .= '.' . posix_getpid(); } if (is_array($to)) { $dest = array(); foreach ($to as $u) { $dest[] = $u->address; } } else { $dest = $to->address; } $headers['From'] = $from->toString(); if ($wgEnotifImpersonal) { $headers['To'] = 'undisclosed-recipients:;'; } else { $headers['To'] = implode(", ", (array) $dest); } if ($replyto) { $headers['Reply-To'] = $replyto->toString(); } $headers['Subject'] = wfQuotedPrintable($subject); $headers['Date'] = date('r'); $headers['MIME-Version'] = '1.0'; $headers['Content-type'] = is_null($contentType) ? 'text/plain; charset=' . $wgOutputEncoding : $contentType; $headers['Content-transfer-encoding'] = '8bit'; $headers['Message-ID'] = "<{$msgid}@" . $wgSMTP['IDHost'] . '>'; // FIXME $headers['X-Mailer'] = 'MediaWiki mailer'; wfSuppressWarnings(); // Create the mail object using the Mail::factory method $mail_object =& Mail::factory('smtp', $wgSMTP); if (PEAR::isError($mail_object)) { wfDebug("PEAR::Mail factory failed: " . $mail_object->getMessage() . "\n"); wfRestoreWarnings(); return Status::newFatal('pear-mail-error', $mail_object->getMessage()); } wfDebug("Sending mail via PEAR::Mail to {$dest}\n"); $chunks = array_chunk((array) $dest, $wgEnotifMaxRecips); foreach ($chunks as $chunk) { $status = self::sendWithPear($mail_object, $chunk, $headers, $body); if (!$status->isOK()) { wfRestoreWarnings(); return $status; } } wfRestoreWarnings(); return Status::newGood(); } else { # In the following $headers = expression we removed "Reply-To: {$from}\r\n" , because it is treated differently # (fifth parameter of the PHP mail function, see some lines below) # Line endings need to be different on Unix and Windows due to # the bug described at http://trac.wordpress.org/ticket/2603 if (wfIsWindows()) { $body = str_replace("\n", "\r\n", $body); $endl = "\r\n"; } else { $endl = "\n"; } $ctype = is_null($contentType) ? 'text/plain; charset=' . $wgOutputEncoding : $contentType; $headers = "MIME-Version: 1.0{$endl}" . "Content-type: {$ctype}{$endl}" . "Content-Transfer-Encoding: 8bit{$endl}" . "X-Mailer: MediaWiki mailer{$endl}" . 'From: ' . $from->toString(); if ($replyto) { $headers .= "{$endl}Reply-To: " . $replyto->toString(); } wfDebug("Sending mail via internal mail() function\n"); self::$mErrorString = ''; $html_errors = ini_get('html_errors'); ini_set('html_errors', '0'); set_error_handler(array('UserMailer', 'errorHandler')); if (is_array($to)) { foreach ($to as $recip) { $sent = mail($recip->toString(), wfQuotedPrintable($subject), $body, $headers, $wgAdditionalMailParams); } } else { $sent = mail($to->toString(), wfQuotedPrintable($subject), $body, $headers, $wgAdditionalMailParams); } restore_error_handler(); ini_set('html_errors', $html_errors); if (self::$mErrorString) { wfDebug("Error sending mail: " . self::$mErrorString . "\n"); return Status::newFatal('php-mail-error', self::$mErrorString); } elseif (!$sent) { // mail function only tells if there's an error wfDebug("Error sending mail\n"); return Status::newFatal('php-mail-error-unknown'); } else { return Status::newGood(); } } }
/** * This function will perform a direct (authenticated) login to * a SMTP Server to use for mail relaying if 'wgSMTP' specifies an * array of parameters. It requires PEAR:Mail to do that. * Otherwise it just uses the standard PHP 'mail' function. * * @param $to MailAddress: recipient's email * @param $from MailAddress: sender's email * @param $subject String: email's subject. * @param $body String: email's text. * @param $replyto String: optional reply-to email (default: null). * @return mixed True on success, a WikiError object on failure. */ static function send($to, $from, $subject, $body, $replyto = null, $content_type = 'text/plain; charset=utf-8') { global $wgSMTP, $wgOutputEncoding, $wgErrorString, $wgEnotifImpersonal; global $wgEnotifMaxRecips; if (is_array($to)) { wfDebug(__METHOD__ . ': sending mail to ' . implode(',', $to) . "\n"); } else { wfDebug(__METHOD__ . ': sending mail to ' . implode(',', array($to->toString())) . "\n"); } if (is_array($wgSMTP)) { require_once 'Mail.php'; $msgid = str_replace(" ", "_", microtime()); if (function_exists('posix_getpid')) { $msgid .= '.' . posix_getpid(); } if (is_array($to)) { $dest = array(); foreach ($to as $u) { $dest[] = $u->address; } } else { $dest = $to->address; } $headers['From'] = $from->toString(); if ($wgEnotifImpersonal) { $headers['To'] = 'undisclosed-recipients:;'; } else { $headers['To'] = $to->toString(); } if ($replyto) { $headers['Reply-To'] = $replyto->toString(); } $headers['Subject'] = wfQuotedPrintable($subject); $headers['Date'] = date('r'); $headers['MIME-Version'] = '1.0'; $headers['Content-type'] = $content_type; $headers['Content-transfer-encoding'] = '8bit'; $headers['Message-ID'] = "<{$msgid}@" . $wgSMTP['IDHost'] . '>'; // FIXME $headers['X-Mailer'] = 'MediaWiki mailer'; // Create the mail object using the Mail::factory method $mail_object =& Mail::factory('smtp', $wgSMTP); if (PEAR::isError($mail_object)) { wfDebug("PEAR::Mail factory failed: " . $mail_object->getMessage() . "\n"); return new WikiError($mail_object->getMessage()); } wfDebug("Sending mail via PEAR::Mail to {$dest}\n"); $chunks = array_chunk((array) $dest, $wgEnotifMaxRecips); foreach ($chunks as $chunk) { $e = self::sendWithPear($mail_object, $chunk, $headers, $body); if (WikiError::isError($e)) { return $e; } } } else { # In the following $headers = expression we removed "Reply-To: {$from}\r\n" , because it is treated differently # (fifth parameter of the PHP mail function, see some lines below) # Line endings need to be different on Unix and Windows due to # the bug described at http://trac.wordpress.org/ticket/2603 if (wfIsWindows()) { $body = str_replace("\n", "\r\n", $body); $endl = "\r\n"; } else { $endl = "\n"; } $headers = "MIME-Version: 1.0{$endl}" . "Content-type: {$content_type} {$endl}" . "Content-Transfer-Encoding: 8bit{$endl}" . "X-Mailer: MediaWiki mailer{$endl}" . 'From: ' . $from->toString(); if ($replyto) { $headers .= "{$endl}Reply-To: " . $replyto->toString(); } $wgErrorString = ''; $html_errors = ini_get('html_errors'); ini_set('html_errors', '0'); set_error_handler(array('UserMailer', 'errorHandler')); wfDebug("Sending mail via internal mail() function\n"); if (function_exists('mail')) { if (IS_PROD_EN_SITE || IS_PROD_INTL_SITE || IS_CLOUD_SITE) { if (is_array($to)) { foreach ($to as $recip) { $sent = mail($recip->toString(), wfQuotedPrintable($subject), $body, $headers); } } else { $sent = mail($to->toString(), wfQuotedPrintable($subject), $body, $headers); } } } else { $wgErrorString = 'PHP is not configured to send mail'; } restore_error_handler(); ini_set('html_errors', $html_errors); // ADDED - TRACK FOR SPAM global $wgLanguageCode, $wgUser; if ($wgLanguageCode == 'en') { $dbw = wfGetDB(DB_MASTER); $fullip = wfGetIP(); $baseip = substr($fullip, 0, strrpos($fullip, ".")); $bt = debug_backtrace(); $caller = ""; $line = ""; for ($i = 0; $i < sizeof($bt); $i++) { if (strpos($bt[$i]['file'], "UserMailer.php") === false) { $caller = $bt[$i]['file']; $caller = substr($caller, strrpos($caller, "/") + 1, strlen($caller)); $line = $bt[$i]['line']; break; } } $dbw->insert('sent_email', array('se_user' => $wgUser->getID(), 'se_user_text' => $wgUser->getName(), 'se_fullip' => $fullip, 'se_baseip' => $baseip, 'se_subject' => $subject, 'se_from' => $from->toString(), 'se_to' => is_array($to) ? print_r($to, true) : $to->toString(), 'se_caller' => $caller, 'se_caller_linenum' => $line, 'se_timestamp' => wfTimestampNow(TS_MW))); } // ADDED - TRACK FOR SPAM if ($wgErrorString) { wfDebug("Error sending mail: {$wgErrorString}\n"); return new WikiError($wgErrorString); } elseif (!$sent) { //mail function only tells if there's an error wfDebug("Error sending mail\n"); return new WikiError('mailer error'); } else { return true; } } }