Skip to content

Commit

Permalink
Item15132: multiple improvements n fixes
Browse files Browse the repository at this point in the history
- don't use CGI to render html
- default to utf8 encoding in emails
- support sending mail forms via ajax
- remove hard-coded colors and styles from documentation
  • Loading branch information
MichaelDaum committed May 20, 2022
1 parent d743dea commit b5b12d8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 46 deletions.
41 changes: 12 additions & 29 deletions data/System/SendEmailPlugin.txt
@@ -1,4 +1,4 @@
%META:TOPICINFO{author="ProjectContributor" comment="" date="1262991297" format="1.1" reprev="1.2" version="1"}%
%META:TOPICINFO{author="ProjectContributor" comment="reprev" date="1652974852" format="1.1" version="1"}%
---+!! Send Email Plugin
%FORMFIELD{"Description"}%.

Expand Down Expand Up @@ -48,7 +48,6 @@ The HTML form encapsulates the data to be sent, each part passed as a form field

Note, that the =from=, =to= and =cc= parameters can be either an email address or a wiki user name, in which case the email address of that wiki user is used instead.

%TABLE{tablewidth="100%" columnwidths="15%,5%,25%,55%" sort="off" headerbg="#f5f5f5" databg="#ffffff" headercolor="#333333"}%
| *Element* | *Required* | *Description* | *Example* |
| =name,method= | Required | A form must have a name; send method is POST. | |
| =action= | Required | Must call =sendemail= script | =action="%<nop>SCRIPTURL{sendemail}%/%<nop>WEB%/%<nop>TOPIC%"= |
Expand All @@ -62,7 +61,6 @@ Note, that the =from=, =to= and =cc= parameters can be either an email address o
| =successsection= field | Optional | (Presumably) hidden field. Will display a named section (that you define using =STARTSECTION= and =ENDSECTION= after a successful sent. | =&lt;input type="hidden" name="successsection" value="feedbackYes" /&gt;= |
| =errorsection= field | Optional | (Presumably) hidden field. Will display a named section (that you define using =STARTSECTION= and =ENDSECTION= after an unsuccessful sent. | =&lt;input type="hidden" name="errorsection" value="feedbackNo" /&gt;= |


---+++ Allow/restrict settings
To prevent this plugin to be used as open email relay, by default sending emails is prohibited. To send out mails, first set the mail addresses you want to allow to send to in [[%SCRIPTURLPATH{configure}%#SendEmailPlugin$Extensions][configure]]. Each can be a list of comma separated regular expressions that emails are checked against before allowing the mail to be sent.
* ={Plugins}{SendEmailPlugin}{Permissions}{Allow}{MailTo}= - enter =.*= to allow to send to anyone
Expand Down Expand Up @@ -101,7 +99,6 @@ will write a feedback message (if any), using the default message strings set in

The message can be tailored using SENDEMAIL parameters:

%TABLE{tablewidth="100%" columnwidths="15%,35%,55%" sort="off" headerbg="#f5f5f5" databg="#ffffff" headercolor="#333333"}%
| *Parameter* | *Description* | *Example* |
| =feedbackSuccess= | Message string in case of successful send. | =feedbackSuccess="Request sent, we'll contact you shortly."= |
| =feedbackError= | Message string in case of unsuccessful send. | =feedbackError="Could not send your message, please contact us."= |
Expand Down Expand Up @@ -140,8 +137,6 @@ The search path for templates always starts in the current web.
%TWISTY{showlink=" Show code" hidelink=" Hide code" showimgleft="%ICONURLPATH{toggleopen}%" hideimgleft="%ICONURLPATH{toggleclose}%" mode="div"}%
<verbatim>
<form enctype="application/x-www-form-urlencoded" name="mailform1" action="%SCRIPTURL{sendemail}%/%WEB%/%TOPIC%" method="POST">
<fieldset>
<legend><b>Send Email</b></legend>
<div class="foswikiFormSteps">
<div class="foswikiFormStep">
<h3>To:</h3>
Expand All @@ -163,7 +158,6 @@ The search path for templates always starts in the current web.
<input type="submit" class="foswikiSubmit" value="Send" />
</div>
</div>
</fieldset>
</form>
%SENDEMAIL{
feedbackSuccess="Request sent, we'll contact you shortly."
Expand All @@ -174,8 +168,6 @@ format="<div class=\"foswikiNotification\">$message</div>"
%ENDTWISTY%

<form enctype="application/x-www-form-urlencoded" name="mailform" action="%SCRIPTURL{sendemail}%/%WEB%/%TOPIC%" method="POST">
<fieldset>
<legend><b>Send Email</b></legend>
<div class="foswikiFormSteps">
<div class="foswikiFormStep">
---+++!! To:
Expand All @@ -197,7 +189,6 @@ format="<div class=\"foswikiNotification\">$message</div>"
<input type="submit" class="foswikiSubmit" value="Send" />
</div>
</div>
</fieldset>
</form>
%SENDEMAIL{
feedbackSuccess="Request sent, we'll contact you shortly."
Expand All @@ -223,9 +214,6 @@ name="mailForm"
action="%SCRIPTURL{sendemail}%/%WEB%/%TOPIC%"
method="POST"
}%
<fieldset style="border:1px solid #ddd; padding:1em">
<legend><b>Send Email</b></legend>
%RED%*%ENDCOLOR% All fields are required.
%FORMELEMENT{
name="Subject"
type="hidden"
Expand Down Expand Up @@ -254,7 +242,6 @@ Email: $Email"
type="submit"
buttonlabel="Send"
}%
</fieldset>
%ENDFORM%

%SENDEMAIL{feedbackSuccess="Request sent, we'll contact you shortly." feedbackError="Could not send your message, please contact us." }%
Expand All @@ -266,9 +253,6 @@ name="mailForm"
action="%SCRIPTURL{sendemail}%/%WEB%/%TOPIC%"
method="POST"
}%
<fieldset style="border:1px solid #ddd; padding:1em">
<legend><b>Send Email</b></legend>
%RED%*%ENDCOLOR% All fields are required.
%FORMELEMENT{
name="Subject"
type="hidden"
Expand Down Expand Up @@ -297,7 +281,6 @@ Email: $Email"
type="submit"
buttonlabel="Send"
}%
</fieldset>
%ENDFORM%

%SENDEMAIL{
Expand All @@ -323,27 +306,27 @@ Summary: %FORMFIELD{"Summary"}%
</textarea>
</verbatim>


#PluginTest
---++ Plugin test
* !SendEmailPlugin is %IF{"context SendEmailPluginEnabled" then='%GREEN%enabled%ENDCOLOR%' else='%RED%not enabled%ENDCOLOR%'}%.
* !SendEmailPlugin is %IF{"context SendEmailPluginEnabled" then="$percntGREEN$percntenabled$percntENDCOLOR$percnt" else="$percntRED$percntnot enabled$percntENDCOLOR$percnt"}%.

#PluginSettings
---++ Plugin Settings * Plugin settings are set in [[%SCRIPTURLPATH{configure}%#SendEmailPlugin$Extensions][configure]].
* Note: by default any emailing is prohibited. Change the settings in configure to set to which addresses you allow mails to be sent from and to.
---++ Plugin Settings
Plugin settings are set in [[%SCRIPTURLPATH{configure}%#SendEmailPlugin$Extensions][configure]].
Note: by default any emailing is prohibited. Change the settings in configure to set to which addresses you allow mails to be sent from and to.

---++ CSS classes
HTML elements with these style names are written to the default feedback.
| *Class name* | *Note* |
| =sendEmailPluginNotification= | Used for feedback after sending a mail |
| =sendEmailPluginError= | Styles =sendEmailPluginNotification= in case of an error |


---++ Installation Instructions
%$INSTALL_INSTRUCTIONS%

---++ Change History
%TABLE{columnwidths="7em" tablewidth="100%"}%
| 20 May 2022 | 2.00 Michael Daum: remove dependency on CGI for rendering html; use utf8 encoding when sending emails; support ajax-sending mail forms |
| 09 Sep 2017 | 1.55 fix switchboard configuration |
| 15 Mar 2013 | 1.54 Michael Daum: don't send an empty CC; fix adding css to the header |
| 25 Aug 2011 | 1.53 Fixes a bug in the example form. |
Expand Down Expand Up @@ -373,12 +356,12 @@ HTML elements with these style names are written to the default feedback.

%META:FORM{name="PackageForm"}%
%META:FIELD{name="Author" title="Author" value="Foswiki:Main.ArthurClemens"}%
%META:FIELD{name="Copyright" title="Copyright" value="&copy; 2007-2010 Arthur Clemens; 2007-2017 Foswiki Contributors"}%
%META:FIELD{name="Version" title="Version" value="%25$VERSION%25"}%
%META:FIELD{name="Release" title="Release" value="%25$RELEASE%25"}%
%META:FIELD{name="Description" title="Description" value="%25$SHORTDESCRIPTION%25"}%
%META:FIELD{name="Home" title="Home" value="https://foswiki.org/Extensions/%TOPIC%"}%
%META:FIELD{name="Repository" title="Repository" value="https://github.com/foswiki/%25TOPIC%25"}%
%META:FIELD{name="Copyright" title="Copyright" value="&copy; 2007-2010 Arthur Clemens; 2007-2022 Foswiki Contributors"}%
%META:FIELD{name="License" title="License" value="GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]])"}%
%META:FIELD{name="Release" title="Release" value="%$RELEASE%"}%
%META:FIELD{name="Repository" title="Repository" value="https://github.com/foswiki/%TOPIC%"}%
%META:FIELD{name="Support" title="Support" value="https://foswiki.org/Support/%TOPIC%"}%
%META:FIELD{name="Version" title="Version" value="%$VERSION%"}%
%META:FIELD{name="Home" title="Home" value="https://foswiki.org/Extensions/%25TOPIC%25"}%
%META:FIELD{name="Support" title="Support" value="https://foswiki.org/Support/%25TOPIC%25"}%
%META:FILEATTACHMENT{name="screenshot.png" attr="" autoattached="1" comment="" date="1262991210" path="screenshot.png" size="16751" version=""}%
1 change: 1 addition & 0 deletions data/System/SendEmailPluginTemplate.txt
Expand Up @@ -4,6 +4,7 @@ From: %FROM%
To: %TO%
CC: %CC%
Subject: %SUBJECT%
Content-Type: text/plain; charset="UTF-8"
Auto-Submitted: auto-generated

%BODY%
9 changes: 3 additions & 6 deletions lib/Foswiki/Plugins/SendEmailPlugin.pm
@@ -1,7 +1,7 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2007-2011 Arthur Clemens (arthur@visiblearea.com)
# Copyright (c) 2007-2017 Foswiki Contributors
# Copyright (c) 2007-2022 Foswiki Contributors
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand All @@ -22,16 +22,13 @@ use warnings;

use Foswiki::Func();

our $VERSION = '1.55';
our $RELEASE = '25 Sep 2017';
our $VERSION = '2.00';
our $RELEASE = '20 May 2022';
our $pluginName = 'SendEmailPlugin';
our $SHORTDESCRIPTION = "Send e-mails through an e-mail form";
our $NO_PREFS_IN_TOPIC = 1;
our $topic;
our $web;

sub initPlugin {
( $topic, $web ) = @_;

# check for Plugins.pm versions
if ( $Foswiki::Plugins::VERSION < 1.026 ) {
Expand Down
39 changes: 28 additions & 11 deletions lib/Foswiki/Plugins/SendEmailPlugin/Core.pm
@@ -1,7 +1,7 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (c) 2007-2010 by Arthur Clemens
# Copyright (c) 2007-2017 Foswiki Contributors
# Copyright (c) 2007-2022 Foswiki Contributors
#
# and Foswiki Contributors. All Rights Reserved. Foswiki Contributors
# are listed in the AUTHORS file in the root of this distribution.
Expand All @@ -27,9 +27,9 @@ use warnings;
use Foswiki ();
use Foswiki::Func ();
use Foswiki::Plugins ();
use CGI qw( :all );

use vars qw( $debug $emailRE );
our $debug;
our $emailRE;

my $ERROR_STATUS_TAG = 'SendEmailErrorStatus';
my $ERROR_MESSAGE_TAG = 'SendEmailErrorMessage';
Expand Down Expand Up @@ -60,8 +60,8 @@ writes a debug message if the $debug flag is set
=cut

sub _debug {
Foswiki::Func::writeDebug("SendEmailPlugin -- $_[0]")
if $debug;
return unless $debug;
Foswiki::Func::writeDebug("SendEmailPlugin - $_[0]")
}

=pod
Expand Down Expand Up @@ -113,10 +113,10 @@ Invoked by bin/sendemail
sub sendEmail {
my $session = shift;

_debug("sendEmail");

init($session);

_debug("sendEmail");

my $query = Foswiki::Func::getCgiQuery();
my $errorMessage = '';
my $redirectUrl = $query->param('redirectto');
Expand Down Expand Up @@ -280,14 +280,15 @@ sub sendEmail {
# remove 'Template' at end - stupid TWiki solution from the old days
$templateName =~ s/^(.*?)Template$/$1/;

my $template = Foswiki::Func::readTemplate($templateName);
_debug("templateName=$templateName");
my $template = Foswiki::Func::readTemplate($templateName);
unless ($template) {
$template = <<'HERE';
From: %FROM%
To: %TO%
CC: %CC%
Subject: %SUBJECT%
Content-Type: text/plain; charset="UTF-8"
Auto-Submitted: auto-generated
%BODY%
Expand Down Expand Up @@ -455,7 +456,23 @@ sub _finishSendEmail {
$redirectUrl ||= $origUrl;
$redirectUrl = "$redirectUrl#$NOTIFICATION_ANCHOR_NAME";

Foswiki::Func::redirectCgiQuery( undef, $redirectUrl, 1 );
if ($query->header('X-Requested-With') eq 'XMLHttpRequest') {
#print STDERR "requested via ajax: errorStatus=$errorStatus errorMessage=".($errorMessage//'undef')."\n";

my $message = "Status: $errorStatus - $errorMessage";

if ($section) {
$message = Foswiki::Func::expandCommonVariables("%INCLUDE{\"$web.$topic\" section=\"$section\"}%", $topic, $web);
$message = Foswiki::Func::renderText($message, $web, $topic);
}
#print STDERR "message=$message\n";

$session->{response}->print($message);

} else {
Foswiki::Func::redirectCgiQuery( $query, $redirectUrl, 1 );
}

return 0;
}

Expand Down Expand Up @@ -486,7 +503,7 @@ sub _createNotificationMessage {
$cssClass .= ' ' . $NOTIFICATION_ERROR_CSS_CLASS
if ( $errorStatus == $ERROR_STATUS{'error'} );

return CGI::div( { class => $cssClass }, "$text $errorMessage" );
return "<div class='$cssClass'>$text $errorMessage</div>"
}

=pod
Expand All @@ -496,7 +513,7 @@ sub _createNotificationMessage {
sub _wrapHtmlNotificationContainer {
my ($notificationMessage) = @_;

return CGI::a( { name => $NOTIFICATION_ANCHOR_NAME }, '<!--#-->' ) . "\n"
return "<a name='$NOTIFICATION_ANCHOR_NAME' />\n"
. $notificationMessage;
}

Expand Down

0 comments on commit b5b12d8

Please sign in to comment.