Announcement mailer

From VoidWarranties - Hackerspace Antwerp, Belgium
Revision as of 04:19, 23 September 2014 by Kefcom (Talk | contribs)
Jump to: navigation, search


Announcement mailer
What:
A mechanisme that post the stus of the space (mailinglist, twitter)
Event.png
Participants:
Jeffrey
Category:
Coding
Locations:
Den Bunker

Background

Recently a separate mailinglist has been created for announcements only. It would be nice if new events, workshops, projects created with the forms on the wiki would be announced automatically on the announce mailinglist. This way people are getting notified on what's going on, without having to follow all the discussions. To accomplish this, a piece of software (plugin) should be written to send the mails.

how it works

The announcementmailer works through a tiny extension that hooks into the mediawiki installation. The extension will be triggered when a new 'article' is added through the wiki (by adding a hook to the ArticleInsertComplete event)

NOTE: the recipient and the sender email addresses are hardcoded in the script, you can easily change them when needed:

$recipient = new MailAddress( "antwerp.announce@discuss.hackerspaces.be");
$from = new MailAddress( "announcement@voidwarranties.be");

The sender email address has been added to the 'approved sender list' on the mailinglist. So the admins don't have to approve each mail that has been sent by this plugin.


The script is located at /var/www/extensions/AnnouncementMailer/AnnouncementMailer.php

<?php
 
if ( !defined( 'MEDIAWIKI' ) ) {
	echo 'Not a valid entry point';
	exit( 1 );
}
 
if ( !defined( 'SMW_VERSION' ) ) {
	echo 'This extension requires Semantic MediaWiki to be installed.';
	exit( 1 );
}
 
//classes
class AnnouncementMailer {
	public static function sendAnnouncement( &$article, &$user, $text, $summary, $minor, $watch, $sectionanchor, &$flags, $rev ) {
		$title = $article->getTitle();
		$title_text = $title->getFullText();
		$link = $title->escapeFullURL();
 
		$user_mailer = new UserMailer();
		$recipient = new MailAddress( "antwerp.announce@discuss.hackerspaces.be");
		$from = new MailAddress( "announcement@voidwarranties.be");
 
 
		if (substr($text, 2, 7) == "Project"){
			$short_description = AnnouncementMailer::getShortDescription($text);
			$subject = "New project: " . $title_text;
			$body = "Dear,";
			$body .= "\n\n" . $user->getName() . " has just created the project " . $title_text;
			$body .= "\n\nShort description:";
			$body .= "\n" . $short_description;
			$body .= "\n\nMore information can be found at: " .  $link;
			$body .= "\n\nRegards,";
			$body .= "\n\nVoidWarranties";
 
			$user_mailer->send($recipient, $from, $subject, $body);
		} elseif (substr($text, 2, 5) == "Event"){
			$short_description = AnnouncementMailer::getShortDescription($text);
			$subject = "New event: " . $title_text;
			$body = "Dear,";
			$body .= "\n\n" . $user->getName() . " has just created the event " . $title_text;
			$body .= "\n\nShort description:";
			$body .= "\n" . $short_description;
			$body .= "\n\nMore information can be found at: " .  $link;
			$body .= "\n\nRegards,";
			$body .= "\n\nVoidWarranties";
			$user_mailer->send($recipient, $from, $subject, $body);
		}
 
		return true;
	}
 
	public static function getShortDescription(&$text){
		$params = explode("}}", $text);
		$text_parts = explode("|", $params[0]);
		foreach ($text_parts as $part){
			if (substr($part, 0, 17) == "Short description"){
				return substr($part, 18, strlen($part));
			}
		}
		return "--";
	}
}
// i18n
$wgExtensionMessagesFiles['AnnouncementMailer'] = dirname( __FILE__ ) . '/AnnouncementMailer.i18n.php';
 
// Hooks
$wgHooks['ArticleInsertComplete'][] = 'AnnouncementMailer::sendAnnouncement';

To load the extension the following snippet has been added to /var/www/LocalSettings.php

########################AnnouncementMailer##############################
require_once( "$IP/extensions/AnnouncementMailer/AnnouncementMailer.php");
 
#For debugging errors:
#$wgShowExceptionDetails = true;

conclusion

It seems to work right now.

So content created on the wiki will be announced on the announcelist without need for approval etc...

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
Content Creation
Belgian Spaces