Extension:FormMailer

From MediaWiki.org

Jump to: navigation, search

         

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
FormMailer

Release status: beta

Implementation  Notify
Description Formats and sends posted forms to email recipients.
Author(s)  User:Nad
Last Version  1.0.1 (2007-08-08)
MediaWiki  1.6.x and above
License No license specified
Download FormMailer.php

check usage (experimental)

This extension allows forms in the wiki to be sent to a specified list of recipients. This is useful for people using their MediaWiki to maintain website content who wish to have enquiry forms in the site. This extension only handles the sending of the data in the form, but does not offer any means to create forms, for that you can use Extension:Simple Forms.

Contents

[edit] Installation

The extension is a single script which you can obtain from OrganicDesign:Extension:FormMailer.php and save into your extensions directory and include in your LocalSettings.php file as in the following example,

include("$IP/extensions/FormMailer.php");

[edit] Usage

Any standard form can be mailed with this extension, all that needs to be done is to ensure that the form contains a value called "formmailer" in it. As long as a value of that name is posted, the formmailer will know that the data from the form is to be sent. The name of this special variable can be set to something else in the $wgFormMailerVarName global configuration variable.

[edit] Configuration

Configuration of this extension is done via two global variables which can be set in your LocalSettings file after the include of the FormMailer.php script.

Variable Default value Meaning
$wgFormMailerRecipients empty list A list of email addresses which should recieve posted forms.
$wgFormMailerVarName formmailer If a variable of this name is posted, the data is assumed to be for mailing.
$wgFormMailerFrom formmailer The FROM field in the mailed forms.
$wgFormMailerDontSend list of title, action The items in this list are considered as part of the environment and are not included in the mailed data.
$wgFormMailerMessage Thanks, your enquiry has been submitted! Message to display after sending the form (can also be set in the form by posting formmailer_message.
$wgFormMailerSubject Form submitted from SiteName Message to display after sending the form (can also be set in the form by posting formmailer_subject.

[edit] FormMailer.php

<?php
# Extension:FORMMAILER
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
# - Author:  [http://www.organicdesign.co.nz/nad User:Nad]
# - Started: 2007-06-17
# - See http://www.mediawiki.org/wiki/Extension:FormMailerfor installation and usage details
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
define('FORMMAILER_VERSION','1.0.2, 2008-10-29');
 
# A list of email addresses which should recieve posted forms
$wgFormMailerRecipients = array();
 
# If a variable of this name is posted, the data is assumed to be for mailing
$wgFormMailerVarName    = "formmailer";
 
# Name of sender of forms
$wgFormMailerFrom       = 'wiki@'.ereg_replace('^.+www.', '', $wgServer);
 
# Don't post the following posted items
$wgFormMailerDontSend   = array('title', 'action');
 
# Message to display after sending the form (can also be set in the form by posting formmailer_message
$wgFormMailerMessage    = "Thanks, your enquiry has been submitted!";
 
# Message to display after sending the form (can also be set in the form by posting formmailer_subject
$wgFormMailerSubject    = "Form submitted from $wgSitename";
 
 
$wgExtensionFunctions[] = 'wfSetupFormMailer';
 
$wgExtensionCredits['other'][] = array(
	'name'        => 'FormMailer',
	'author'      => '[http://www.organicdesign.co.nz/nad User:Nad]',
	'description' => 'Formats and sends posted form fields to email recipients',
	'url'         => 'http://www.mediawiki.org/wiki/Extension:FormMailer',
	'version'     => FORMMAILER_VERSION
);
 
function wfSetupFormMailer() {
	global $wgFormMailerVarName, $wgFormMailerRecipients, $wgFormMailerMessage, $wgFormMailerSubject,
		$wgFormMailerFrom, $wgFormMailerDontSend, $wgSimpleFormsRequestPrefix,
		$wgRequest, $wgSiteNotice, $wgSitename;
 
	if (isset($wgRequest->getText($wgSimpleFormsRequestPrefix.$wgFormMailerVarName))) {
 
		# Construct the message
		$body    = "Form posted from ".$_SERVER['REMOTE_ADDR']."\n\n";
		$message = $wgFormMailerMessage;
		$subject = $wgFormMailerSubject;
		foreach ($wgRequest->getValues() as $k => $v) if (!in_array($k, $wgFormMailerDontSend)) {
			if ($wgSimpleFormsRequestPrefix) $k = ereg_replace("^$wgSimpleFormsRequestPrefix", '', $k);
			$k = str_replace('_', ' ', $k);
			if     ($k == 'formmailer_message') $message = $v;
			elseif ($k == 'formmailer_subject') $subject = $v;
			elseif ($k != $wgFormMailerVarName) $body .= "$k: $v\n\n";
		}
 
		# Send to recipients using the MediaWiki mailer
		$err  = '';
		$user = new User();
		$from = "\"$wgSitename\"<$wgFormMailerFrom>";
		foreach ($wgFormMailerRecipients as $recipient) if (User::isValidEmailAddr($recipient)) {
			$user->setName($recipient);
			$user->setEmail($recipient);
			if ($user->sendMail($subject, $body, $from) !== true) $err = 'Failed to send!';
		}
		$wgSiteNotice .= "<div class='usermessage'>".($err ? $err : $message)."</div>";
	}
}

[edit] Todo & Bugs

  • Allow named recipient lists which can be specified in forms

[edit] See also

  • Extension:Simple Forms - A set of simple mechanisms for making forms, these can be used with FormMailer
  • Extension:EmailArticle - Send fully rendered articles with embedded CSS to users, groups or contact lists
  • Extension:EmailToWiki - Allows emails to be sent to and appended to articles in the wiki using pagename@your-wiki-domain