Added the ability to do an authenticated SMTP login to send mail.
authorNicholas Pisarro, Jr <npassoc@users.mediawiki.org>
Fri, 27 Feb 2004 12:48:07 +0000 (12:48 +0000)
committerNicholas Pisarro, Jr <npassoc@users.mediawiki.org>
Fri, 27 Feb 2004 12:48:07 +0000 (12:48 +0000)
  * by setting the array variable '$wgSMTP' in Local settings,
    the source will use PEAR:mail to connect to a specific
    SMTP server to send mail. It will use an authenticated
    login, if requested.

  * Example:

    $wgSMTP = array (  "host" => "smtp.wherever.com",\r                       "IDHost" => "mail.wikipedia.org",\r                       "port" => "25",\r                       "auth" => true,\r                       "username" => "mailID",\r                       "password" => "mailPswd");

includes/DefaultSettings.php
includes/SpecialEmailuser.php
includes/SpecialUserlogin.php
includes/UserMailer.php [new file with mode: 0644]

index ea1515a..695d038 100644 (file)
@@ -31,6 +31,10 @@ $wgTmpDirectory     = "{$wgUploadDirectory}/tmp";
 $wgEmergencyContact = "wikiadmin@" . getenv( "SERVER_NAME" );
 $wgPasswordSender      = "Wikipedia Mail <apache@" . getenv( "SERVER_NAME" ) . ">";
 
+# For using a direct (authenticated) SMTP server connection.
+# "host" => 'SMTP domain', "IDHost" => 'domain for MessageID', "port" => "25", "auth" => true/false, "username" => user, "password" => password
+$wgSMTP                                = false;
+
 # MySQL settings
 #
 $wgDBserver         = "localhost";
index 5fac3c8..bdd6217 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+require_once('UserMailer.php');
+
 function wfSpecialEmailuser()
 {
        global $wgUser, $wgOut, $action, $target;
@@ -107,22 +109,17 @@ class EmailUserForm {
                global $wpSubject, $wpText, $target;
            
                $from = wfQuotedPrintable( $wgUser->getName() ) . " <" . $wgUser->getEmail() . ">";
-           $to = wfQuotedPrintable( $this->mAddress );
-
-               $headers =
-                       "MIME-Version: 1.0\r\n" .
-                       "Content-type: text/plain; charset={$wgOutputEncoding}\r\n" .
-                       "Content-transfer-encoding: 8bit\r\n" .
-                       "From: {$from}\r\n" .
-                       "Reply-To: {$from}\r\n" .
-                       "To: {$to}\r\n" .
-                       "X-Mailer: MediaWiki interuser e-mailer";
-               mail( $this->mAddress, wfQuotedPrintable( $wpSubject ), $wpText, $headers );
-
-
-               $success = wfLocalUrl( $wgLang->specialPage( "Emailuser" ),
-                 "target={$target}&action=success" );
-               $wgOut->redirect( $success );
+               
+               $mailResult = userMailer( $this->mAddress, $from, wfQuotedPrintable( $wpSubject ), $wpText );
+
+               if (! $mailResult)
+               {
+                       $success = wfLocalUrl( $wgLang->specialPage( "Emailuser" ),
+                               "target={$target}&action=success" );
+                       $wgOut->redirect( $success );
+               }
+               else
+                       $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
        }
 
        function showSuccess()
index 0ef1d3b..bdb389e 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+require_once('UserMailer.php');
+
 function wfSpecialUserlogin()
 {
        global $wpCreateaccount, $wpCreateaccountMail;
@@ -232,11 +234,7 @@ function wfSpecialUserlogin()
 
        $m = wfMsg( "passwordremindertext", $ip, $u->getName(), $np );
 
-       mail( $u->getEmail(), wfMsg( "passwordremindertitle" ), $m,
-         "MIME-Version: 1.0\r\n" .
-         "Content-type: text/plain; charset={$wgOutputEncoding}\r\n" .
-         "Content-transfer-encoding: 8bit\r\n" .
-         "From: $wgPasswordSender" );
+       userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( "passwordremindertitle" ), $m );
          
        return $u;
 }
diff --git a/includes/UserMailer.php b/includes/UserMailer.php
new file mode 100644 (file)
index 0000000..5a82988
--- /dev/null
@@ -0,0 +1,58 @@
+<?
+
+# 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.
+function userMailer( $to, $from, $subject, $body )
+{
+       global $wgUser, $wgSMTP, $wgOutputEncoding;
+       
+       $qto = wfQuotedPrintable( $to );
+       
+       if (is_array( $wgSMTP ))
+       {
+               include_once( "Mail.php" );
+               
+               $timestamp = time();
+       
+               $headers["From"] = $from;
+               $headers["To"] = $qto;
+               $headers["Subject"] = $subject;
+               $headers["MIME-Version"] = "1.0";
+               $headers["Content-type"] = "text/plain; charset={$wgOutputEncoding}";
+               $headers["Content-transfer-encoding"] = "8bit";
+               $headers["Message-ID"] = "<{$timestamp}" . $wgUser->getName() . "@" . $wgSMTP["IDHost"] . ">";
+               $headers["X-Mailer"] = "MediaWiki interuser e-mailer";
+       
+               // Create the mail object using the Mail::factory method
+               $mail_object =& Mail::factory("smtp", $wgSMTP);
+       
+               $mailResult =& $mail_object->send($to, $headers, $body);
+               
+               # 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
+       {
+               $headers =
+                       "MIME-Version: 1.0\r\n" .
+                       "Content-type: text/plain; charset={$wgOutputEncoding}\r\n" .
+                       "Content-transfer-encoding: 8bit\r\n" .
+                       "From: {$from}\r\n" .
+                       "Reply-To: {$from}\r\n" .
+                       "To: {$qto}\r\n" .
+                       "X-Mailer: MediaWiki interuser e-mailer";
+               mail( $to, $subject, $body, $headers );
+               
+               return "";
+       }
+}
+
+?>
\ No newline at end of file