Extension talk:New User Email Notification/Archives

From MediaWiki.org
Jump to navigation Jump to search

Add users e-mail address to the notification message[edit]

Hey, in most cases you probably don't need that, and it could even be problematic regarding the users privacy. But for certain cases, like me using a MediaWiki for an collaborative online course, I want to get the users e-mail address as soon as they register at the course wiki. Therefore I applied following changes:

In NewUserNotif.class.php I changed:

 89         private function makeMessage( $recipient, $user ) {
 90                 global $wgSitename, $wgContLang;
 91                 return wfMsgForContent(
 92                         'newusernotifbody',
 93                         $recipient,
 94                         $user->getName(),
 95                         $wgSitename,
 96                         $wgContLang->timeAndDate( wfTimestampNow() ),
 97                         $wgContLang->date( wfTimestampNow() ),
 98                         $wgContLang->time( wfTimestampNow() )
 99                 );

to this:

 89         private function makeMessage( $recipient, $user ) {
 90                 global $wgSitename, $wgContLang;
 91                 return wfMsgForContent(
 92                         'newusernotifbody',
 93                         $recipient,
 94                         $user->getName(),
 95                         $wgSitename,
 96                         $wgContLang->timeAndDate( wfTimestampNow() ),
 97                         $wgContLang->date( wfTimestampNow() ),
 98                         $wgContLang->time( wfTimestampNow() ),
 99                         $user->getEmail()
100                 );

Now you can put another parameter into the body text of the notification mail (MediaWiki:Newusernotifbody):

Parameter Value
$7 Users e-mail address

I thought I'll put this here in case somebody else needs it.

Best regards, --Bin!johnny 22:54, 27 February 2009 (UTC)

Enhancement Recommendation - Configurable Message parameters --jdpond 01:31, 25 July 2009 (UTC)[edit]

The original intent was to have customizable email notifications sent when a new user creates an account. The recommended modifications would allow the customization of which paramters are passed to the MediaWiki:Newusernotifbody and the MediaWiki:newusernotifsubj messages (body and subject respectively), and subsequently a far more flexible email message that is sent as notification. In the example, I've used a facilitated notification and approval process for users requesting access to a wiki.

The parameters passed to the email notification could be themselves parameterized and allowed to be extended/modified in the LocalSettings modifications as follows:

  • Modify NewUserNotif.php to create an array of parameter statements that would be passed to the MediaWiki:Newusernotifbody and MediaWiki:Newusernotifsubj messages.
  • Modify NewUserNotif.class.php to use the arrays in NewUserNotif.php (and any other additions from LocalSettings)
  • (Example Site customization) Modify LocalSettings to add the new parameter statements (if any) for MediaWiki:Newusernotifbody message into the $wgNewUserNotifSenderParam array (escaped, or single quoted into a string) AFTER NewUserNotif.php has been included.
require_once("$IP/extensions/NewUserNotif/NewUserNotif.php");
$wgNewUserNotifSenderParam[] = '$user->getEmail()';	// $7 email (example from above)
$wgNewUserNotifSenderParam[] = 'wfGetIP()';		// $8 IP address of server -
							// Suggested in "Further customisation" IP addresses 
  • (Example Site customization) Modify LocalSettings to add the new parameter statements (if any) for MediaWiki:Newusernotifsubj message into the $wgNewUserNotifSenderParam array (escaped, or single quoted into a string) AFTER NewUserNotif.php has been included.
$wgNewUserNotifSenderSubjParam[] = '$user->getname()';	// $2 requesting user name
$wgNewUserNotifSenderSubjParam[] = 'wfGetIP()';		// $3 IP address of server -
							// Suggested in "Further customisation" IP addresses 
  • (Example Site Customization) This example sends an email message with the new user information and two shortcut links that would a) Set User rights for the newly created account and b) Send an email notifying user of same. To do this, Change the MediaWiki:Newusernotifbody message to:
Hello $1,

A new user account, $2, has been created on $3 at $4 for $2<$7>.

If this is a desired user, you should approve at: {{fullurl:Special:UserRights}}/$2.

Then notify mailto:$2<$7>&subject=Account%20Approved%20for%20$3%20Access&body=A%20new%20user%20account,%20$2,%20has%20been%20approved%20on%20{{SITENAME}}%20at%20your%20request.%0A%0AYou%20now%20have%20approved%20rights%20at%20{{SERVER}}/{{SITENAME}}%20for%20this%20wiki.%0A%0AYour%20Friendly%20Sysop
  • (Example Site Customization) You probably will also want to change the welcome message, MediaWiki:Welcomecreation to something like:
== Welcome, $1! ==

Your account has been created. Don't forget to change your {{SITENAME}} preferences.  In order to have full access to this site, a systems operator has to approve your request.  You will be notified by email when this happens.


The required changes would be:


Index: NewUserNotif.php
===================================================================
--- NewUserNotif.php	(revision 52725)
+++ NewUserNotif.php	(working copy)
@@ -39,6 +39,26 @@
  * Additional email addresses to send mails to
  */
 $wgNewUserNotifEmailTargets = array();
+/**
+ *  These are the parameters that will be passed into MediaWiki:newusernotifbody
+ *  Can use anthing available as part of $this, $user (created user object), $recipient (target),
+ *		or from globals $wfContLang, $wgSitename
+ */
+$wgNewUserNotifSenderParam = array(
+			'$recipient',						// $1 Recipient
+			'$user->getname()',					// $2 User Name
+			'$wgSitename',						// $3 Site Name
+			'$wgContLang->timeAndDate( wfTimestampNow() )',		// $4 Time and date stamp
+			'$wgContLang->date( wfTimestampNow() )',		// $5 Date Stamp
+			'$wgContLang->time( wfTimestampNow() )'			// $6 Time Stamp
+			);
+/**
+ *  These are the parameters that will be passed into MediaWiki:Newusernotifsubj
+ *  parameters defs have same options as $wgNewUserNotifSenderParam
+ */
+$wgNewUserNotifSenderSubjParam = array(
+			'$wgSitename'						// $1 Site Name
+			);
 
 /**
  * Extension setup

Index: NewUserNotif.class.php
===================================================================
--- NewUserNotif.class.php	(revision 52725)
+++ NewUserNotif.class.php	(working copy)
@@ -38,13 +38,13 @@
 	 * Send email to external addresses
 	 */
 	private function sendExternalMails() {
-		global $wgNewUserNotifEmailTargets, $wgSitename;
+		global $wgNewUserNotifEmailTargets,$wgNewUserNotifSenderParam,$wgNewUserNotifSenderSubjParam;
 		foreach( $wgNewUserNotifEmailTargets as $target ) {
 			userMailer(
 				new MailAddress( $target ),
 				new MailAddress( $this->sender ),
-				wfMsgForContent( 'newusernotifsubj', $wgSitename ),
-				$this->makeMessage( $target, $this->user )
+				$this->makeMessage($target,'newusernotifsubj',$wgNewUserNotifSenderSubjParam),
+				$this->makeMessage($target,'newusernotifbody',$wgNewUserNotifSenderParam)
 			);
 		}
 	}
@@ -53,13 +53,13 @@
 	 * Send email to users
 	 */
 	private function sendInternalMails() {
-		global $wgNewUserNotifTargets, $wgSitename;
+		global $wgNewUserNotifEmailTargets,$wgNewUserNotifSenderParam,$wgNewUserNotifSenderSubjParam;
 		foreach( $wgNewUserNotifTargets as $userSpec ) {
 			$user = $this->makeUser( $userSpec );
 			if( $user instanceof User && $user->isEmailConfirmed() ) {
 				$user->sendMail(
-					wfMsgForContent( 'newusernotifsubj', $wgSitename ),
-					$this->makeMessage( $user->getName(), $this->user ),
+					$this->makeMessage($user->getName(),'newusernotifsubj',$wgNewUserNotifSenderSubjParam),
+					$this->makeMessage($user->getName(),'newusernotifbody',$wgNewUserNotifSenderParam),
 					$this->sender
 				);
 			}
@@ -81,22 +81,18 @@
 	}
 
 	/**
-	 * Build a notification email
+	 * Build a notification email message (body and subject)
 	 *
 	 * @param string $recipient Name of the recipient
-	 * @param User $user User that was created
+	 * @param string $msgId Localised Message Identifier
+	 * @param string $parmArr Array of Strings eval'd to pass parameters
+	 * @return string
 	 */
-	private function makeMessage( $recipient, $user ) {
-		global $wgSitename, $wgContLang;
-		return wfMsgForContent(
-			'newusernotifbody',
-			$recipient,
-			$user->getName(),
-			$wgSitename,
-			$wgContLang->timeAndDate( wfTimestampNow() ),
-			$wgContLang->date( wfTimestampNow() ),
-			$wgContLang->time( wfTimestampNow() )
-		);
+	private function makeMessage( $recipient,$msgId,$parmArr) {
+		global $wgSitename,$wgContLang;
+		$user = $this->user;
+		eval("\$retval = wfMsgForContent('".$msgId."',".implode(",",$parmArr).");");
+		return ($retval);
 	}
 
 	/**

This would be the first functional change on this extension since July 10, 2007.

Concerns about this approach[edit]

Using eval - while it is certainly not without precedent in MediaWiki, the concept of self-modifying code sends a shiver up any self respecting developer's spine. A better approach might be to enumerate the individual variables - but I'm too lazy to do that.

I have implemented and tested these changes on several of my sites and have commit access to SVN so could make this change. Also believe this is constent (and extends) robchurch's original intent of "Extension to provide a customisable email notification of new user creation" but thought I would bounce it here first.

If no one screams, will commit in the next couple of days and update the documentation.

(--jdpond 20:20, 25 July 2009 (UTC))

Re: "Further customisation" IP addresses[edit]

The method suggested in "Further customisation" for obtaining the IP address should probably be altered to use wfGetIP() instead of querying $_SERVER directly; that function will deal with proxies, etc. as far as possible. 86.133.208.13 13:22, 25 July 2009 (UTC)