[Mediawiki-l] newsletter extension (needs help )... (long mail as source included)

info at astro-schnack.de info at astro-schnack.de
Thu Mar 3 08:55:00 UTC 2005


Hi,

i am new to the list and wonder if i can ask some PHP question here:
if i fail the meaning of this list pls just ignore. thanks for answer anyhow ;)

NEWSLETTER : i couldnt find a function like this in mediawiki and did
not find an extension, too. during searching for extentions i decided
to write a simple extension to mail a newsletter to registered
users (only some dozend) but failed as i am not so trained with php

must mention that the newsletter function already worked fine
when i separated the newslettersend-module and used the (a little
modified)original emailform of specialpages to try it. but i wanted
a special entry in specialpages as NEWSLETTER and now mixed something up.
--the mailform appears but when i "submit" it complains class double-defined
  and i cant find out where
--i also want to separate the db-connection as this seems to be a little unsecure ;)
  (how?)

best regards Arno


##-----------------------------------------------------------
##newsletter.php
##-----------------------------------------------------------
<?php
# to use : include this file from your LocalSettings.php like this
# include("extensions/newsletter.php");

$wgExtensionFunctions[] = "wfnewsletter";
# This extension acts as a special page
require_once( 'includes/SpecialPage.php' );

function wfnewsletter() { //START EXTENSION FUNC
        class NewsLetter extends SpecialPage  {
        function NewsLetter() {
        SpecialPage::SpecialPage("NewsLetter");
        }

        function execute( $par ) {
                global $wgUser, $wgOut, $wgRequest;
                $nt = Title::newFromURL( "newFromURL");
                $nu = User::newFromName( "newFromName");
                $id = $nu->idForName();
                $f = showForm($target);
                if ( "success" == $action ) { $f->showSuccess(); }
                        else if ( "submit" == $action && $wgRequest->wasPosted() ) { $f->doSubmit(); }
                        else { $f->showForm( "" ); }
         } // end execute function
        } // end Newsletter class
        global $wgMessageCache;
        SpecialPage::addPage( new NewsLetter );
        $wgMessageCache->addMessage( "newsletter", "NEWSLETTER" );

        function EmailUserForm( $addr, $target )        {
                global $wgRequest;
                $this->mAddress = $addr;
                $this->target = "NEWSLETTER";
                $this->text = $wgRequest->getText( 'wpText' );
                $this->subject = $wgRequest->getText( 'wpSubject' );
        }//end function EmailUserForm

        function showForm( $err ){
                global $wgOut, $wgUser, $wgLang;
                $wgOut->setPagetitle("newsletter to all registered users");
                if ( $this->subject === "" ) { 
                        $this->subject = "[..........yourwikiname.....-newsletter] no. ";
                }
                $emf = "sender";
                $sender = $wgUser->getName();
                $emr = "re";
                $emm = "message";
                $ems = "send";
                $encSubject = htmlspecialchars( $this->subject );
                
                $titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
                $action = $titleObj->escapeLocalURL( "target={$this->target}&action=submit" );

                if ( "" != $err ) {
                        $wgOut->setSubtitle( wfMsg( "formerror" ) );
                        $wgOut->addHTML( "<p><font color='red' size='+1'>{$err}</font>\n" );
                }
                $wgOut->addHTML( "<p>
                <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
                <table border=0><tr>
                <td align=right>{$emf}:</td>
                <td align=left><strong>{$sender}</strong></td>
                </tr><tr>
                <td align=right>{$emt}:</td>
                <td align=left><strong>{$rcpt}</strong></td>
                </tr><tr>
                <td align=right>{$emr}:</td>
                <td align=left>
                <input type=text name=\"wpSubject\" value=\"{$encSubject}\">
                </td>
                </tr><tr>
                <td align=right>{$emm}:</td>
                <td align=left>
                <textarea name=\"wpText\" rows=10 cols=60 wrap=virtual>" . htmlspecialchars( $this->text ) .
                "</textarea>
                </td></tr><tr>
                <td>&nbsp;</td><td align=left>
                <input type=submit name=\"wpSend\" value=\"{$ems}\">
                </td></tr></table>
                </form>\n" );
        }//end function showForm

        function doSubmit(){
                global $wgOut, $wgUser, $wgLang, $wgOutputEncoding;
                $from = wfQuotedPrintable( $wgUser->getName() ) . " <" . $wgUser->getEmail() . ">";
                $mailResult = newslettersend( $this->mAddress, $from, wfQuotedPrintable( $this->subject ), $this->text );
                if (! $mailResult)
                {
                        $titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
                        $encTarget = wfUrlencode( $this->target );
                        $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
                }
                else
                        $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
        }// end function doSubmit()

        function showSuccess()  {
                global $wgOut, $wgUser;
                $wgOut->setPagetitle( wfMsg( "emailsent" ) );
                $wgOut->addHTML( wfMsg( "emailsenttext" ) );
                $wgOut->returnToMain( false );
        }//end function showSuccess()

        function newslettersend( $to, $from, $subject, $body ){
                global $wgUser, $wgSMTP, $wgOutputEncoding, $wgErrorString;
                $qto = wfQuotedPrintable( $to );
                $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" .
                "From: ..............yourname...........";
                "X-Mailer: ..........your newsletter";
                $wgErrorString = "";
                set_error_handler( "mailErrorHandler" );
                $Config_host     = "localhost";
                $ConfigDBname    = "...............your db";
                $Config_user     = "...............dbuser";
                $Config_password = "...............password";
                //connect to SQL
                $db = mysql_connect($Config_host, $Config_user,$Config_password) or die  ("no connection possible");
                //select database 
                mysql_select_db($ConfigDBname, $db) or die ("connection to DB failed");
                // define query
                $z = 0;
                $sql = "SELECT * FROM user";
                // execute query
                $result = mysql_query($sql);
                while($arr = mysql_fetch_array($result, MYSQL_ASSOC)) 
                    {
                    $name               = $arr['user_real_name'];
                    $username           = $arr['user_name'];
                    $email              = $arr['user_email'];
                    mail( "......try with your own email-adress first.....",$subject, $body, $headers );
                    echo($name);
                    $z++;
                    }
                restore_error_handler();
                return $wgErrorString;

                //close query
                mysql_free_result($result);
                //close sql-connection
                mysql_close($db);
        }//end function newsletter

        function mailErrorHandler( $code, $string ) {
                global $wgErrorString;
                $wgErrorString = preg_replace( "/^mail\(\): /", "", $string );
        }//end function mailErrorHandler
} # End of extension function

?>







More information about the MediaWiki-l mailing list